|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StaticFieldTypeRef | Line # 43 | 6 | 0% | 4 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Feb 5, 2006 | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | |
| 5 | * the License. You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
| 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| 11 | * specific language governing permissions and limitations under the License. | |
| 12 | * | |
| 13 | * Copyright @2006-2009 the original author or authors. | |
| 14 | */ | |
| 15 | package org.fest.reflect.field; | |
| 16 | ||
| 17 | import static org.fest.reflect.field.Invoker.newInvoker; | |
| 18 | ||
| 19 | import org.fest.reflect.exception.ReflectionError; | |
| 20 | import org.fest.reflect.reference.TypeRef; | |
| 21 | ||
| 22 | /** | |
| 23 | * Understands the type of a static field to access using Java Reflection. | |
| 24 | * <p> | |
| 25 | * The following is an example of proper usage of this class: | |
| 26 | * <pre> | |
| 27 | * // Retrieves the value of the static field "commonPowers" | |
| 28 | * List<String> commmonPowers = {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link StaticFieldName#ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}<List<String>>() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#get() get}(); | |
| 29 | * | |
| 30 | * // Sets the value of the static field "commonPowers" | |
| 31 | * List<String> commonPowers = new ArrayList<String>(); | |
| 32 | * commonPowers.add("jump"); | |
| 33 | * {@link org.fest.reflect.core.Reflection#staticField(String) staticField}("commonPowers").{@link StaticFieldName#ofType(TypeRef) ofType}(new {@link TypeRef TypeRef}<List<String>>() {}).{@link StaticFieldTypeRef#in(Class) in}(Jedi.class).{@link Invoker#set(Object) set}(commonPowers); | |
| 34 | * </pre> | |
| 35 | * </p> | |
| 36 | * | |
| 37 | * @param <T> the generic type of the field. | |
| 38 | * | |
| 39 | * @author Alex Ruiz | |
| 40 | * | |
| 41 | * @since 1.1 | |
| 42 | */ | |
| 43 | public class StaticFieldTypeRef<T> { | |
| 44 | ||
| 45 | 3 |
static <T> StaticFieldTypeRef<T> newFieldTypeRef(String name, TypeRef<T> type) { |
| 46 | 3 | if (type == null) |
| 47 | 1 | throw new NullPointerException("The type reference of the static field to access should not be null"); |
| 48 | 2 | return new StaticFieldTypeRef<T>(name, type); |
| 49 | } | |
| 50 | ||
| 51 | private final String name; | |
| 52 | private final TypeRef<T> type; | |
| 53 | ||
| 54 | 2 |
private StaticFieldTypeRef(String name, TypeRef<T> type) { |
| 55 | 2 | this.name = name; |
| 56 | 2 | this.type = type; |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Returns a new field invoker. A field invoker is capable of accessing (read/write) the underlying field. | |
| 61 | * @param target the type containing the static field of interest. | |
| 62 | * @return the created field invoker. | |
| 63 | * @throws NullPointerException if the given target is <code>null</code>. | |
| 64 | * @throws ReflectionError if a static field with a matching name and type cannot be found. | |
| 65 | */ | |
| 66 | 2 |
public Invoker<T> in(Class<?> target) { |
| 67 | 2 | return newInvoker(name, type, target); |
| 68 | } | |
| 69 | } | |
|
||||||||||||