/
ConstructorMapper.java
399 lines (342 loc) · 16.3 KB
/
ConstructorMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.core.mapper.reflect;
import java.beans.ConstructorProperties;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import org.jdbi.v3.core.mapper.Nested;
import org.jdbi.v3.core.mapper.PropagateNull;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.mapper.RowMapperFactory;
import org.jdbi.v3.core.mapper.SingleColumnMapper;
import org.jdbi.v3.core.mapper.reflect.internal.NullDelegatingMapper;
import org.jdbi.v3.core.qualifier.QualifiedType;
import org.jdbi.v3.core.qualifier.Qualifiers;
import org.jdbi.v3.core.statement.StatementContext;
import static java.lang.String.format;
import static org.jdbi.v3.core.mapper.reflect.JdbiConstructors.findFactoryFor;
import static org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.addPropertyNamePrefix;
import static org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.anyColumnsStartWithPrefix;
import static org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex;
import static org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames;
/**
* 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.
* <p>
* This mapper respects {@link Nested} annotations on constructor parameters.
* <p>
* Constructor parameters annotated as {@code @Nullable} may be omitted from the result set without error. Any annotation named "Nullable" can be used, no
* matter which package it is from.
*/
public final class ConstructorMapper<T> implements RowMapper<T> {
private static final String DEFAULT_PREFIX = "";
@SuppressWarnings("InlineFormatString")
private static final String UNMATCHED_CONSTRUCTOR_PARAMETERS =
"Instance factory '%s' could not match any parameter to any columns in the result set. "
+ "Verify that the Java compiler is configured to emit parameter names, "
+ "that your result set has the columns expected, annotate the "
+ "parameter names explicitly with @ColumnName, or annotate nullable parameters as @Nullable";
@SuppressWarnings("InlineFormatString")
private static final String UNMATCHED_CONSTRUCTOR_PARAMETER =
"Instance factory '%s' parameter '%s' has no matching columns in the result set. "
+ "Verify that the Java compiler is configured to emit parameter names, "
+ "that your result set has the columns expected, annotate the "
+ "parameter names explicitly with @ColumnName, or annotate nullable parameters as @Nullable";
/**
* Use the only declared constructor to map a class.
*
* @param clazz the class to find a constructor of
* @return the factory
*/
public static RowMapperFactory factory(Class<?> clazz) {
return RowMapperFactory.of(clazz, ConstructorMapper.of(clazz));
}
/**
* Use the only declared constructor to map a class.
*
* @param clazz the class to find a constructor of
* @param prefix a prefix for the parameter names
* @return the factory
*/
public static RowMapperFactory factory(Class<?> clazz, String prefix) {
return RowMapperFactory.of(clazz, ConstructorMapper.of(clazz, prefix));
}
/**
* Use a {@code Constructor<T>} to map its declaring type.
*
* @param constructor the constructor to invoke
* @return the factory
*/
public static RowMapperFactory factory(Constructor<?> constructor) {
return RowMapperFactory.of(constructor.getDeclaringClass(), ConstructorMapper.of(constructor));
}
/**
* Use a {@code Constructor<T>} to map its declaring type.
*
* @param constructor the constructor to invoke
* @param prefix a prefix to the constructor parameter names
* @return the factory
*/
public static RowMapperFactory factory(Constructor<?> constructor, String prefix) {
return RowMapperFactory.of(constructor.getDeclaringClass(), ConstructorMapper.of(constructor, prefix));
}
/**
* Return a ConstructorMapper for the given type.
*
* @param <T> the type to map
* @param type the mapped type
* @return the mapper
*/
public static <T> RowMapper<T> of(Class<T> type) {
return ConstructorMapper.of(type, DEFAULT_PREFIX);
}
/**
* Return a ConstructorMapper for the given type and prefix.
*
* @param <T> the type to map
* @param type the mapped type
* @param prefix the column name prefix
* @return the mapper
*/
public static <T> RowMapper<T> of(Class<T> type, String prefix) {
return new ConstructorMapper<>(findFactoryFor(type), prefix);
}
/**
* Return a ConstructorMapper using the given constructor
*
* @param <T> the type to map
* @param constructor the constructor to be used in mapping
* @return the mapper
*/
public static <T> RowMapper<T> of(Constructor<T> constructor) {
return ConstructorMapper.of(constructor, DEFAULT_PREFIX);
}
/**
* Instantiate a ConstructorMapper using the given constructor and prefix
*
* @param <T> the type to map
* @param constructor the constructor to be used in mapping
* @param prefix the column name prefix
* @return the mapper
*/
public static <T> RowMapper<T> of(Constructor<T> constructor, String prefix) {
return new ConstructorMapper<>(new ConstructorInstanceFactory<>(constructor), prefix);
}
private final InstanceFactory<T> factory;
private final String prefix;
private final ConstructorProperties constructorProperties;
private final Map<Parameter, ConstructorMapper<?>> nestedMappers = new ConcurrentHashMap<>();
private ConstructorMapper(InstanceFactory<T> factory, String prefix) {
this.factory = factory;
this.prefix = prefix;
this.constructorProperties = factory.getAnnotation(ConstructorProperties.class);
}
@Override
public T map(ResultSet rs, StatementContext ctx) throws SQLException {
return specialize(rs, ctx).map(rs, ctx);
}
@Override
public RowMapper<T> specialize(ResultSet rs, StatementContext ctx) throws SQLException {
final UnaryOperator<String> caseChange = ctx.getConfig(ReflectionMappers.class).getCaseChange();
final List<String> columnNames = getColumnNames(rs, caseChange);
final List<ColumnNameMatcher> columnNameMatchers =
ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers();
final List<String> unmatchedColumns = new ArrayList<>(columnNames);
RowMapper<T> mapper = createSpecializedRowMapper(ctx, columnNames, columnNameMatchers, unmatchedColumns)
.orElseGet(() -> new UnmatchedConstructorMapper<>(format(
UNMATCHED_CONSTRUCTOR_PARAMETERS, factory)));
if (ctx.getConfig(ReflectionMappers.class).isStrictMatching()
&& anyColumnsStartWithPrefix(unmatchedColumns, prefix, columnNameMatchers)) {
return new UnmatchedConstructorMapper<>(
format("Mapping instance factory %s could not match parameters for columns: %s", factory, unmatchedColumns));
}
return mapper;
}
private Optional<RowMapper<T>> createSpecializedRowMapper(StatementContext ctx,
List<String> columnNames,
List<ColumnNameMatcher> columnNameMatchers,
List<String> unmatchedColumns) {
final int count = factory.getParameterCount();
final Parameter[] parameters = factory.getParameters();
final List<Type> types = factory.getTypes();
boolean matchedColumns = false;
final List<String> unmatchedParameters = new ArrayList<>();
final List<ParameterData> paramData = new ArrayList<>();
for (int i = 0; i < count; i++) {
final Parameter parameter = parameters[i];
final Type parameterType = types.get(i);
boolean nullable = isNullable(parameter);
Nested nested = parameter.getAnnotation(Nested.class);
if (nested == null) {
final String paramName = addPropertyNamePrefix(prefix, paramName(parameters, i, constructorProperties));
final OptionalInt columnIndex = findColumnIndex(paramName, columnNames, columnNameMatchers,
() -> debugName(parameter));
if (columnIndex.isPresent()) {
int colIndex = columnIndex.getAsInt();
final QualifiedType<?> type = QualifiedType.of(parameterType)
.withAnnotations(ctx.getConfig(Qualifiers.class).findFor(parameter));
paramData.add(new ParameterData(i, parameter, ctx.findColumnMapperFor(type)
.map(mapper -> new SingleColumnMapper<>(mapper, colIndex + 1))
.orElseThrow(() -> new IllegalArgumentException(
format("Could not find column mapper for type '%s' of parameter '%s' for instance factory '%s'", type, paramName, factory)))));
matchedColumns = true;
unmatchedColumns.remove(columnNames.get(colIndex));
} else if (nullable) {
paramData.add(new ParameterData(i, parameter, null));
} else {
unmatchedParameters.add(paramName);
}
} else {
final String nestedPrefix = addPropertyNamePrefix(prefix, nested.value());
final Optional<? extends RowMapper<?>> nestedMapper = nestedMappers
.computeIfAbsent(parameter, p ->
new ConstructorMapper<>(findFactoryFor(p.getType()), nestedPrefix))
.createSpecializedRowMapper(ctx, columnNames, columnNameMatchers, unmatchedColumns);
if (nestedMapper.isPresent()) {
paramData.add(new ParameterData(i, parameter, nestedMapper.get()));
matchedColumns = true;
} else if (nullable) {
paramData.add(new ParameterData(i, parameter, null));
} else {
unmatchedParameters.add(paramName(parameters, i, constructorProperties));
}
}
}
if (!matchedColumns) {
return Optional.empty();
}
paramData.sort(Comparator.comparing(
p -> p.propagateNull ? 1 : 0));
if (!unmatchedParameters.isEmpty()) {
throw new IllegalArgumentException(format(
UNMATCHED_CONSTRUCTOR_PARAMETER, factory, unmatchedParameters));
}
RowMapper<T> boundMapper = new BoundConstructorMapper(paramData);
OptionalInt propagateNullColumnIndex = locatePropagateNullColumnIndex(columnNames, columnNameMatchers);
if (propagateNullColumnIndex.isPresent()) {
return Optional.of(new NullDelegatingMapper<>(propagateNullColumnIndex.getAsInt() + 1, boundMapper));
} else {
return Optional.of(boundMapper);
}
}
private OptionalInt locatePropagateNullColumnIndex(List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers) {
Optional<String> propagateNullColumn =
Optional.ofNullable(factory.getAnnotationIncludingType(PropagateNull.class))
.map(PropagateNull::value)
.map(name -> addPropertyNamePrefix(prefix, name));
if (!propagateNullColumn.isPresent()) {
return OptionalInt.empty();
}
return findColumnIndex(propagateNullColumn.get(), columnNames, columnNameMatchers, propagateNullColumn::get);
}
private boolean isNullable(Parameter parameter) {
// Any annotation named @Nullable is honored. We're nice that way.
return Stream.of(parameter.getAnnotations())
.map(Annotation::annotationType)
.map(Class::getSimpleName)
.anyMatch("Nullable"::equals);
}
private static String paramName(Parameter[] parameters,
int position,
ConstructorProperties parameterNames) {
final Parameter parameter = parameters[position];
return Optional.ofNullable(parameter.getAnnotation(ColumnName.class))
.map(ColumnName::value)
.orElseGet(() -> {
if (parameterNames != null) {
return parameterNames.value()[position];
} else {
return parameter.getName();
}
});
}
private String debugName(Parameter parameter) {
return format("%s constructor parameter %s",
factory.getDeclaringClass().getSimpleName(),
parameter.getName());
}
private static class ParameterData {
ParameterData(int index, Parameter parameter, RowMapper<?> mapper) {
this.index = index;
this.parameter = parameter;
this.mapper = mapper;
propagateNull = checkPropagateNullAnnotation(parameter);
isPrimitive = parameter.getType().isPrimitive();
}
private static boolean checkPropagateNullAnnotation(Parameter parameter) {
final Optional<String> propagateNullValue = Optional.ofNullable(parameter.getAnnotation(PropagateNull.class)).map(PropagateNull::value);
propagateNullValue.ifPresent(v -> {
if (!v.isEmpty()) {
throw new IllegalArgumentException(format("@PropagateNull does not support a value (%s) on a parameter (%s)", v, parameter.getName()));
}
});
return propagateNullValue.isPresent();
}
final int index;
final Parameter parameter;
final RowMapper<?> mapper;
final boolean propagateNull;
final boolean isPrimitive;
}
static class UnmatchedConstructorMapper<T> implements RowMapper<T> {
private final String message;
UnmatchedConstructorMapper(String message) {
this.message = message;
}
@Override
public T map(ResultSet rs, StatementContext ctx) throws SQLException {
throw new IllegalArgumentException(message);
}
}
class BoundConstructorMapper implements RowMapper<T> {
private final List<ParameterData> paramData;
private final int count;
BoundConstructorMapper(List<ParameterData> paramData) {
this.paramData = paramData;
this.count = factory.getParameterCount();
}
@Override
public T map(ResultSet rs, StatementContext ctx) throws SQLException {
final Object[] params = new Object[count];
for (ParameterData p : paramData) {
params[p.index] = p.mapper == null ? null : p.mapper.map(rs, ctx);
if (p.propagateNull && (params[p.index] == null || (p.isPrimitive && rs.wasNull()))) {
return null;
}
}
return factory.newInstance(params);
}
@Override
public String toString() {
return new StringJoiner(", ", BoundConstructorMapper.class.getSimpleName() + "[", "]")
.add("type=" + factory.getDeclaringClass().getSimpleName())
.add("prefix=" + prefix)
.toString();
}
}
}