Class FieldInfo

All Implemented Interfaces:
Comparable<FieldInfo>, Annotatable

public class FieldInfo extends AccessibleInfo implements Comparable<FieldInfo>, Annotatable
Lightweight utility class for introspecting information about a Java field.

This class provides a convenient wrapper around Field that extends the standard Java reflection API with additional functionality for field introspection, annotation handling, and value access. It extends AccessibleInfo to provide AccessibleObject functionality for accessing private fields.

Features:
  • Field introspection - access field metadata, type, modifiers
  • Annotation support - get annotations declared on the field
  • Value access - get and set field values with type safety
  • Accessibility control - make private fields accessible
  • Thread-safe - instances are immutable and safe for concurrent access
Use Cases:
  • Introspecting field metadata for code generation or analysis
  • Accessing field values in beans or data objects
  • Finding annotations on fields
  • Working with field types and modifiers
  • Building frameworks that need to analyze or manipulate field values
Usage:

// Get FieldInfo from a class ClassInfo ci = ClassInfo.of(MyClass.class); FieldInfo field = ci.getField("myField"); // Get field type ClassInfo type = field.getType(); // Get annotations List<AnnotationInfo<MyAnnotation>> annotations = field.getAnnotations(MyAnnotation.class).toList(); // Access field value MyClass obj = new MyClass(); field.accessible(); // Make accessible if private Object value = field.get(obj);

