|
16 | 16 | package io.adminshell.aas.v3.dataformat.core; |
17 | 17 |
|
18 | 18 | import io.adminshell.aas.v3.model.Constraint; |
19 | | -import io.adminshell.aas.v3.model.DataSpecificationContent; |
20 | 19 | import io.adminshell.aas.v3.model.Referable; |
21 | 20 | import io.github.classgraph.ClassGraph; |
22 | 21 | import io.github.classgraph.ClassInfo; |
@@ -162,6 +161,62 @@ public static boolean isModelInterfaceOrDefaultImplementation(Class<?> type) { |
162 | 161 | return isModelInterface(type) || isDefaultImplementation(type); |
163 | 162 | } |
164 | 163 |
|
| 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 | + |
165 | 220 | static { |
166 | 221 | ScanResult modelScan = new ClassGraph() |
167 | 222 | .enableClassInfo() |
|
0 commit comments