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