/
ConstructorInstanceFactory.java
106 lines (94 loc) · 4.13 KB
/
ConstructorInstanceFactory.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
/*
* 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.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.jdbi.v3.core.internal.exceptions.Unchecked;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toUnmodifiableList;
class ConstructorInstanceFactory<T> extends InstanceFactory<T> {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private final Constructor<T> constructor;
private final List<Type> types;
private final MethodHandle constructorHandle;
ConstructorInstanceFactory(Constructor<T> constructor) {
super(constructor);
this.constructor = requireNonNull(constructor, "constructor is null");
this.types = extractTypes(constructor, super::getTypes);
this.constructorHandle = getConstructorMethodHandle(constructor);
}
@Override
List<Type> getTypes() {
return types;
}
@Override
T newInstance(Object... params) {
return (T) Unchecked.<Object[], Object>function(constructorHandle::invokeWithArguments).apply(params);
}
@Override
public String toString() {
return constructor.toString();
}
// Note: this can be removed once https://bugs.openjdk.org/browse/JDK-8320575 is resolved
private static <T> boolean isGenericInformationLost(Constructor<T> factory) {
// If there is more than one constructor, ensure that the factory constructor is the canonical constructor.
// If so it will need to get the type parameters from the declared fields.
if (factory.getParameters().length != getFields(factory)
.count()) {
return false;
}
boolean lossDetected = false;
for (int i = 0; i < factory.getParameters().length; i++) {
Parameter parameter = factory.getParameters()[i];
Field field = factory.getDeclaringClass().getDeclaredFields()[i];
if (!parameter.getName().equals(field.getName()) || !parameter.getType().equals(field.getType())) {
return false;
}
// Check if the generic type information is lost, due to jdk21 record constructor: https://bugs.openjdk.org/browse/JDK-8320575
if (parameter.getType().getTypeParameters().length > 0
&& !parameter.getParameterizedType().equals(field.getGenericType())) {
lossDetected = true;
}
}
return lossDetected;
}
private static <T> List<Type> extractTypes(Constructor<T> constructor, Supplier<List<Type>> defaultSupplier) {
if (isGenericInformationLost(constructor)) {
return getFields(constructor)
.map(Field::getGenericType)
.collect(toUnmodifiableList());
}
return defaultSupplier.get();
}
private static <T> MethodHandle getConstructorMethodHandle(Constructor<T> constructor) {
try {
return LOOKUP.unreflectConstructor(constructor).asFixedArity();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static <T> Stream<Field> getFields(Constructor<T> constructor) {
return Arrays.stream(constructor.getDeclaringClass().getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers()));
}
}