Skip to content

Commit 87e78b9

Browse files
committed
improve type reflection
1 parent ab3c51d commit 87e78b9

27 files changed

Lines changed: 201 additions & 186 deletions

src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeScanner.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import lombok.Getter;
66
import lombok.extern.slf4j.Slf4j;
77

8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
11+
import java.lang.annotation.Target;
812
import java.lang.reflect.Modifier;
913
import java.util.ArrayList;
1014
import java.util.Arrays;
@@ -42,7 +46,14 @@ public static Set<Class<? extends CustomType>> findAllCustomTypes(ClassScannerUt
4246
!item.isInterface()
4347
&& !item.isAnonymousClass()
4448
&& !Modifier.isAbstract(item.getModifiers())
49+
&& !item.isAnnotationPresent(DisableCustomTypeConfiguration.class)
4550
)
4651
.collect(Collectors.toSet());
4752
}
53+
54+
@Target({ElementType.TYPE})
55+
@Retention(RetentionPolicy.RUNTIME)
56+
public @interface DisableCustomTypeConfiguration {
57+
58+
}
4859
}

src/main/java/it/aboutbits/springboot/toolbox/jackson/CustomTypeDeserializer.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import com.fasterxml.jackson.core.JsonParser;
44
import com.fasterxml.jackson.databind.DeserializationContext;
55
import com.fasterxml.jackson.databind.JsonDeserializer;
6-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
6+
import it.aboutbits.springboot.toolbox.reflection.util.CustomTypeReflectionUtil;
77
import it.aboutbits.springboot.toolbox.type.CustomType;
88
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
99

1010
import java.io.IOException;
1111
import java.lang.reflect.Constructor;
1212
import java.lang.reflect.InvocationTargetException;
13-
import java.lang.reflect.ParameterizedType;
1413
import java.math.BigDecimal;
1514
import java.math.BigInteger;
16-
import java.util.Arrays;
1715
import java.util.function.Function;
1816

