Class FieldFinder.NamedFieldFinder<T>

java.lang.Object
nl.pim16aap2.util.reflection.ReflectionFinder<Field, FieldFinder.NamedFieldFinder<T>>
nl.pim16aap2.util.reflection.FieldFinder.NamedFieldFinder<T>
All Implemented Interfaces:
IAccessibleSetter<FieldFinder.NamedFieldFinder<T>>, IAnnotationFinder<FieldFinder.NamedFieldFinder<T>>
Enclosing class:
FieldFinder

public static final class FieldFinder.NamedFieldFinder<T> extends ReflectionFinder<Field, FieldFinder.NamedFieldFinder<T>>
Represents an implementation of ReflectionFinder to retrieve a field by its name.
  • Field Details

    • setAccessible

      protected boolean setAccessible
    • checkSuperClasses

      protected boolean checkSuperClasses
    • annotations

      protected Class<? extends Annotation>[] annotations
  • Method Details

    • get

      public Field get()
      Description copied from class: ReflectionFinder
      Retrieves the target reflection object(s) using the current configuration.

      This may throw an exception if the lookup hasn't been configured yet.

      Specified by:
      get in class ReflectionFinder<Field, FieldFinder.NamedFieldFinder<T>>
      Returns:
      The target reflection object(s) being searched for.
    • getNullable

      public @Nullable Field getNullable()
      Description copied from class: ReflectionFinder
      Retrieves the target reflection object(s) using the current configuration.

      This may throw an exception if the lookup hasn't been configured yet.

      Specified by:
      getNullable in class ReflectionFinder<Field, FieldFinder.NamedFieldFinder<T>>
      Returns:
      The target reflection object(s) being searched for or null if no object(s) could be found.
    • ofType

      @CheckReturnValue @Contract(pure=true) public <U> FieldFinder.NamedFieldFinder<U> ofType(Class<U> fieldType)
      Specifies the type the field should have.

      If this is set to null (default), the type of the field will be ignored.

      Parameters:
      fieldType - The type the field should have.
      Returns:
      The new instance of the current finder.
    • getTargetFieldType

      protected Class<T> getTargetFieldType()
      Gets the configured type of the target field.

      This is used to cast the field to the correct type when retrieving it.

      Returns:
      The type of the target field.
    • get

      protected T get(Field field, @Nullable Object instance)
      Retrieves the object of the field from the given instance.
      Parameters:
      field - The field to retrieve the object from.
      instance - The instance to retrieve the field from.

      This can be null if the field is static.

      Returns:
      The object of the field from the given instance.
    • get

      @CheckReturnValue public T get(@Nullable Object instance)
      Retrieves the object of the field from the given instance.

      The field will be retrieved using ReflectionFinder.get() first.

      Parameters:
      instance - The instance to retrieve the field from.

      This can be null if the field is static.

      Returns:
      The object of the field from the given instance.
      Throws:
      NullPointerException - If the field could not be found, if the instance was null and the field was not static, or if the object of the field was null.
      ClassCastException - If the field could be found, but could not be cast to the correct type.
      IllegalArgumentException - If the field could be found, but could not be accessed or
    • getNullable

      @CheckReturnValue public T getNullable(@Nullable Object instance)
      Retrieves the object of the field from the given instance.

      The field will be retrieved using ReflectionFinder.getNullable() first. If the field could not be found, this method will return null.

      Parameters:
      instance - The instance to retrieve the field from.

      This can be null if the field is static.

      Returns:
      The object of the field from the given instance.
      Throws:
      NullPointerException - If the field could not be found or if the instance was null and the field was not static.
      ClassCastException - If the field could be found, but could not be cast to the correct type.
      IllegalArgumentException - If the field could be found, but could not be accessed or
    • set

      public void set(@Nullable Object instance, @Nullable Object value)
      Sets the value of the field in the given instance.

      The field will be retrieved using ReflectionFinder.get() first.

      Parameters:
      instance - The instance to set the field in.
      value - The value to set the field to.
      Throws:
      NullPointerException - If the field could not be found or if the instance was null and the field was not static.
      IllegalArgumentException - If the field could not be accessed, or if the field could not be set to the given value.
    • setAccessible

      public FieldFinder.NamedFieldFinder<T> setAccessible()
      Description copied from interface: IAccessibleSetter
      Sets the returned reflection object(s) as accessible.

      See AccessibleObject.setAccessible(boolean).

      Specified by:
      setAccessible in interface IAccessibleSetter<T>
      Returns:
      The current object.
    • withAnnotations

      @SafeVarargs public final FieldFinder.NamedFieldFinder<T> withAnnotations(Class<? extends Annotation>... annotations)
      Description copied from interface: IAnnotationFinder
      Adds a requirement that the target constructor should contain at least a set of given annotations.
      Specified by:
      withAnnotations in interface IAnnotationFinder<T>
      Parameters:
      annotations - The annotations the constructor should have.
      Returns:
      The current object.
    • checkSuperClasses

      public FieldFinder.NamedFieldFinder<T> checkSuperClasses()
      Used to configure the field finder to include the source class's superclasses when searching for the target field.
      Returns:
      The current field finder instance.