Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 6da8b65

Browse files
committed
refactoring ReflectionHelper
1 parent 815ef3c commit 6da8b65

3 files changed

Lines changed: 59 additions & 44 deletions

File tree

dataformat-core/src/main/java/io/adminshell/aas/v3/dataformat/core/ReflectionHelper.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package io.adminshell.aas.v3.dataformat.core;
1717

1818
import io.adminshell.aas.v3.model.Constraint;
19-
import io.adminshell.aas.v3.model.DataSpecificationContent;
2019
import io.adminshell.aas.v3.model.Referable;
2120
import io.github.classgraph.ClassGraph;
2221
import io.github.classgraph.ClassInfo;
@@ -162,6 +161,62 @@ public static boolean isModelInterfaceOrDefaultImplementation(Class<?> type) {
162161
return isModelInterface(type) || isDefaultImplementation(type);
163162
}
164163

164+
/**
165+
* Returns the AAS type information used for de-/serialization for a given
166+
* class or null if type information should not be included
167+
*
168+
* @param clazz the class to find the type information for
169+
* @return the type information for the given class or null if there is no
170+
* type information or type information should not be included
171+
*/
172+
public static String getModelType(Class<?> clazz) {
173+
Class<?> type = getMostSpecificTypeWithModelType(clazz);
174+
if (type != null) {
175+
return type.getSimpleName();
176+
}
177+
for (Class<?> interfaceClass : clazz.getInterfaces()) {
178+
String result = getModelType(interfaceClass);
179+
if (result != null) {
180+
return result;
181+
}
182+
}
183+
Class<?> superClass = clazz.getSuperclass();
184+
if (superClass != null) {
185+
return getModelType(superClass);
186+
}
187+
return null;
188+
}
189+
190+
/**
191+
* Returns the most specific supertype that contains some AAS type
192+
* information or null if there is none
193+
*
194+
* @param clazz the class to find the type for
195+
* @return the most specific supertype of given class that contains some AAS
196+
* type information or null if there is none
197+
*/
198+
public static Class<?> getMostSpecificTypeWithModelType(Class<?> clazz) {
199+
return TYPES_WITH_MODEL_TYPE.stream()
200+
.filter(x -> clazz.isInterface() ? x.equals(clazz) : x.isAssignableFrom(clazz))
201+
.sorted((Class<?> o1, Class<?> o2) -> {
202+
// -1: o1 more special than o2
203+
// 0: o1 equals o2 or on same samelevel
204+
// 1: o2 more special than o1
205+
if (o1.isAssignableFrom(o2)) {
206+
if (o2.isAssignableFrom(o1)) {
207+
return 0;
208+
}
209+
return 1;
210+
}
211+
if (o2.isAssignableFrom(o1)) {
212+
return -1;
213+
}
214+
return 0;
215+
})
216+
.findFirst()
217+
.orElse(null);
218+
}
219+
165220
static {
166221
ScanResult modelScan = new ClassGraph()
167222
.enableClassInfo()

dataformat-json/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>io.admin-shell.aas</groupId>
2121
<artifactId>dataformat-core</artifactId>
22-
<version>${model.version}</version>
22+
<version>${revision}</version>
2323
<scope>compile</scope>
2424
</dependency>
2525
<dependency>

dataformat-json/src/main/java/io/adminshell/aas/v3/dataformat/json/ReflectionAnnotationIntrospector.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,55 +60,15 @@ public class ReflectionAnnotationIntrospector extends JacksonAnnotationIntrospec
6060

6161
@Override
6262
public String findTypeName(AnnotatedClass ac) {
63-
String customType = findModelType(ac.getRawType());
63+
String customType = ReflectionHelper.getModelType(ac.getRawType());
6464
return customType != null
6565
? customType
6666
: super.findTypeName(ac);
6767
}
6868

69-
private Class<?> getMostSpecificTypeWithModelType(Class<?> clazz) {
70-
return ReflectionHelper.TYPES_WITH_MODEL_TYPE.stream()
71-
.filter(x -> clazz.isInterface() ? x.equals(clazz) : x.isAssignableFrom(clazz))
72-
.sorted((Class<?> o1, Class<?> o2) -> {
73-
// -1: o1 more special than o2
74-
// 0: o1 equals o2 or on same samelevel
75-
// 1: o2 more special than o1
76-
if (o1.isAssignableFrom(o2)) {
77-
if (o2.isAssignableFrom(o1)) {
78-
return 0;
79-
}
80-
return 1;
81-
}
82-
if (o2.isAssignableFrom(o1)) {
83-
return -1;
84-
}
85-
return 0;
86-
})
87-
.findFirst()
88-
.orElse(null);
89-
}
90-
91-
private String findModelType(Class<?> clazz) {
92-
Class<?> type = getMostSpecificTypeWithModelType(clazz);
93-
if (type != null) {
94-
return type.getSimpleName();
95-
}
96-
for (Class<?> interfaceClass : clazz.getInterfaces()) {
97-
String result = findModelType(interfaceClass);
98-
if (result != null) {
99-
return result;
100-
}
101-
}
102-
Class<?> superClass = clazz.getSuperclass();
103-
if (superClass != null) {
104-
return findModelType(superClass);
105-
}
106-
return null;
107-
}
108-
10969
@Override
11070
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config, AnnotatedClass ac, JavaType baseType) {
111-
String modelType = findModelType(ac.getRawType());
71+
String modelType = ReflectionHelper.getModelType(ac.getRawType());
11272
if (modelType != null) {
11373
TypeResolverBuilder<?> result = _constructStdTypeResolverBuilder();
11474
result = result.init(JsonTypeInfo.Id.NAME, null);

0 commit comments

Comments
 (0)