001 package org.junit.runners.model;
002
003 import java.lang.annotation.Annotation;
004 import java.lang.reflect.Field;
005
006 import org.junit.runners.BlockJUnit4ClassRunner;
007
008 /**
009 * Represents a field on a test class (currently used only for Rules in
010 * {@link BlockJUnit4ClassRunner}, but custom runners can make other uses)
011 *
012 * @since 4.7
013 */
014 public class FrameworkField extends FrameworkMember<FrameworkField> {
015 private final Field field;
016
017 FrameworkField(Field field) {
018 if (field == null) {
019 throw new NullPointerException(
020 "FrameworkField cannot be created without an underlying field.");
021 }
022 this.field = field;
023 }
024
025 @Override
026 public String getName() {
027 return getField().getName();
028 }
029
030 public Annotation[] getAnnotations() {
031 return field.getAnnotations();
032 }
033
034 public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
035 return field.getAnnotation(annotationType);
036 }
037
038 @Override
039 public boolean isShadowedBy(FrameworkField otherMember) {
040 return otherMember.getName().equals(getName());
041 }
042
043 @Override
044 protected int getModifiers() {
045 return field.getModifiers();
046 }
047
048 /**
049 * @return the underlying java Field
050 */
051 public Field getField() {
052 return field;
053 }
054
055 /**
056 * @return the underlying Java Field type
057 * @see java.lang.reflect.Field#getType()
058 */
059 @Override
060 public Class<?> getType() {
061 return field.getType();
062 }
063
064 @Override
065 public Class<?> getDeclaringClass() {
066 return field.getDeclaringClass();
067 }
068
069 /**
070 * Attempts to retrieve the value of this field on {@code target}
071 */
072 public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
073 return field.get(target);
074 }
075
076 @Override
077 public String toString() {
078 return field.toString();
079 }
080 }