java.lang.Object
org.litebridge.orm.persistence.DtoConstructor
A utility class responsible for constructing Data Transfer Object (DTO) instances
using default or canonical constructors. This class leverages cached constructor data
for performance optimization and ensures proper mapping of field values to parameters.
It supports both Java POJOs and Java Records as DTOs.
The class heavily relies on a TableRegistry instance to retrieve metadata
about the DTOs being constructed, and it computes or caches constructor and field
accessor mappings as needed.
The caching mechanism ensures that constructors and their associated parameters are resolved only once for each DTO class, significantly improving performance for repeated instantiations.
Thread-safety is ensured via the use of ConcurrentHashMap for storing
cached constructor and field accessor data.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordThe result of constructing a Data Transfer Object (DTO).static final recordDependency configuration for a Data Transfer Object (DTO).static final recordA pair consisting of aFieldAccessorand its corresponding value.static final recordInformation about a particular constructor. -
Constructor Summary
ConstructorsConstructorDescriptionDtoConstructor(TableRegistry tableRegistry, MethodHandles.Lookup lookup) Creates a new DTO constructor instance. -
Method Summary
Modifier and TypeMethodDescriptiongetMappingInfo(Class<?> dtoClass, @Nullable Class<?> contextDtoClass) Resolves and returns the mapping information for instantiating the specified DTO class.getMappingInfo(OrmTable ormTable) Resolves and returns the mapping information for instantiating the DTO class corresponding to the specified ORM table.<DTO> DtoConstructor.ConstructionResult<DTO> newInstance(Class<DTO> dtoClass, List<DtoConstructor.FieldAccessorValue> fieldAccessorValues) Creates a new instance of the specified DTO class using the provided field accessor values to resolve constructor arguments.
-
Constructor Details
-
DtoConstructor
Creates a new DTO constructor instance.- Parameters:
tableRegistry- the table registry to use for dependency resolution
-
-
Method Details
-
getMappingInfo
Resolves and returns the mapping information for instantiating the DTO class corresponding to the specified ORM table.- Parameters:
ormTable- the ORM table to inspect- Returns:
- the resolved
DtoConstructor.MappingInfo
-
getMappingInfo
public DtoConstructor.MappingInfo getMappingInfo(Class<?> dtoClass, @Nullable Class<?> contextDtoClass) Resolves and returns the mapping information for instantiating the specified DTO class.- Parameters:
dtoClass- the DTO class to inspectcontextDtoClass- the context DTO class, ornull- Returns:
- the resolved
DtoConstructor.MappingInfo
-
newInstance
public <DTO> DtoConstructor.ConstructionResult<DTO> newInstance(Class<DTO> dtoClass, List<DtoConstructor.FieldAccessorValue> fieldAccessorValues) Creates a new instance of the specified DTO class using the provided field accessor values to resolve constructor arguments.- Type Parameters:
DTO- The type of the Data Transfer Object to instantiate.- Parameters:
dtoClass- The class of the DTO to be instantiated.fieldAccessorValues- A list ofFieldAccessorValueobjects, where each object specifies a field accessor and its corresponding value for the DTO.- Returns:
- A
ConstructionResult<DTO>containing the instantiated DTO object and a boolean indicating whether the default constructor was used. - Throws:
IllegalArgumentException- If no suitable constructor is found for the given DTO class.
-