package com.jn.langx.util.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;

public class FieldAttributes implements MemberAttributes<Field>{
    private final Field field;

    /**
     * Constructs a Field Attributes object from the {@code f}.
     *
     * @param f the field to pull attributes from
     */
    public FieldAttributes(Field f) {
        this.field = f;
    }

    @Override
    public int getModifier() {
        return field.getModifiers();
    }

    public Field get(){
        return this.field;
    }

    /**
     * @return the declaring class that contains this field
     */
    public Class<?> getDeclaringClass() {
        return field.getDeclaringClass();
    }

    /**
     * @return the name of the field
     */
    public String getName() {
        return field.getName();
    }

    /**
     * <p>For example, assume the following class definition:
     * <pre class="code">
     * public class Foo {
     * private String bar;
     * private List&lt;String&gt; red;
     * }
     * <p>
     * Type listParameterizedType = new TypeToken&lt;List&lt;String&gt;&gt;() {}.getType();
     * </pre>
     * <p>
     * <p>This method would return {@code String.class} for the {@code bar} field and
     * {@code listParameterizedType} for the {@code red} field.
     *
     * @return the specific type declared for this field
     */
    @Deprecated
    public Type getDeclaredType() {
        return field.getGenericType();
    }

    /**
     * Returns the {@code Class} object that was declared for this field.
     * <p>
     * <p>For example, assume the following class definition:
     * <pre class="code">
     * public class Foo {
     * private String bar;
     * private List&lt;String&gt; red;
     * }
     * </pre>
     * <p>
     * <p>This method would return {@code String.class} for the {@code bar} field and
     * {@code List.class} for the {@code red} field.
     *
     * @return the specific class object that was declared for the field
     */
    public Class<?> getDeclaredClass() {
        return field.getType();
    }

    /**
     * Return the {@code T} annotation object from this field if it exist; otherwise returns
     * {@code null}.
     *
     * @param annotation the class of the annotation that will be retrieved
     * @return the annotation instance if it is bound to the field; otherwise {@code null}
     */
    public <T extends Annotation> T getAnnotation(Class<T> annotation) {
        return field.getAnnotation(annotation);
    }

    /**
     * Return the annotations that are present on this field.
     *
     * @return an array of all the annotations set on the field
     * @since 1.4
     */
    public Collection<Annotation> getAnnotations() {
        return Arrays.asList(field.getAnnotations());
    }

    /**
     * Returns {@code true} if the field is defined with the {@code modifier}.
     * <p>
     * <p>This method is meant to be called as:
     * <pre class="code">
     * boolean hasPublicModifier = fieldAttribute.hasModifier(java.lang.reflect.Modifier.PUBLIC);
     * </pre>
     *
     * @see java.lang.reflect.Modifier
     */
    public boolean hasModifier(int modifier) {
        return Modifiers.hasModifier(field, modifier);
    }

    /**
     * Returns the value of the field represented by this {@code Field}, on
     * the specified object. The value is automatically wrapped in an
     * object if it has a primitive type.
     *
     * @return the value of the represented field in object
     * {@code obj}; primitive values are wrapped in an appropriate
     * object before being returned
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    Object get(Object instance) throws IllegalAccessException {
        return field.get(instance);
    }

    /**
     * This is exposed internally only for the removing synthetic fields from the JSON output.
     *
     * @return true if the field is synthetic; otherwise false
     */
    boolean isSynthetic() {
        return field.isSynthetic();
    }
}