See Also:
  • Constructor Details

    • FieldInfo

      protected FieldInfo(ClassInfo declaringClass, Field inner)
      Constructor.

      Creates a new FieldInfo wrapper for the specified field. This constructor is protected and should not be called directly. Use the static factory methods of(Field) or obtain FieldInfo instances from ClassInfo.getField(Field).

      Parameters:
      declaringClass - The ClassInfo for the class that declares this field.
      inner - The field being wrapped.
  • Method Details

    • of

      public static FieldInfo of(ClassInfo declaringClass, Field inner)
      Creates a FieldInfo wrapper for the specified field.
      Example:

      ClassInfo ci = ClassInfo.of(MyClass.class); Field f = MyClass.class.getField("myField"); FieldInfo fi = FieldInfo.of(ci, f);

      Parameters:
      declaringClass - The ClassInfo for the class that declares this field. Must not be null.
      inner - The field being wrapped. Must not be null.
      Returns:
      A new FieldInfo object wrapping the field.
    • of

      public static FieldInfo of(Field inner)
      Creates a FieldInfo wrapper for the specified field.

      This convenience method automatically determines the declaring class from the field.

      Example:

      Field f = MyClass.class.getField("myField"); FieldInfo fi = FieldInfo.of(f);

      Parameters:
      inner - The field being wrapped. Must not be null.
      Returns:
      A new FieldInfo object wrapping the field.
    • accessible

      Attempts to call x.setAccessible(true) and quietly ignores security exceptions.
      Returns:
      This object.
    • compareTo

      public int compareTo(FieldInfo o)
      Specified by:
      compareTo in interface Comparable<FieldInfo>
    • get

      public <T> T get(Object o) throws BeanRuntimeException
      Returns the field value on the specified object.
      Type Parameters:
      T - The object type to retrieve.
      Parameters:
      o - The object containing the field.
      Returns:
      The field value.
      Throws:
      BeanRuntimeException - Field was not accessible or field does not belong to object.
    • getAnnotatableType

      Description copied from interface: Annotatable
      Returns the type of this annotatable object.
      Specified by:
      getAnnotatableType in interface Annotatable
      Returns:
      The type of annotatable object this represents.
    • getAnnotatedType

      Returns an AnnotatedType object that represents the use of a type to specify the declared type of the field.

      Same as calling Field.getAnnotatedType().

      Example:

      // Get annotated type: @NotNull String name FieldInfo fi = ClassInfo.of(MyClass.class).getField("name"); AnnotatedType aType = fi.getAnnotatedType(); // Check for @NotNull on the type

      Returns:
      An AnnotatedType object representing the declared type.
      See Also:
    • getAnnotations

      Returns all annotations declared on this field.

      Note on Repeatable Annotations: Repeatable annotations (those marked with @Repeatable) are automatically expanded into their individual annotation instances. For example, if a field has multiple @Bean annotations, this method returns each @Bean annotation separately, rather than the container annotation.

      Returns:
      An unmodifiable list of all annotations declared on this field.
      Repeatable annotations are expanded into individual instances.
    • getAnnotations

      public <A extends Annotation> Stream<AnnotationInfo<A>> getAnnotations(Class<A> type)
      Returns all annotations of the specified type declared on this field.
      Type Parameters:
      A - The annotation type.
      Parameters:
      type - The annotation type.
      Returns:
      A stream of all matching annotations.
    • getDeclaringClass

      Returns metadata about the declaring class.
      Returns:
      Metadata about the declaring class.
    • getFieldType

      Returns the type of this field.
      Returns:
      The type of this field.
    • getFullName

      public String getFullName()
      Returns the full name of this field.
      Examples:
      • "com.foo.MyClass.myField" - Method.
      Returns:
      The underlying executable name.
    • getLabel

      public String getLabel()
      Description copied from interface: Annotatable
      Returns a human-readable label for this annotatable element.

      The label format depends on the type of annotatable:

      • CLASS_TYPE - Simple class name (e.g., "MyClass")
      • METHOD_TYPE - Class and method with parameter types (e.g., "MyClass.myMethod(String,int)")
      • FIELD_TYPE - Class and field name (e.g., "MyClass.myField")
      • CONSTRUCTOR_TYPE - Class and constructor with parameter types (e.g., "MyClass.MyClass(String)")
      • PARAMETER_TYPE - Class, method/constructor, and parameter index (e.g., "MyClass.myMethod[0]")
      • PACKAGE_TYPE - Package name (e.g., "com.example.package")
      Specified by:
      getLabel in interface Annotatable
      Returns:
      The human-readable label for this annotatable element.
    • getName

      public String getName()
      Returns the name of this field.
      Returns:
      The name of this field.
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type)
      Returns true if the specified annotation is present.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      Returns:
      true if the specified annotation is present.
    • hasName

      public boolean hasName(String name)
      Returns true if the field has the specified name.
      Parameters:
      name - The name to compare against.
      Returns:
      true if the field has the specified name.
    • inner

      public Field inner()
      Returns the wrapped field.
      Returns:
      The wrapped field.
    • equals

      public boolean equals(Object obj)
      Compares this FieldInfo with the specified object for equality.

      Two FieldInfo objects are considered equal if they wrap the same underlying Field object. This delegates to the underlying Field.equals(Object) method.

      This method makes FieldInfo suitable for use as keys in hash-based collections such as HashMap and HashSet.

      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare with.
      Returns:
      true if the objects are equal, false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code value for this FieldInfo.

      This delegates to the underlying Field.hashCode() method.

      This method makes FieldInfo suitable for use as keys in hash-based collections such as HashMap and HashSet.

      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this FieldInfo.
    • is

      public boolean is(ElementFlag flag)
      Returns true if all specified flags are applicable to this field.
      Overrides:
      is in class ElementInfo
      Parameters:
      flag - The flag to test for.
      Returns:
      true if all specified flags are applicable to this field.
    • isDeprecated

      public boolean isDeprecated()
      Returns true if this field has the @Deprecated annotation on it.
      Returns:
      true if this field has the @Deprecated annotation on it.
    • isEnumConstant

      public boolean isEnumConstant()
      Returns true if this field represents an element of an enumerated type.

      Same as calling Field.isEnumConstant().

      Example:

      // Check if field is an enum constant FieldInfo fi = ClassInfo.of(MyEnum.class).getField("VALUE1"); if (fi.isEnumConstant()) { // Handle enum constant }

      Returns:
      true if this field represents an enum constant.
      See Also:
    • isNotDeprecated

      public boolean isNotDeprecated()
      Returns true if this field doesn't have the @Deprecated annotation on it.
      Returns:
      true if this field doesn't have the @Deprecated annotation on it.
    • isSynthetic

      public boolean isSynthetic()
      Returns true if this field is a synthetic field as defined by the Java Language Specification.

      Same as calling Field.isSynthetic().

      Example:

      // Filter out compiler-generated fields FieldInfo fi = ...; if (! fi.isSynthetic()) { // Process real field }

      Returns:
      true if this field is a synthetic field.
      See Also:
    • isVisible

      public boolean isVisible(Visibility v)
      Identifies if the specified visibility matches this field.
      Parameters:
      v - The visibility to validate against.
      Returns:
      true if this visibility matches the modifier attribute of this field.
    • set

      public void set(Object o, Object value) throws BeanRuntimeException
      Sets the field value on the specified object.
      Parameters:
      o - The object containing the field.
      value - The new field value.
      Throws:
      BeanRuntimeException - Field was not accessible or field does not belong to object.
    • setIfNull

      public void setIfNull(Object o, Object value)
      Sets the field value on the specified object if the value is null.
      Parameters:
      o - The object containing the field.
      value - The new field value.
      Throws:
      BeanRuntimeException - Field was not accessible or field does not belong to object.
    • toGenericString

      Returns a string describing this field, including its generic type.

      Same as calling Field.toGenericString().

      Example:

      // Get generic string for: public static final List<String> VALUES FieldInfo fi = ClassInfo.of(MyClass.class).getField("VALUES"); String str = fi.toGenericString(); // Returns: "public static final java.util.List<java.lang.String> com.example.MyClass.VALUES"

      Returns:
      A string describing this field.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object