View Javadoc
1   package org.junit.runners.model;
2   
3   import java.lang.annotation.Annotation;
4   import java.lang.reflect.Field;
5   
6   import org.junit.runners.BlockJUnit4ClassRunner;
7   
8   /**
9    * Represents a field on a test class (currently used only for Rules in
10   * {@link BlockJUnit4ClassRunner}, but custom runners can make other uses)
11   *
12   * @since 4.7
13   */
14  public class FrameworkField extends FrameworkMember<FrameworkField> {
15      private final Field field;
16  
17      FrameworkField(Field field) {
18          if (field == null) {
19              throw new NullPointerException(
20                      "FrameworkField cannot be created without an underlying field.");
21          }
22          this.field = field;
23      }
24  
25      @Override
26      public String getName() {
27          return getField().getName();
28      }
29  
30      public Annotation[] getAnnotations() {
31          return field.getAnnotations();
32      }
33  
34      public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
35          return field.getAnnotation(annotationType);
36      }
37  
38      @Override
39      public boolean isShadowedBy(FrameworkField otherMember) {
40          return otherMember.getName().equals(getName());
41      }
42  
43      @Override
44      protected int getModifiers() {
45          return field.getModifiers();
46      }
47  
48      /**
49       * @return the underlying java Field
50       */
51      public Field getField() {
52          return field;
53      }
54  
55      /**
56       * @return the underlying Java Field type
57       * @see java.lang.reflect.Field#getType()
58       */
59      @Override
60      public Class<?> getType() {
61          return field.getType();
62      }
63      
64      @Override
65      public Class<?> getDeclaringClass() {
66          return field.getDeclaringClass();
67      }
68  
69      /**
70       * Attempts to retrieve the value of this field on {@code target}
71       */
72      public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
73          return field.get(target);
74      }
75  
76      @Override
77      public String toString() {
78          return field.toString();
79      }
80  }