|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FieldType | Line # 38 | 6 | 0% | 4 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Aug 17, 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 | ||
| 21 | /** | |
| 22 | * Understands the type of a field to access using Java Reflection. | |
| 23 | * <p> | |
| 24 | * The following is an example of proper usage of this class: | |
| 25 | * <pre> | |
| 26 | * // Retrieves the value of the field "name" | |
| 27 | * String name = {@link org.fest.reflect.core.Reflection#field(String) field}("name").{@link FieldName#ofType(Class) ofType}(String.class).{@link FieldType#in(Object) in}(person).{@link Invoker#get() get}(); | |
| 28 | * | |
| 29 | * // Sets the value of the field "name" to "Yoda" | |
| 30 | * {@link org.fest.reflect.core.Reflection#field(String) field}("name").{@link FieldName#ofType(Class) ofType}(String.class).{@link FieldType#in(Object) in}(person).{@link Invoker#set(Object) set}("Yoda"); | |
| 31 | * </pre> | |
| 32 | * </p> | |
| 33 | * | |
| 34 | * @param <T> the generic type of the field. | |
| 35 | * | |
| 36 | * @author Alex Ruiz | |
| 37 | */ | |
| 38 | public class FieldType<T> { | |
| 39 | ||
| 40 | 9 |
static <T> FieldType<T> newFieldType(String name, Class<T> type) { |
| 41 | 9 | if (type == null) throw new NullPointerException("The type of the field to access should not be null"); |
| 42 | 8 | return new FieldType<T>(name, type); |
| 43 | } | |
| 44 | ||
| 45 | private final String name; | |
| 46 | private final Class<T> type; | |
| 47 | ||
| 48 | 8 |
private FieldType(String name, Class<T> type) { |
| 49 | 8 | this.name = name; |
| 50 | 8 | this.type = type; |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * Returns a new field access invoker, capable of accessing (read/write) the underlying field. | |
| 55 | * @param target the object containing the field of interest. | |
| 56 | * @return the created field access invoker. | |
| 57 | * @throws NullPointerException if the given target is <code>null</code>. | |
| 58 | * @throws ReflectionError if a field with a matching name and type cannot be found. | |
| 59 | */ | |
| 60 | 8 |
public Invoker<T> in(Object target) { |
| 61 | 8 | return newInvoker(name, type, target); |
| 62 | } | |
| 63 | } | |
|
||||||||||||