Class FieldDefinition

  • All Implemented Interfaces:
    java.io.Serializable

    @Immutable
    public final class FieldDefinition
    extends MemberDefinition<java.lang.reflect.Field>
    The definition class of Field

    Example 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' field
     

    This 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
    • Constructor Detail

      • FieldDefinition

        public FieldDefinition​(@Nonnull
                               java.lang.String since,
                               @Nonnull
                               java.lang.String declaredClassName,
                               @Nonnull
                               java.lang.String fieldName)
        Parameters:
        since - the 'since' version
        declaredClassName - the name of declared class
        fieldName - 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' version
        deprecation - the deprecation
        declaredClassName - the name of declared class
        fieldName - the field name
      • FieldDefinition

        public FieldDefinition​(@Nonnull
                               Version since,
                               @Nonnull
                               java.lang.String declaredClassName,
                               @Nonnull
                               java.lang.String fieldName)
        Parameters:
        since - the 'since' version
        declaredClassName - the name of declared class
        fieldName - 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' version
        deprecation - the deprecation
        declaredClassName - the name of declared class
        fieldName - the field name
    • Method Detail

      • resolveMember

        protected java.lang.reflect.Field resolveMember()
        Description copied from class: MemberDefinition
        Resolve the member instance
        Specified by:
        resolveMember in class MemberDefinition<java.lang.reflect.Field>
        Returns:
        null if 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.IllegalArgumentException
        Get 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 instance
        fieldValue - 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).