Skip to content

Commit 6777f80

Browse files
committed
fix reflection for inheritance
1 parent 60af3f2 commit 6777f80

4 files changed

Lines changed: 61 additions & 10 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/reflection/util/CustomTypeReflectionUtil.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ public static <T extends CustomType<?>> Constructor<T> getCustomTypeConstructor(
2020
}
2121
}
2222

23-
public static Class<?> getWrappedType(Class<? extends CustomType<?>> customType) {
24-
var customTypeInterface = Arrays.stream(customType.getGenericInterfaces())
25-
.filter(i ->
26-
i instanceof ParameterizedType
27-
&& CustomType.class.isAssignableFrom((Class<?>) ((ParameterizedType) i).getRawType())
28-
).findFirst()
29-
.map(i -> (ParameterizedType) i)
30-
.orElseThrow();
31-
32-
return (Class<?>) customTypeInterface.getActualTypeArguments()[0];
23+
public static Class<?> getWrappedType(Class<? extends CustomType<?>> customType) throws NoSuchMethodException {
24+
Class<?> currentClass = customType;
25+
while (currentClass != null) {
26+
// Check interfaces of the current class
27+
var customTypeInterface = Arrays.stream(currentClass.getGenericInterfaces())
28+
.filter(i ->
29+
i instanceof ParameterizedType
30+
&& CustomType.class.isAssignableFrom((Class<?>) ((ParameterizedType) i).getRawType())
31+
).findFirst()
32+
.map(i -> (ParameterizedType) i);
33+
34+
if (customTypeInterface.isPresent()) {
35+
return (Class<?>) customTypeInterface.get().getActualTypeArguments()[0];
36+
}
37+
38+
// Move to the parent class
39+
currentClass = currentClass.getSuperclass();
40+
}
41+
42+
throw new NoSuchMethodException();
3343
}
3444
}

src/main/java/it/aboutbits/springboot/toolbox/swagger/type/CustomTypeModelConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import it.aboutbits.springboot.toolbox.type.CustomType;
1010
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
1111
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
12+
import lombok.SneakyThrows;
1213

1314
import java.math.BigDecimal;
1415
import java.math.BigInteger;
@@ -17,6 +18,7 @@
1718
public class CustomTypeModelConverter implements ModelConverter {
1819

1920
@Override
21+
@SneakyThrows(NoSuchMethodException.class)
2022
public Schema<?> resolve(
2123
AnnotatedType annotatedType,
2224
ModelConverterContext context,

src/main/java/it/aboutbits/springboot/toolbox/swagger/type/CustomTypePropertyCustomizer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import it.aboutbits.springboot.toolbox.type.CustomType;
99
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
1010
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
11+
import lombok.SneakyThrows;
1112
import lombok.extern.slf4j.Slf4j;
1213
import org.springdoc.core.customizers.PropertyCustomizer;
1314

@@ -17,6 +18,7 @@
1718
@Slf4j
1819
public class CustomTypePropertyCustomizer implements PropertyCustomizer {
1920
@Override
21+
@SneakyThrows(NoSuchMethodException.class)
2022
public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
2123
var type = annotatedType.getType();
2224

src/test/java/it/aboutbits/springboot/toolbox/reflection/util/CustomTypeReflectionUtilTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ void testGetCustomTypeConstructorWithMismatchingConstructor() {
4444
);
4545
}
4646

47+
@Test
48+
void testGetCustomTypeConstructorWithInheritedCustomType() throws Exception {
49+
// given
50+
var customTypeClass = ChildClass.class;
51+
52+
// when
53+
var constructor = CustomTypeReflectionUtil.getCustomTypeConstructor(customTypeClass);
54+
55+
// then
56+
assertThat(constructor).isNotNull();
57+
assertThat(constructor.getParameterCount()).isEqualTo(1);
58+
assertThat(constructor.getParameterTypes()[0]).isEqualTo(Integer.class);
59+
}
60+
4761
@SuppressWarnings("checkstyle:RedundantModifier")
4862
@CustomTypeScanner.DisableCustomTypeConfiguration
4963
public static class ValidCustomType implements CustomType<String> {
@@ -88,4 +102,27 @@ public String value() {
88102
return value;
89103
}
90104
}
105+
106+
@SuppressWarnings("checkstyle:RedundantModifier")
107+
@CustomTypeScanner.DisableCustomTypeConfiguration
108+
public static class ParentClass implements CustomType<Integer> {
109+
private final Integer value;
110+
111+
public ParentClass(Integer value) {
112+
this.value = value;
113+
}
114+
115+
@Override
116+
public Integer value() {
117+
return value;
118+
}
119+
}
120+
121+
@SuppressWarnings("checkstyle:RedundantModifier")
122+
@CustomTypeScanner.DisableCustomTypeConfiguration
123+
public static class ChildClass extends ParentClass {
124+
public ChildClass(Integer value) {
125+
super(value);
126+
}
127+
}
91128
}

0 commit comments

Comments
 (0)