Class FieldInfo
- All Implemented Interfaces:
Comparable<FieldInfo>,Annotatable
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:
See Also:
ClassInfo- Class introspectionMethodInfo- Method introspectionConstructorInfo- Constructor introspection- Reflection Package
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAttempts to callx.setAccessible(and quietly ignores security exceptions.true )intbooleanCompares this FieldInfo with the specified object for equality.<T> TReturns the field value on the specified object.Returns the type of this annotatable object.Returns anAnnotatedTypeobject that represents the use of a type to specify the declared type of the field.Returns all annotations declared on this field.<A extends Annotation>
Stream<AnnotationInfo<A>>getAnnotations(Class<A> type) Returns all annotations of the specified type declared on this field.Returns metadata about the declaring class.Returns the type of this field.Returns the full name of this field.getLabel()Returns a human-readable label for this annotatable element.getName()Returns the name of this field.<A extends Annotation>
booleanhasAnnotation(Class<A> type) Returnstrue if the specified annotation is present.inthashCode()Returns a hash code value for this FieldInfo.booleanReturnstrue if the field has the specified name.inner()Returns the wrapped field.booleanis(ElementFlag flag) Returnstrue if all specified flags are applicable to this field.booleanReturnstrue if this field has the@Deprecatedannotation on it.booleanReturnstrue if this field represents an element of an enumerated type.booleanReturnstrue if this field doesn't have the@Deprecatedannotation on it.booleanReturnstrue if this field is a synthetic field as defined by the Java Language Specification.booleanIdentifies if the specified visibility matches this field.static FieldInfoCreates a FieldInfo wrapper for the specified field.static FieldInfoCreates a FieldInfo wrapper for the specified field.voidSets the field value on the specified object.voidSets the field value on the specified object if the value isnull .Returns a string describing this field, including its generic type.toString()Methods inherited from class org.apache.juneau.commons.reflect.AccessibleInfo
isAccessible, setAccessibleMethods inherited from class org.apache.juneau.commons.reflect.ElementInfo
ai, getModifiers, isAbstract, isAll, isAny, isFinal, isInterface, isNative, isNotAbstract, isNotFinal, isNotInterface, isNotNative, isNotPrivate, isNotProtected, isNotPublic, isNotStatic, isNotSynchronized, isNotTransient, isNotVolatile, isPrivate, isProtected, isPublic, isStatic, isSynchronized, isTransient, isVolatile
-
Constructor Details
-
FieldInfo
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 fromClassInfo.getField(Field).- Parameters:
declaringClass- The ClassInfo for the class that declares this field.inner- The field being wrapped.
-
-
Method Details
-
of
Creates a FieldInfo wrapper for the specified field.Example:
ClassInfo
ci = ClassInfo.of (MyClass.class ); Fieldf = MyClass.class .getField("myField" ); FieldInfofi = FieldInfo.of (ci ,f );- Parameters:
declaringClass- The ClassInfo for the class that declares this field. Must not benull .inner- The field being wrapped. Must not benull .- Returns:
- A new FieldInfo object wrapping the field.
-
of
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" ); FieldInfofi = FieldInfo.of (f );- Parameters:
inner- The field being wrapped. Must not benull .- Returns:
- A new FieldInfo object wrapping the field.
-
accessible
Attempts to callx.setAccessible(and quietly ignores security exceptions.true )- Returns:
- This object.
-
compareTo
- Specified by:
compareToin interfaceComparable<FieldInfo>
-
get
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:AnnotatableReturns the type of this annotatable object.- Specified by:
getAnnotatableTypein interfaceAnnotatable- Returns:
- The type of annotatable object this represents.
-
getAnnotatedType
Returns anAnnotatedTypeobject 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 FieldInfofi = ClassInfo.of (MyClass.class ).getField("name" ); AnnotatedTypeaType =fi .getAnnotatedType();// Check for @NotNull on the type - Returns:
- An
AnnotatedTypeobject 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@Beanannotations, this method returns each@Beanannotation 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
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
Returns the full name of this field.Examples:
"com.foo.MyClass.myField" - Method.
- Returns:
- The underlying executable name.
-
getLabel
Description copied from interface:AnnotatableReturns 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:
getLabelin interfaceAnnotatable- Returns:
- The human-readable label for this annotatable element.
-
getName
Returns the name of this field.- Returns:
- The name of this field.
-
hasAnnotation
Returnstrue 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
Returnstrue if the field has the specified name.- Parameters:
name- The name to compare against.- Returns:
true if the field has the specified name.
-
inner
Returns the wrapped field.- Returns:
- The wrapped field.
-
equals
Compares this FieldInfo with the specified object for equality.Two FieldInfo objects are considered equal if they wrap the same underlying
Fieldobject. This delegates to the underlyingField.equals(Object)method.This method makes FieldInfo suitable for use as keys in hash-based collections such as
HashMapandHashSet. -
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
HashMapandHashSet. -
is
Returnstrue if all specified flags are applicable to this field.- Overrides:
isin classElementInfo- Parameters:
flag- The flag to test for.- Returns:
true if all specified flags are applicable to this field.
-
isDeprecated
Returnstrue if this field has the@Deprecatedannotation on it.- Returns:
true if this field has the@Deprecatedannotation on it.
-
isEnumConstant
Returnstrue if this field represents an element of an enumerated type.Same as calling
Field.isEnumConstant().Example:
// Check if field is an enum constant FieldInfofi = ClassInfo.of (MyEnum.class ).getField("VALUE1" );if (fi .isEnumConstant()) {// Handle enum constant }- Returns:
true if this field represents an enum constant.- See Also:
-
isNotDeprecated
Returnstrue if this field doesn't have the@Deprecatedannotation on it.- Returns:
true if this field doesn't have the@Deprecatedannotation on it.
-
isSynthetic
Returnstrue 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 FieldInfofi = ...;if (!fi .isSynthetic()) {// Process real field }- Returns:
true if this field is a synthetic field.- See Also:
-
isVisible
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
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
Sets the field value on the specified object if the value isnull .- 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 FieldInfofi = ClassInfo.of (MyClass.class ).getField("VALUES" ); Stringstr =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
-