Package io.microsphere.reflect
Class FieldDefinition
- java.lang.Object
-
- io.microsphere.reflect.ReflectiveDefinition
-
- io.microsphere.reflect.MemberDefinition<java.lang.reflect.Field>
-
- io.microsphere.reflect.FieldDefinition
-
- All Implemented Interfaces:
java.io.Serializable
@Immutable public final class FieldDefinition extends MemberDefinition<java.lang.reflect.Field>
The definition class ofFieldExample Usage
// Create a FieldDefinition for the 'name' field of the User class, introduced in version 1.0.0. FieldDefinition fieldDef = new FieldDefinition("1.0.0", "com.example.User", "name"); // Get the resolved Field object (null if not found or not resolvable) Field field = fieldDef.getResolvedField(); // Access and modify the field value on an instance User user = new User(); fieldDef.set(user, "John Doe"); // Set the 'name' field to "John Doe" String name = fieldDef.get(user); // Retrieve the value of 'name' fieldThis class provides utilities to define, resolve, and manipulate fields reflectively, with support for versioning and deprecation information inherited from
MemberDefinition.- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Field, Serialized Form
-
-
Field Summary
-
Fields inherited from class io.microsphere.reflect.MemberDefinition
name
-
Fields inherited from class io.microsphere.reflect.ReflectiveDefinition
className, deprecation, since
-
-
Constructor Summary
Constructors Constructor Description FieldDefinition(Version since, Deprecation deprecation, java.lang.String declaredClassName, java.lang.String fieldName)FieldDefinition(Version since, java.lang.String declaredClassName, java.lang.String fieldName)FieldDefinition(java.lang.String since, Deprecation deprecation, java.lang.String declaredClassName, java.lang.String fieldName)FieldDefinition(java.lang.String since, java.lang.String declaredClassName, java.lang.String fieldName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Tget(java.lang.Object instance)Get the field valuejava.lang.StringgetFieldName()java.lang.reflect.FieldgetResolvedField()protected java.lang.reflect.FieldresolveMember()Resolve thememberinstance<T> Tset(java.lang.Object instance, T fieldValue)Set the field value-
Methods inherited from class io.microsphere.reflect.MemberDefinition
equals, getDeclaredClass, getDeclaredClassName, getMember, getName, hashCode, isPresent
-
Methods inherited from class io.microsphere.reflect.ReflectiveDefinition
getClassName, getDeprecation, getResolvedClass, getSince, isDeprecated, toString
-
-
-
-
Constructor Detail
-
FieldDefinition
public FieldDefinition(@Nonnull java.lang.String since, @Nonnull java.lang.String declaredClassName, @Nonnull java.lang.String fieldName)
- Parameters:
since- the 'since' versiondeclaredClassName- the name of declared classfieldName- the field name
-
FieldDefinition
public FieldDefinition(@Nonnull java.lang.String since, @Nullable Deprecation deprecation, @Nonnull java.lang.String declaredClassName, @Nonnull java.lang.String fieldName)
- Parameters:
since- the 'since' versiondeprecation- the deprecationdeclaredClassName- the name of declared classfieldName- the field name
-
FieldDefinition
public FieldDefinition(@Nonnull Version since, @Nonnull java.lang.String declaredClassName, @Nonnull java.lang.String fieldName)
- Parameters:
since- the 'since' versiondeclaredClassName- the name of declared classfieldName- the field name
-
FieldDefinition
public FieldDefinition(@Nonnull Version since, @Nullable Deprecation deprecation, @Nonnull java.lang.String declaredClassName, @Nonnull java.lang.String fieldName)
- Parameters:
since- the 'since' versiondeprecation- the deprecationdeclaredClassName- the name of declared classfieldName- the field name
-
-
Method Detail
-
resolveMember
protected java.lang.reflect.Field resolveMember()
Description copied from class:MemberDefinitionResolve thememberinstance- Specified by:
resolveMemberin classMemberDefinition<java.lang.reflect.Field>- Returns:
nullif can't be resolved
-
getFieldName
@Nonnull public java.lang.String getFieldName()
-
getResolvedField
@Nullable public java.lang.reflect.Field getResolvedField()
-
get
public <T> T get(java.lang.Object instance) throws java.lang.IllegalStateException, java.lang.IllegalArgumentExceptionGet the field value- Type Parameters:
T- the field type- Parameters:
instance- the instance- Returns:
null- Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
set
public <T> T set(java.lang.Object instance, T fieldValue)Set the field value- Type Parameters:
T- the field type- Parameters:
instance- the instancefieldValue- the value of field to be set- Returns:
- the previous value of the specified
Field - Throws:
java.lang.IllegalStateException- if this Field object is enforcing Java language access control and the underlying field is inaccessible.java.lang.IllegalArgumentException- if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
-
-