Class PropertyAccessorFactory
java.lang.Object
com.alibaba.fastjson2.introspect.PropertyAccessorFactory
- Direct Known Subclasses:
PropertyAccessorFactoryLambda
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceInterface for property accessors that handle BigDecimal-typed properties.protected static interfaceInterface for property accessors that handle BigInteger-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle boolean-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle byte-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle char-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle double-typed properties.protected static interfaceprotected static interfaceprotected static interfaceInterface for property accessors that handle int-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle long-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle object-typed properties.protected static interfaceprotected static interfaceInterface for property accessors that handle short-typed properties.protected static interfaceInterface for property accessors that handle String-typed properties. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreate(PropertyAccessor impl, ObjBoolConsumer getterConsumer, ObjBoolConsumer setterConsumer) create(PropertyAccessor impl, ObjByteConsumer getterConsumer, ObjByteConsumer setterConsumer) create(PropertyAccessor impl, ObjCharConsumer getterConsumer, ObjCharConsumer setterConsumer) create(PropertyAccessor impl, ObjFloatConsumer getterConsumer, ObjFloatConsumer setterConsumer) create(PropertyAccessor impl, ObjShortConsumer getterConsumer, ObjShortConsumer setterConsumer) create(PropertyAccessor impl, BiConsumer getterConsumer, BiConsumer setterConsumer) create(PropertyAccessor impl, ObjDoubleConsumer getterConsumer, ObjDoubleConsumer setterConsumer) create(PropertyAccessor impl, ObjIntConsumer getterConsumer, ObjIntConsumer setterConsumer) create(PropertyAccessor impl, ObjLongConsumer getterConsumer, ObjLongConsumer setterConsumer) Creates a property accessor for the specified field.Creates a property accessor for the specified method.create(Method method, BiFunction<PropertyAccessor, Throwable, RuntimeException> exceptionHandler) Creates a property accessor for the specified method.<T> PropertyAccessorcreate(String name, ToByteFunction<T> getterFunc, ObjByteConsumer<T> setterFunc) Creates a property accessor using functional interfaces for byte property access.<T> PropertyAccessorcreate(String name, ToCharFunction<T> getterFunc, ObjCharConsumer<T> setterFunc) Creates a property accessor using functional interfaces for char property access.<T> PropertyAccessorcreate(String name, ToFloatFunction<T> getterFunc, ObjFloatConsumer<T> setterFunc) Creates a property accessor using functional interfaces for float property access.<T> PropertyAccessorcreate(String name, ToShortFunction<T> getterFunc, ObjShortConsumer<T> setterFunc) Creates a property accessor using functional interfaces for short property access.final PropertyAccessorCreates a property accessor using getter and/or setter methods with explicit type information.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.<T,V> PropertyAccessor create(String name, Class<?> propertyClass, Type propertyType, Function<T, V> getterFunc, BiConsumer<T, V> setterFunc) <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.<T> PropertyAccessorcreate(String name, Predicate<T> getterFunc, ObjBoolConsumer<T> setterFunc) Creates a property accessor using functional interfaces for boolean property access.<T> PropertyAccessorcreate(String name, ToDoubleFunction<T> getterFunc, ObjDoubleConsumer<T> setterFunc) Creates a property accessor using functional interfaces for double property access.<T> PropertyAccessorcreate(String name, ToIntFunction<T> getterFunc, ObjIntConsumer<T> setterFunc) Creates a property accessor using functional interfaces for int property access.<T> PropertyAccessorcreate(String name, ToLongFunction<T> getterFunc, ObjLongConsumer<T> setterFunc) Creates a property accessor using functional interfaces for long property access.createBiFunction(Constructor constructor) Creates a BiFunction that can instantiate objects using the given constructor.createDoubleFunction(Constructor constructor) Creates a Function that can instantiate objects using the given constructor.createFunction(Constructor constructor) Creates a Function that can instantiate objects using the given constructor.protected PropertyAccessorcreateInternal(Field field) Internal method to create a field-based property accessor based on the field's type.createIntFunction(Constructor constructor) Creates a Function that can instantiate objects using the given constructor.createLongFunction(Constructor constructor) Creates a Function that can instantiate objects using the given constructor.createSupplier(Constructor constructor) Creates a Supplier that can instantiate objects using the given constructor.
-
Constructor Details
-
PropertyAccessorFactory
public PropertyAccessorFactory()
-
-
Method Details
-
createSupplier
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
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
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
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
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
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
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
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
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 namepropertyClass- the class of the property valuepropertyType- the generic type of the property valuegetter- 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 namepropertyClass- the class of the property valuepropertyType- the generic type of the property valuegetter- 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 namegetterFunc- function to get the byte property value from the objectsetterFunc- 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 namegetterFunc- function to get the short property value from the objectsetterFunc- 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 namegetterFunc- function to get the int property value from the objectsetterFunc- 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 namegetterFunc- function to get the long property value from the objectsetterFunc- 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 namegetterFunc- function to get the float property value from the objectsetterFunc- 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 namegetterFunc- function to get the double property value from the objectsetterFunc- 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 namegetterFunc- function to get the boolean property value from the objectsetterFunc- 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 namegetterFunc- function to get the char property value from the objectsetterFunc- 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 propertyV- the type of the property value- Parameters:
name- the property namepropertyClass- the class of the property valuepropertyType- the generic type of the property valuegetterFunc- function to get the property value from the objectsetterFunc- 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)
-