001    /*
002     * Created on Feb 5, 2006
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2006-2009 the original author or authors.
014     */
015    package org.fest.reflect.field;
016    
017    import static org.fest.reflect.field.Invoker.newInvoker;
018    
019    import org.fest.reflect.exception.ReflectionError;
020    import org.fest.reflect.reference.TypeRef;
021    
022    /**
023     * Understands the type of a static field to access using Java Reflection.
024     * <p>
025     * The following is an example of proper usage of this class:
026     * <pre>
027     *   // Retrieves the value of the static field "commonPowers"
028     *   List&lt;String&gt; commmonPowers = {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link StaticFieldName#ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}&lt;List&lt;String&gt;&gt;() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#get() get}();
029     *
030     *   // Sets the value of the static field "commonPowers"
031     *   List&lt;String&gt; commonPowers = new ArrayList&lt;String&gt;();
032     *   commonPowers.add("jump");
033     *   {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link StaticFieldName#ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}&lt;List&lt;String&gt;&gt;() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#set(Object) set}(commonPowers);
034     * </pre>
035     * </p>
036     *
037     * @param <T> the generic type of the field.
038     *
039     * @author Alex Ruiz
040     *
041     * @since 1.1
042     */
043    public class StaticFieldTypeRef<T> {
044    
045      static <T> StaticFieldTypeRef<T> newFieldTypeRef(String name, TypeRef<T> type) {
046        if (type == null)
047          throw new NullPointerException("The type reference of the static field to access should not be null");
048        return new StaticFieldTypeRef<T>(name, type);
049      }
050    
051      private final String name;
052      private final TypeRef<T> type;
053    
054      private StaticFieldTypeRef(String name, TypeRef<T> type) {
055        this.name = name;
056        this.type = type;
057      }
058    
059      /**
060       * Returns a new field invoker. A field invoker is capable of accessing (read/write) the underlying field.
061       * @param target the type containing the static field of interest.
062       * @return the created field invoker.
063       * @throws NullPointerException if the given target is <code>null</code>.
064       * @throws ReflectionError if a static field with a matching name and type cannot be found.
065       */
066      public Invoker<T> in(Class<?> target) {
067        return newInvoker(name, type, target);
068      }
069    }