|
| 1 | +# Fastjson2 Introspect Package Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `com.alibaba.fastjson2.introspect` package provides a comprehensive reflection-based property access functionality for fastjson2. This package contains classes and interfaces for efficient property access during JSON serialization and deserialization operations. |
| 6 | + |
| 7 | +The core of this package is the `PropertyAccessor` interface, which provides a unified API for getting and setting object properties regardless of the underlying access mechanism (field access, method calls, or functional interfaces). |
| 8 | + |
| 9 | +The `PropertyAccessorFactory` class serves as the main factory for creating optimized property accessors based on the property type and access method, providing implementations for primitive types, wrapper classes, and complex objects. |
| 10 | + |
| 11 | +## Key Features |
| 12 | + |
| 13 | +- Optimized accessors for primitive types to avoid boxing/unboxing overhead |
| 14 | +- Support for both field-based and method-based property access |
| 15 | +- Functional interface-based accessors for maximum performance |
| 16 | +- Automatic type conversion between compatible types |
| 17 | +- Exception handling with detailed error messages |
| 18 | +- Multiple implementation strategies for different performance requirements |
| 19 | + |
| 20 | +## Architecture |
| 21 | + |
| 22 | +The introspect package implements a hierarchical architecture with multiple access strategies: |
| 23 | + |
| 24 | +### 1. PropertyAccessor Interface |
| 25 | + |
| 26 | +The `PropertyAccessor` interface is the core abstraction that defines methods for accessing object properties generically. It provides unified methods for getting and setting properties of objects, supporting both primitive types and objects through various accessor methods. |
| 27 | + |
| 28 | +Key methods include: |
| 29 | +- `getObject()`, `getByteValue()`, `getCharValue()`, `getShortValue()`, `getIntValue()`, `getLongValue()`, `getFloatValue()`, `getDoubleValue()`, `getBooleanValue()` |
| 30 | +- `setObject()`, `setByteValue()`, `setCharValue()`, `setShortValue()`, `setIntValue()`, `setLongValue()`, `setFloatValue()`, `setDoubleValue()`, `setBooleanValue()` |
| 31 | + |
| 32 | +### 2. Base Accessor Classes |
| 33 | + |
| 34 | +The package provides three abstract base classes that implement different access strategies: |
| 35 | + |
| 36 | +#### FieldAccessor |
| 37 | +- Abstract base class for field-based property accessors |
| 38 | +- Provides common functionality for accessing object fields using reflection |
| 39 | +- Handles field metadata and determines if the field supports setting based on whether the field is declared as final |
| 40 | +- Implements PropertyAccessor interface using direct field access through reflection |
| 41 | + |
| 42 | +#### MethodAccessor |
| 43 | +- Abstract base class for method-based property accessors |
| 44 | +- Provides common functionality for accessing object properties using getter and setter methods |
| 45 | +- Allows for accessing properties through standard getter/setter method pairs |
| 46 | +- Implements PropertyAccessor interface using method invocation for property access |
| 47 | + |
| 48 | +#### FunctionAccessor |
| 49 | +- Abstract base class for function-based property accessors |
| 50 | +- Provides common functionality for accessing object properties using getter and setter functions |
| 51 | +- Allows for accessing properties through functional interfaces rather than direct field access or method invocation |
| 52 | +- Implements PropertyAccessor interface using functional interfaces for property access |
| 53 | + |
| 54 | +### 3. Property Accessor Factory Hierarchy |
| 55 | + |
| 56 | +The package includes a sophisticated factory hierarchy that provides multiple strategies for property access: |
| 57 | + |
| 58 | +#### PropertyAccessorFactory |
| 59 | +- The main factory class that creates property accessors using reflection |
| 60 | +- Provides optimized accessor implementations for different data types (primitives, String, BigInteger, BigDecimal) to optimize performance and avoid boxing/unboxing overhead where possible |
| 61 | +- Creates specialized accessor implementations for different data types |
| 62 | + |
| 63 | +#### PropertyAccessorFactoryLambda |
| 64 | +- Extends PropertyAccessorFactory and adds support for LambdaMetafactory-based access |
| 65 | +- Creates efficient functional interfaces for property access when possible |
| 66 | +- Provides optimized constructor instantiation using MethodHandle and LambdaMetafactory |
| 67 | + |
| 68 | +#### PropertyAccessorFactoryMethodHandle |
| 69 | +- Available on JDK 11+, uses MethodHandles.Lookup for field and method access |
| 70 | +- Uses MethodHandles.Lookup's unreflectGetter/unreflectSetter instead of VarHandle for field access |
| 71 | +- Provides an alternative way to access object properties efficiently |
| 72 | + |
| 73 | +#### PropertyAccessorFactoryVarHandle |
| 74 | +- Available on JDK 11+, uses VarHandle for field access |
| 75 | +- Provides high-performance property access using the VarHandle API which is more efficient than traditional reflection or Unsafe-based approaches |
| 76 | + |
| 77 | +#### PropertyAccessorFactoryUnsafe |
| 78 | +- Uses Unsafe operations for field access to provide better performance compared to reflection-based access |
| 79 | +- Creates property accessors that use direct memory access via Unsafe to get and set field values, which is faster than traditional reflection |
| 80 | +- Note: Uses sun.misc.Unsafe, which is not part of the standard Java API and may not be available in all JVM implementations |
| 81 | + |
| 82 | +## Type-Specific Accessor Interfaces |
| 83 | + |
| 84 | +The package includes specialized interfaces for different data types to optimize performance: |
| 85 | + |
| 86 | +- `PropertyAccessorBooleanValue`, `PropertyAccessorByteValue`, `PropertyAccessorShortValue`, `PropertyAccessorIntValue`, `PropertyAccessorLongValue`, `PropertyAccessorFloatValue`, `PropertyAccessorDoubleValue`, `PropertyAccessorCharValue` |
| 87 | +- `PropertyAccessorObject`, `PropertyAccessorString`, `PropertyAccessorBigInteger`, `PropertyAccessorBigDecimal`, `PropertyAccessorNumber` |
| 88 | +- These interfaces provide type-specific getter and setter methods and handle conversions between compatible types |
| 89 | + |
| 90 | +## Constructor Support |
| 91 | + |
| 92 | +The factory also provides methods to create constructor-based functions: |
| 93 | +- `createSupplier(Constructor)`: Creates a Supplier that can instantiate objects using the given constructor |
| 94 | +- `createFunction(Constructor)`: Creates a Function that can instantiate objects using the given constructor |
| 95 | +- `createIntFunction(Constructor)`, `createLongFunction(Constructor)`, `createDoubleFunction(Constructor)`: Create specialized function types for different parameter types |
| 96 | + |
| 97 | +## Performance Considerations |
| 98 | + |
| 99 | +The introspect package is designed with performance in mind: |
| 100 | + |
| 101 | +1. **Type-Specific Accessors**: Different accessor implementations are created based on the field type to provide optimal performance for each specific type |
| 102 | +2. **Avoiding Boxing/Unboxing**: Specialized accessor interfaces for primitive types avoid the overhead of boxing and unboxing |
| 103 | +3. **Multiple Access Strategies**: The package provides multiple access strategies (reflection, MethodHandle, VarHandle, Unsafe) to choose the most efficient one for the runtime environment |
| 104 | +4. **LambdaMetafactory Integration**: Uses LambdaMetafactory to create efficient functional interfaces for property access |
| 105 | + |
| 106 | +## Error Handling |
| 107 | + |
| 108 | +All accessor implementations provide proper error handling with detailed exception messages. When errors occur during property access, the implementations create JSON exceptions with detailed information about the operation that failed. |
| 109 | + |
| 110 | +## Usage in Fastjson2 |
| 111 | + |
| 112 | +The introspect package is used internally by fastjson2 for efficient property access during serialization and deserialization operations. It allows fastjson2 to efficiently read and write object properties regardless of whether they are accessed through fields, getter/setter methods, or functional interfaces. |
0 commit comments