Class DtoConstructor

java.lang.Object
org.litebridge.orm.persistence.DtoConstructor

public final class DtoConstructor extends Object
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.

  • Constructor Details

    • DtoConstructor

      public DtoConstructor(TableRegistry tableRegistry)
      Creates a new DTO constructor instance.
      Parameters:
      tableRegistry - the table registry to use for dependency resolution
  • Method Details

    • 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 of FieldAccessorValue objects, 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.