1917
public class CustomTypeDeserializer<T extends CustomType<?>> extends JsonDeserializer<T> {
@@ -22,33 +20,17 @@ public class CustomTypeDeserializer<T extends CustomType<?>> extends JsonDeseria
2220
private final Function<JsonParser, Object> typeConverter;
2321

2422
public CustomTypeDeserializer(Class<T> customType) {
25-
Constructor<T> c;
2623
this.customType = customType;
2724

28-
if (customType.isRecord()) {
29-
c = RecordReflectionUtil.getCanonicalConstructor(customType);
30-
} else {
31-
try {
32-
var customTypeInterface = Arrays.stream(customType.getGenericInterfaces())
33-
.filter(i ->
34-
i instanceof ParameterizedType
35-
&& CustomType.class.isAssignableFrom((Class<?>) ((ParameterizedType) i).getRawType())
36-
).findFirst()
37-
.map(i -> (ParameterizedType) i)
38-
.orElseThrow();
39-
40-
var parameterType = (Class<?>) customTypeInterface.getActualTypeArguments()[0];
41-
42-
c = customType.getConstructor(parameterType);
43-
} catch (Exception e) {
44-
throw new CustomTypeDeserializerException(
45-
"Unable to find constructor for type: " + customType.getName(),
46-
e
47-
);
48-
}
25+
try {
26+
this.constructor = CustomTypeReflectionUtil.getCustomTypeConstructor(customType);
27+
} catch (NoSuchMethodException e) {
28+
throw new CustomTypeDeserializerException(
29+
"Unable to find constructor for type: " + customType.getName(),
30+
e
31+
);
4932
}
5033

51-
this.constructor = c;
5234
this.typeConverter = getTypeConverter(
5335
constructor.getParameterTypes()[0]
5436
);

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedBigDecimalJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -19,18 +18,11 @@ public abstract class WrappedBigDecimalJavaType<T extends CustomType<BigDecimal>
1918
protected WrappedBigDecimalJavaType(Class<T> type) {
2019
super(type);
2120

22-
Constructor<T> c;
23-
if (type.isRecord()) {
24-
c = RecordReflectionUtil.getCanonicalConstructor(type);
25-
} else {
26-
try {
27-
c = type.getConstructor(BigDecimal.class);
28-
} catch (NoSuchMethodException e) {
29-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
30-
}
21+
try {
22+
this.constructor = type.getConstructor(BigDecimal.class);
23+
} catch (NoSuchMethodException e) {
24+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3125
}
32-
33-
this.constructor = c;
3426
}
3527

3628
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedBigIntegerJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -19,18 +18,11 @@ public abstract class WrappedBigIntegerJavaType<T extends CustomType<BigInteger>
1918
protected WrappedBigIntegerJavaType(Class<T> type) {
2019
super(type);
2120

22-
Constructor<T> c;
23-
if (type.isRecord()) {
24-
c = RecordReflectionUtil.getCanonicalConstructor(type);
25-
} else {
26-
try {
27-
c = type.getConstructor(BigInteger.class);
28-
} catch (NoSuchMethodException e) {
29-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
30-
}
21+
try {
22+
this.constructor = type.getConstructor(BigInteger.class);
23+
} catch (NoSuchMethodException e) {
24+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3125
}
32-
33-
this.constructor = c;
3426
}
3527

3628
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedDoubleJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -18,18 +17,11 @@ public abstract class WrappedDoubleJavaType<T extends CustomType<Double>> extend
1817
protected WrappedDoubleJavaType(Class<T> type) {
1918
super(type);
2019

21-
Constructor<T> c;
22-
if (type.isRecord()) {
23-
c = RecordReflectionUtil.getCanonicalConstructor(type);
24-
} else {
25-
try {
26-
c = type.getConstructor(Double.class);
27-
} catch (NoSuchMethodException e) {
28-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
29-
}
20+
try {
21+
this.constructor = type.getConstructor(Double.class);
22+
} catch (NoSuchMethodException e) {
23+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3024
}
31-
32-
this.constructor = c;
3325
}
3426

3527
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedFloatJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -18,18 +17,11 @@ public abstract class WrappedFloatJavaType<T extends CustomType<Float>> extends
1817
protected WrappedFloatJavaType(Class<T> type) {
1918
super(type);
2019

21-
Constructor<T> c;
22-
if (type.isRecord()) {
23-
c = RecordReflectionUtil.getCanonicalConstructor(type);
24-
} else {
25-
try {
26-
c = type.getConstructor(Float.class);
27-
} catch (NoSuchMethodException e) {
28-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
29-
}
20+
try {
21+
this.constructor = type.getConstructor(Float.class);
22+
} catch (NoSuchMethodException e) {
23+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3024
}
31-
32-
this.constructor = c;
3325
}
3426

3527
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedIntegerJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -18,18 +17,11 @@ public abstract class WrappedIntegerJavaType<T extends CustomType<Integer>> exte
1817
protected WrappedIntegerJavaType(Class<T> type) {
1918
super(type);
2019

21-
Constructor<T> c;
22-
if (type.isRecord()) {
23-
c = RecordReflectionUtil.getCanonicalConstructor(type);
24-
} else {
25-
try {
26-
c = type.getConstructor(Integer.class);
27-
} catch (NoSuchMethodException e) {
28-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
29-
}
20+
try {
21+
this.constructor = type.getConstructor(Integer.class);
22+
} catch (NoSuchMethodException e) {
23+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3024
}
31-
32-
this.constructor = c;
3325
}
3426

3527
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedLongJavaType.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -15,22 +14,14 @@
1514
public abstract class WrappedLongJavaType<T extends CustomType<Long>> extends AbstractClassJavaType<T> {
1615
private final transient Constructor<T> constructor;
1716

18-
@SneakyThrows
1917
protected WrappedLongJavaType(Class<T> type) {
2018
super(type);
2119

22-
Constructor<T> c;
23-
if (type.isRecord()) {
24-
c = RecordReflectionUtil.getCanonicalConstructor(type);
25-
} else {
26-
try {
27-
c = type.getConstructor(Long.class);
28-
} catch (NoSuchMethodException e) {
29-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
30-
}
20+
try {
21+
this.constructor = type.getConstructor(Long.class);
22+
} catch (NoSuchMethodException e) {
23+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3124
}
32-
33-
this.constructor = c;
3425
}
3526

3627
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedScaledBigDecimalJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
65
import lombok.SneakyThrows;
@@ -19,18 +18,11 @@ public abstract class WrappedScaledBigDecimalJavaType<T extends CustomType<Scale
1918
protected WrappedScaledBigDecimalJavaType(Class<T> type) {
2019
super(type);
2120

22-
Constructor<T> c;
23-
if (type.isRecord()) {
24-
c = RecordReflectionUtil.getCanonicalConstructor(type);
25-
} else {
26-
try {
27-
c = type.getConstructor(ScaledBigDecimal.class);
28-
} catch (NoSuchMethodException e) {
29-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
30-
}
21+
try {
22+
this.constructor = type.getConstructor(ScaledBigDecimal.class);
23+
} catch (NoSuchMethodException e) {
24+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3125
}
32-
33-
this.constructor = c;
3426
}
3527

3628
@Override

src/main/java/it/aboutbits/springboot/toolbox/persistence/javatype/base/WrappedShortJavaType.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.toolbox.persistence.javatype.base;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import lombok.SneakyThrows;
65
import org.hibernate.type.descriptor.WrapperOptions;
@@ -18,18 +17,11 @@ public abstract class WrappedShortJavaType<T extends CustomType<Short>> extends
1817
protected WrappedShortJavaType(Class<T> type) {
1918
super(type);
2019

21-
Constructor<T> c;
22-
if (type.isRecord()) {
23-
c = RecordReflectionUtil.getCanonicalConstructor(type);
24-
} else {
25-
try {
26-
c = type.getConstructor(Short.class);
27-
} catch (NoSuchMethodException e) {
28-
throw new IllegalStateException("No constructor found for " + type.getName(), e);
29-
}
20+
try {
21+
this.constructor = type.getConstructor(Short.class);
22+
} catch (NoSuchMethodException e) {
23+
throw new IllegalStateException("No constructor found for " + type.getName(), e);
3024
}
31-
32-
this.constructor = c;
3325
}
3426

3527
@Override

0 commit comments

Comments
 (0)