001 /*
002 * Created on Feb 5, 2008
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.StaticFieldType.newFieldType;
018 import static org.fest.reflect.field.StaticFieldTypeRef.newFieldTypeRef;
019 import static org.fest.util.Strings.isEmpty;
020
021 import org.fest.reflect.reference.TypeRef;
022
023
024 /**
025 * Understands the name of a static field to access using Java Reflection.
026 * <p>
027 * The following is an example of proper usage of this class:
028 * <pre>
029 * // Retrieves the value of the static field "count"
030 * int count = {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("count").{@link StaticFieldName#ofType(Class) ofType}(int.class).{@link StaticFieldType#in(Class) in}(Person.class).{@link Invoker#get() get}();
031 *
032 * // Sets the value of the static field "count" to 3
033 * {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("count").{@link StaticFieldName#ofType(Class) ofType}(int.class).{@link StaticFieldType#in(Class) in}(Person.class).{@link Invoker#set(Object) set}(3);
034 *
035 * // Retrieves the value of the static field "commonPowers"
036 * List<String> commmonPowers = {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link #ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}<List<String>>() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#get() get}();
037 *
038 * // Sets the value of the static field "commonPowers"
039 * List<String> commonPowers = new ArrayList<String>();
040 * commonPowers.add("jump");
041 * {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link #ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}<List<String>>() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#set(Object) set}(commonPowers);
042 * </pre>
043 * </p>
044 *
045 * @author Alex Ruiz
046 */
047 public final class StaticFieldName {
048
049 /**
050 * Creates a new <code>{@link StaticFieldName}</code>: the starting point of the fluent interface for accessing
051 * static fields using Java Reflection.
052 * @param name the name of the field to access using Java Reflection.
053 * @return the created <code>StaticFieldName</code>.
054 * @throws NullPointerException if the given name is <code>null</code>.
055 * @throws IllegalArgumentException if the given name is empty.
056 */
057 public static StaticFieldName beginStaticFieldAccess(String name) {
058 validateIsNotNullOrEmpty(name);
059 return new StaticFieldName(name);
060 }
061
062 private static void validateIsNotNullOrEmpty(String name) {
063 if (name == null)
064 throw new NullPointerException("The name of the static field to access should not be null");
065 if (isEmpty(name))
066 throw new IllegalArgumentException("The name of the static field to access should not be empty");
067 }
068
069 private final String name;
070
071 private StaticFieldName(String name) {
072 this.name = name;
073 }
074
075 /**
076 * Sets the type of the field to access.
077 * @param <T> the generic type of the field type.
078 * @param type the type of the field to access.
079 * @return a recipient for the field type.
080 * @throws NullPointerException if the given type is <code>null</code>.
081 */
082 public <T> StaticFieldType<T> ofType(Class<T> type) {
083 return newFieldType(name, type);
084 }
085
086 /**
087 * Sets the type reference of the field to access. This method reduces casting when the type of the field to access
088 * uses generics.
089 * <p>
090 * For example:
091 * <pre>
092 * List<String> commmonPowers = {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link #ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}<List<String>>() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#get() get}();
093 * </pre>
094 * </p>
095 * @param <T> the generic type of the field type.
096 * @param type the type of the field to access.
097 * @return a recipient for the field type.
098 * @throws NullPointerException if the given type reference is <code>null</code>.
099 */
100 public <T> StaticFieldTypeRef<T> ofType(TypeRef<T> type) {
101 return newFieldTypeRef(name, type);
102 }
103 }