Class PreferredConstructorDiscoverer<T>

java.lang.Object
in.co.akshitbansal.springwebquery.tupleconverter.PreferredConstructorDiscoverer<T>
Type Parameters:
T - target DTO type
Direct Known Subclasses:
CachedPreferredConstructorDiscoverer

public class PreferredConstructorDiscoverer<T> extends Object
Locates a constructor that can materialize a DTO from a JPA Tuple.

Constructor discovery is based entirely on the tuple's positional shape. Parameter names, tuple aliases, and property names are not consulted.

A constructor is considered suitable when all of the following are true:

  • it is not synthetic
  • its parameter count exactly matches the tuple element count
  • for each position, the constructor parameter type is assignable from the tuple element Java type after primitive types are boxed

The implementation iterates over Class.getDeclaredConstructors(), keeps track of the latest suitable constructor it has seen, and stops early if it encounters a suitable constructor annotated with PersistenceCreator. Because Java reflection does not guarantee a stable ordering for declared constructors, constructor selection is unpredictable when multiple suitable constructors are present.

To keep selection deterministic, DTOs should ideally expose only one suitable constructor. If PersistenceCreator is used, it is strongly recommended that at most one suitable constructor be annotated with it.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final @NonNull Class<T>
    DTO type whose constructors are inspected.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    discover(@NonNull jakarta.persistence.Tuple tuple)
    Finds a constructor whose parameter list is compatible with the supplied tuple and makes it accessible for later invocation.
    Returns the DTO type whose constructors this discoverer inspects.
    protected Class<?>
    wrap(Class<?> clazz)
    Converts primitive types to their boxed equivalents before assignability checks are performed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • clazz

      @NonNull protected final @NonNull Class<T> clazz
      DTO type whose constructors are inspected.
  • Constructor Details

    • PreferredConstructorDiscoverer

      public PreferredConstructorDiscoverer()
  • Method Details

    • getTargetClass

      public Class<T> getTargetClass()
      Returns the DTO type whose constructors this discoverer inspects.
      Returns:
      target DTO type
    • discover

      public Constructor<T> discover(@NonNull @NonNull jakarta.persistence.Tuple tuple)
      Finds a constructor whose parameter list is compatible with the supplied tuple and makes it accessible for later invocation.

      Matching is performed position by position. For each tuple element, the constructor parameter at the same index must be assignable from the tuple element Java type, with primitive parameter types first converted to their boxed equivalents. Tuple aliases are ignored.

      If several suitable constructors exist, the outcome depends on the order returned by Class.getDeclaredConstructors(). A suitable constructor annotated with PersistenceCreator wins immediately when encountered. Otherwise, the last suitable constructor encountered is selected.

      Parameters:
      tuple - tuple whose values will be passed to the constructor
      Returns:
      matching constructor made accessible for invocation
      Throws:
      QueryConfigurationException - if no suitable constructor can be found for the tuple shape
    • wrap

      protected Class<?> wrap(Class<?> clazz)
      Converts primitive types to their boxed equivalents before assignability checks are performed.