Class PropertyAccessorFactory

java.lang.Object
com.alibaba.fastjson2.introspect.PropertyAccessorFactory
Direct Known Subclasses:
PropertyAccessorFactoryLambda

public class PropertyAccessorFactory extends Object
A factory class for creating property accessors that provide efficient getter and setter operations for object properties using reflection, method handles, or functional interfaces.

This factory supports different types of property access mechanisms:

  • Field-based access - Direct field access using reflection
  • Method-based access - Getter/setter method invocation
  • Functional access - Using functional interfaces for custom access logic

The factory creates specialized accessor implementations for different data types (primitives, String, BigInteger, BigDecimal) to optimize performance and avoid boxing/unboxing overhead where possible.

Since:
2.0
  • Constructor Details

    • PropertyAccessorFactory

      public PropertyAccessorFactory()
  • Method Details

    • createSupplier

      public Supplier createSupplier(Constructor constructor)
      Creates a Supplier that can instantiate objects using the given constructor. This method provides a way to create object instances via constructor reflection in a functional programming style.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a Supplier that creates new instances using the provided constructor
    • createFunction

      public Function createFunction(Constructor constructor)
      Creates a Function that can instantiate objects using the given constructor.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a Function that creates new instances using the provided constructor
    • createIntFunction

      public IntFunction createIntFunction(Constructor constructor)
      Creates a Function that can instantiate objects using the given constructor.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a Function that creates new instances using the provided constructor
    • createLongFunction

      public LongFunction createLongFunction(Constructor constructor)
      Creates a Function that can instantiate objects using the given constructor.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a Function that creates new instances using the provided constructor
    • createDoubleFunction

      public DoubleFunction createDoubleFunction(Constructor constructor)
      Creates a Function that can instantiate objects using the given constructor.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a Function that creates new instances using the provided constructor
    • createBiFunction

      public BiFunction createBiFunction(Constructor constructor)
      Creates a BiFunction that can instantiate objects using the given constructor.
      Parameters:
      constructor - the constructor to use for object instantiation
      Returns:
      a BiFunction that creates new instances using the provided constructor
    • create

      public PropertyAccessor create(Field field)
      Creates a property accessor for the specified field. This method analyzes the field type and returns an appropriate accessor implementation optimized for that type.
      Parameters:
      field - the field to create an accessor for
      Returns:
      a PropertyAccessor instance for the specified field
    • createInternal

      protected PropertyAccessor createInternal(Field field)
      Internal method to create a field-based property accessor based on the field's type. Returns a specialized accessor implementation depending on the field type (e.g., primitive, String, BigInteger, etc.).
      Parameters:
      field - the field to create an accessor for
      Returns:
      a specialized FieldAccessor instance for the field type
    • create

      public PropertyAccessor create(Method method)
      Creates a property accessor for the specified method. This method determines if the method is a getter (no parameters) or setter (one parameter) and creates an appropriate accessor that uses method invocation for property access.
      Parameters:
      method - the getter or setter method to create an accessor for
      Returns:
      a PropertyAccessor instance for the specified method
    • create

      public PropertyAccessor create(Method method, BiFunction<PropertyAccessor, Throwable, RuntimeException> exceptionHandler)
      Creates a property accessor for the specified method. This method determines if the method is a getter (no parameters) or setter (one parameter) and creates an appropriate accessor that uses method invocation for property access.
      Parameters:
      method - the getter or setter method to create an accessor for
      Returns:
      a PropertyAccessor instance for the specified method
    • create

      public final PropertyAccessor create(String name, Class<?> propertyClass, Type propertyType, Method getter, Method setter)
      Creates a property accessor using getter and/or setter methods with explicit type information. This method attempts to optimize access by using LambdaMetafactory to create efficient functional interfaces for property access when possible.
      Parameters:
      name - the property name
      propertyClass - the class of the property value
      propertyType - the generic type of the property value
      getter - the getter method (optional, may be null)
      setter - the setter method (optional, may be null)
      Returns:
      a PropertyAccessor instance for the specified getter/setter methods
    • create

      public PropertyAccessor create(String name, Class<?> propertyClass, Type propertyType, Method getter, Method setter, BiFunction<PropertyAccessor, Throwable, RuntimeException> exceptionHandler)
      Creates a property accessor using getter and/or setter methods. Validates that the getter has no parameters and the setter has one parameter. The property type is inferred from the getter's return type or setter's parameter type.
      Parameters:
      name - the property name
      propertyClass - the class of the property value
      propertyType - the generic type of the property value
      getter - the getter method (optional, may be null)
      setter - the setter method (optional, may be null)
      Returns:
      a PropertyAccessor instance for the specified getter/setter methods
      Throws:
      JSONException - if the getter or setter method signatures are invalid
    • create

      public <T> PropertyAccessor create(String name, ToByteFunction<T> getterFunc, ObjByteConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for byte property access. Uses ToByteFunction for getting the property value and ObjByteConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the byte property value from the object
      setterFunc - consumer to set the byte property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToShortFunction<T> getterFunc, ObjShortConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for short property access. Uses ToShortFunction for getting the property value and ObjShortConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the short property value from the object
      setterFunc - consumer to set the short property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToIntFunction<T> getterFunc, ObjIntConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for int property access. Uses ToIntFunction for getting the property value and ObjIntConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the int property value from the object
      setterFunc - consumer to set the int property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToLongFunction<T> getterFunc, ObjLongConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for long property access. Uses ToLongFunction for getting the property value and ObjLongConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the long property value from the object
      setterFunc - consumer to set the long property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToFloatFunction<T> getterFunc, ObjFloatConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for float property access. Uses ToFloatFunction for getting the property value and ObjFloatConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the float property value from the object
      setterFunc - consumer to set the float property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToDoubleFunction<T> getterFunc, ObjDoubleConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for double property access. Uses ToDoubleFunction for getting the property value and ObjDoubleConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the double property value from the object
      setterFunc - consumer to set the double property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, Predicate<T> getterFunc, ObjBoolConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for boolean property access. Uses Predicate for getting the property value and ObjBoolConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the boolean property value from the object
      setterFunc - consumer to set the boolean property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T> PropertyAccessor create(String name, ToCharFunction<T> getterFunc, ObjCharConsumer<T> setterFunc)
      Creates a property accessor using functional interfaces for char property access. Uses ToCharFunction for getting the property value and ObjCharConsumer for setting it.
      Type Parameters:
      T - the type of the object containing the property
      Parameters:
      name - the property name
      getterFunc - function to get the char property value from the object
      setterFunc - consumer to set the char property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public <T,V> PropertyAccessor create(String name, Class<?> propertyClass, Type propertyType, Function<T,V> getterFunc, BiConsumer<T,V> setterFunc)
    • create

      public <T,V> PropertyAccessor create(String name, Class<?> propertyClass, Type propertyType, Function<T,V> getterFunc, BiConsumer<T,V> setterFunc, BiFunction<PropertyAccessor, Throwable, RuntimeException> exceptionHandler)
      Creates a property accessor using functional interfaces for generic property access. Uses Function for getting the property value and BiConsumer for setting it. Supports String, BigInteger, and BigDecimal types with specialized implementations.
      Type Parameters:
      T - the type of the object containing the property
      V - the type of the property value
      Parameters:
      name - the property name
      propertyClass - the class of the property value
      propertyType - the generic type of the property value
      getterFunc - function to get the property value from the object
      setterFunc - consumer to set the property value on the object
      Returns:
      a PropertyAccessor instance using functional interfaces
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjBoolConsumer getterConsumer, ObjBoolConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjByteConsumer getterConsumer, ObjByteConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjCharConsumer getterConsumer, ObjCharConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjShortConsumer getterConsumer, ObjShortConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjIntConsumer getterConsumer, ObjIntConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjLongConsumer getterConsumer, ObjLongConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjFloatConsumer getterConsumer, ObjFloatConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, ObjDoubleConsumer getterConsumer, ObjDoubleConsumer setterConsumer)
    • create

      public PropertyAccessor create(PropertyAccessor impl, BiConsumer getterConsumer, BiConsumer setterConsumer)