Package org.jdbi.v3.core.mapper.reflect
Class ConstructorMapper<T>
- java.lang.Object
-
- org.jdbi.v3.core.mapper.reflect.ConstructorMapper<T>
-
- All Implemented Interfaces:
RowMapper<T>
public final class ConstructorMapper<T> extends java.lang.Object implements RowMapper<T>
A row mapper which maps the fields in a result set into a constructor. The default implementation will perform a case insensitive mapping between the constructor parameter names and the column labels, also considering camel-case to underscores conversion.This mapper respects
Nestedannotations on constructor parameters.Constructor parameters annotated as
@Nullablemay be omitted from the result set without error. Any annotation named "Nullable" can be used, no matter which package it is from.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classConstructorMapper.BoundConstructorMapperprivate static classConstructorMapper.ParameterData(package private) static classConstructorMapper.UnmatchedConstructorMapper<T>
-
Field Summary
Fields Modifier and Type Field Description private java.beans.ConstructorPropertiesconstructorPropertiesprivate static java.lang.StringDEFAULT_PREFIXprivate InstanceFactory<T>factoryprivate java.util.Map<java.lang.reflect.Parameter,ConstructorMapper<?>>nestedMappersprivate java.lang.Stringprefixprivate static java.lang.StringUNMATCHED_CONSTRUCTOR_PARAMETERprivate static java.lang.StringUNMATCHED_CONSTRUCTOR_PARAMETERS
-
Constructor Summary
Constructors Modifier Constructor Description privateConstructorMapper(InstanceFactory<T> factory, java.lang.String prefix)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.Optional<RowMapper<T>>createSpecializedRowMapper(StatementContext ctx, java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers, java.util.List<java.lang.String> unmatchedColumns)private java.lang.StringdebugName(java.lang.reflect.Parameter parameter)static RowMapperFactoryfactory(java.lang.Class<?> clazz)Use the only declared constructor to map a class.static RowMapperFactoryfactory(java.lang.Class<?> clazz, java.lang.String prefix)Use the only declared constructor to map a class.static RowMapperFactoryfactory(java.lang.reflect.Constructor<?> constructor)Use aConstructor<T>to map its declaring type.static RowMapperFactoryfactory(java.lang.reflect.Constructor<?> constructor, java.lang.String prefix)Use aConstructor<T>to map its declaring type.private booleanisNullable(java.lang.reflect.Parameter parameter)private java.util.OptionalIntlocatePropagateNullColumnIndex(java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers)Tmap(java.sql.ResultSet rs, StatementContext ctx)Map the current row of the result set.static <T> RowMapper<T>of(java.lang.Class<T> type)Return a ConstructorMapper for the given type.static <T> RowMapper<T>of(java.lang.Class<T> type, java.lang.String prefix)Return a ConstructorMapper for the given type and prefix.static <T> RowMapper<T>of(java.lang.reflect.Constructor<T> constructor)Return a ConstructorMapper using the given constructorstatic <T> RowMapper<T>of(java.lang.reflect.Constructor<T> constructor, java.lang.String prefix)Instantiate a ConstructorMapper using the given constructor and prefixprivate static java.lang.StringparamName(java.lang.reflect.Parameter[] parameters, int position, java.beans.ConstructorProperties parameterNames)RowMapper<T>specialize(java.sql.ResultSet rs, StatementContext ctx)Returns a specialized row mapper, optimized for the given result set.
-
-
-
Field Detail
-
DEFAULT_PREFIX
private static final java.lang.String DEFAULT_PREFIX
- See Also:
- Constant Field Values
-
UNMATCHED_CONSTRUCTOR_PARAMETERS
private static final java.lang.String UNMATCHED_CONSTRUCTOR_PARAMETERS
- See Also:
- Constant Field Values
-
UNMATCHED_CONSTRUCTOR_PARAMETER
private static final java.lang.String UNMATCHED_CONSTRUCTOR_PARAMETER
- See Also:
- Constant Field Values
-
factory
private final InstanceFactory<T> factory
-
prefix
private final java.lang.String prefix
-
constructorProperties
private final java.beans.ConstructorProperties constructorProperties
-
nestedMappers
private final java.util.Map<java.lang.reflect.Parameter,ConstructorMapper<?>> nestedMappers
-
-
Constructor Detail
-
ConstructorMapper
private ConstructorMapper(InstanceFactory<T> factory, java.lang.String prefix)
-
-
Method Detail
-
factory
public static RowMapperFactory factory(java.lang.Class<?> clazz)
Use the only declared constructor to map a class.- Parameters:
clazz- the class to find a constructor of- Returns:
- the factory
-
factory
public static RowMapperFactory factory(java.lang.Class<?> clazz, java.lang.String prefix)
Use the only declared constructor to map a class.- Parameters:
clazz- the class to find a constructor ofprefix- a prefix for the parameter names- Returns:
- the factory
-
factory
public static RowMapperFactory factory(java.lang.reflect.Constructor<?> constructor)
Use aConstructor<T>to map its declaring type.- Parameters:
constructor- the constructor to invoke- Returns:
- the factory
-
factory
public static RowMapperFactory factory(java.lang.reflect.Constructor<?> constructor, java.lang.String prefix)
Use aConstructor<T>to map its declaring type.- Parameters:
constructor- the constructor to invokeprefix- a prefix to the constructor parameter names- Returns:
- the factory
-
of
public static <T> RowMapper<T> of(java.lang.Class<T> type)
Return a ConstructorMapper for the given type.- Type Parameters:
T- the type to map- Parameters:
type- the mapped type- Returns:
- the mapper
-
of
public static <T> RowMapper<T> of(java.lang.Class<T> type, java.lang.String prefix)
Return a ConstructorMapper for the given type and prefix.- Type Parameters:
T- the type to map- Parameters:
type- the mapped typeprefix- the column name prefix- Returns:
- the mapper
-
of
public static <T> RowMapper<T> of(java.lang.reflect.Constructor<T> constructor)
Return a ConstructorMapper using the given constructor- Type Parameters:
T- the type to map- Parameters:
constructor- the constructor to be used in mapping- Returns:
- the mapper
-
of
public static <T> RowMapper<T> of(java.lang.reflect.Constructor<T> constructor, java.lang.String prefix)
Instantiate a ConstructorMapper using the given constructor and prefix- Type Parameters:
T- the type to map- Parameters:
constructor- the constructor to be used in mappingprefix- the column name prefix- Returns:
- the mapper
-
map
public T map(java.sql.ResultSet rs, StatementContext ctx) throws java.sql.SQLException
Description copied from interface:RowMapperMap the current row of the result set. This method should not cause the result set to advance; allow Jdbi to do that, please.
-
specialize
public RowMapper<T> specialize(java.sql.ResultSet rs, StatementContext ctx) throws java.sql.SQLException
Description copied from interface:RowMapperReturns a specialized row mapper, optimized for the given result set.Before mapping the result set from a SQL statement; Jdbi will first call this method to obtain a specialized instance. The returned mapper will then be used to map the result set rows, and discarded.
Implementing this method is optional; the default implementation returns
this. Implementors might choose to override this method to improve performance, e.g. by matching up column names to properties once for the entire result set, rather than repeating the process for every row.- Specified by:
specializein interfaceRowMapper<T>- Parameters:
rs- the result set to specialize overctx- the statement context to specialize over- Returns:
- a row mapper equivalent to this one, possibly specialized.
- Throws:
java.sql.SQLException- if anything goes wrong go ahead and let this percolate; Jdbi will handle it- See Also:
for an example of specialization.
-
createSpecializedRowMapper
private java.util.Optional<RowMapper<T>> createSpecializedRowMapper(StatementContext ctx, java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers, java.util.List<java.lang.String> unmatchedColumns)
-
locatePropagateNullColumnIndex
private java.util.OptionalInt locatePropagateNullColumnIndex(java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers)
-
isNullable
private boolean isNullable(java.lang.reflect.Parameter parameter)
-
paramName
private static java.lang.String paramName(java.lang.reflect.Parameter[] parameters, int position, java.beans.ConstructorProperties parameterNames)
-
debugName
private java.lang.String debugName(java.lang.reflect.Parameter parameter)
-
-