You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: annotationprocessortesting/src/main/java/com/tickaroo/tikxml/annotationprocessing/element/polymorphism/Organisation.java
Copy file name to clipboardexpand all lines: annotationprocessortesting/src/main/java/com/tickaroo/tikxml/annotationprocessing/elementlist/InlineListCatalogue.java
+2-1
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@
21
21
importcom.tickaroo.tikxml.annotation.Element;
22
22
importcom.tickaroo.tikxml.annotation.Xml;
23
23
importjava.util.List;
24
+
importjava.util.Objects;
24
25
25
26
/**
26
27
* @author Hannes Dorfmann
@@ -37,7 +38,7 @@ public class InlineListCatalogue {
+"@${PropertyElement::class.java.simpleName}, @${Element::class.java.simpleName} or @${TextContent::class.java.simpleName}"
102
+
+"and not multiple of them! The field ${element.simpleName} in class "
103
103
+"${(element.enclosingElement asTypeElement).qualifiedName} is annotated with more than one of these annotations. You must annotate a field with exactly one of these annotations (not multiple)!")
+"@${PropertyElement::class.java.simpleName}, @${Element::class.java.simpleName} or @${TextContent::class.java.simpleName}"
138
+
+"and not multiple of them! The field ${element.simpleName} in class "
139
139
+"${(element.enclosingElement asTypeElement).qualifiedName} is annotated with more than one of these annotations. You must annotate a field with exactly one of these annotations (not multiple)!")
140
140
}
141
141
@@ -169,26 +169,32 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
169
169
"The type of field '${element.simpleName}' in class ${element.enclosingElement} is not a class nor a interface. Only classes or interfaces can be annotated with @${Element::class.simpleName} annotation. If you try to annotate primitives than @${PropertyElement::class.simpleName}")
170
170
}
171
171
172
-
if (isList(element)) {
173
-
val genericListType = getGenericTypeFromList(element)
174
-
val genericListTypeElement = typeUtils.asElement(genericListType) asTypeElement
175
-
val genericTypeNames = genericTypes[genericListTypeElement.toString()]
176
-
177
-
if (genericTypeNames ==null) {
178
-
if (genericListTypeElement.isInterface()) {
172
+
val checkTypeElement =fun(typeElement:TypeElement) {
173
+
if (elementAnnotation.compileTimeChecks) {
174
+
if (typeElement.isInterface()) {
179
175
throwProcessingException(element,
180
-
"The generic list type of '$element' is an interface. Hence polymorphism must be resolved by annotating this interface with @${GenericAdapter::class.simpleName}.")
176
+
"The type of field '${element.simpleName}' in class ${element.getSurroundingClassQualifiedName()}is an interface. Hence polymorphism must be resolved by annotating this interface with @${GenericAdapter::class.simpleName}!")
181
177
}
182
178
183
-
if (genericListTypeElement.isAbstract()) {
179
+
if (typeElement.isAbstract()) {
184
180
throwProcessingException(element,
185
-
"The generic list type of '$element' is a abstract class. Hence polymorphism must be resolved by annotating this abstract class with @${GenericAdapter::class.simpleName}.")
186
-
} elseif (genericListTypeElement.isClass()) {
181
+
"The type of field '${element.simpleName}' in class ${element.getSurroundingClassQualifiedName()}is an abstract class. Hence polymorphism must be resolved by annotating this abstract class with @${GenericAdapter::class.simpleName}!")
182
+
} elseif (typeElement.isClass()) {
187
183
checkNameMatchers(nameMatchers, element)
188
184
}
185
+
}
186
+
}
187
+
188
+
if (isList(element)) {
189
+
val genericListType = getGenericTypeFromList(element)
190
+
val genericListTypeElement = typeUtils.asElement(genericListType) asTypeElement
191
+
val genericTypeNames = genericTypes[genericListTypeElement.toString()]
192
+
193
+
if (genericTypeNames ==null) {
194
+
checkTypeElement(genericListTypeElement)
189
195
190
196
val elementName =if (elementAnnotation.name.isEmpty()) {
@@ -211,30 +219,20 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
211
219
val genericTypeNames = genericTypes[element.asType().toString()]
212
220
val typeElement = (element.asType() asDeclaredType).asElement() asTypeElement
213
221
214
-
if (genericTypeNames ==null&& typeElement.superclass.toString() !="none") {
215
-
if (elementAnnotation.compileTimeChecks) {
216
-
if (typeUtils.asElement(element.asType()).isInterface()) {
217
-
throwProcessingException(element,
218
-
"The type of field '${element.simpleName}' in class ${element.getSurroundingClassQualifiedName()} is an interface. Hence polymorphism must be resolved by annotating this interface with @${GenericAdapter::class.simpleName}!")
219
-
}
220
-
221
-
if (typeUtils.asElement(element.asType()).isAbstract()) {
222
-
throwProcessingException(element,
223
-
"The type of field '${element.simpleName}' in class ${element.getSurroundingClassQualifiedName()} is an abstract class. Hence polymorphism must be resolved by annotating this abstract class with @${GenericAdapter::class.simpleName}!")
224
-
}
225
-
226
-
checkNameMatchers(nameMatchers, element)
227
-
}
222
+
returnif (genericTypeNames ==null) {
223
+
checkTypeElement(typeElement)
228
224
229
225
val elementName =if (elementAnnotation.name.isEmpty()) {
"Conflict: A @${ElementNameMatcher::class.simpleName} with the name \"${matcher.name}\" is already mapped to the type ${filteredNameMatchers[0].type} to resolve polymorphism. Hence it cannot be mapped to ${matcher.type} as well.")
298
+
"Conflict: A @${ElementNameMatcher::class.simpleName} with the name \"${matcher.name}\" is already mapped to the type $matcherType to resolve polymorphism. Hence it cannot be mapped to $conflictingNameMatcherType as well.")
285
299
}
286
300
}
287
301
288
302
val checkTargetClassXmlAnnotated =fun(element:VariableElement, typeElement:TypeElement) {
289
-
if (typeElement.getAnnotation(Xml::class.java) ==null) {
303
+
if (typeElement.getAnnotation(Xml::class.java) ==null&& (typeUtils.asElement(
"The class ${typeElement.qualifiedName} is not annotated with @${Xml::class.simpleName}, but is used in '$element' in class @${element.getSurroundingClassQualifiedName()} to resolve polymorphism. Please annotate @${element.getSurroundingClassQualifiedName()} with @${Xml::class.simpleName}")
292
307
}
@@ -301,7 +316,7 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
301
316
"Neither @${ElementNameMatcher::class.simpleName} nor @${GenericAdapter::class.simpleName} specified to resolve polymorphism!")
302
317
}
303
318
304
-
val namingMap =mutableMapOf<String, PolymorphicTypeElementNameMatcher>()
319
+
val namingMap =hashMapOf<String, PolymorphicTypeElementNameMatcher>()
305
320
306
321
// add generic types first
307
322
genericTypes?.forEach { qualifiedName ->
@@ -340,12 +355,12 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
@@ -381,7 +396,7 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
381
396
val variableType =if (isList(variableElement)) getGenericTypeFromList(variableElement) else variableElement.asType()
382
397
if (!typeUtils.isAssignable(typeElement.asType(), variableType)) {
383
398
throwProcessingException(variableElement,
384
-
"The type $typeElement must be a sub type of ${variableType}. Otherwise this type cannot be used in @${ElementNameMatcher::class.simpleName} to resolve polymorphism");
399
+
"The type $typeElement must be a sub type of ${variableType}. Otherwise this type cannot be used in @${ElementNameMatcher::class.simpleName} to resolve polymorphism.")
385
400
}
386
401
387
402
for (constructorinElementFilter.constructorsIn(typeElement.enclosedElements)) {
@@ -441,14 +456,15 @@ open class DefaultAnnotationDetector(protected val elementUtils: Elements, prote
441
456
* Get the xmlElement name which is either @Xml(name = "foo") property or the class name decapitalize (first letter in lower case)
val xmlAnnotation = typeElement.getAnnotation(Xml::class.java)
447
462
val genericAdapterAnnotation = typeElement.getAnnotation(GenericAdapter::class.java)
448
463
val annotationName =
449
464
if (typeElement.isClass() &&!typeElement.isAbstract()) "@${Xml::class.simpleName}"else"@${GenericAdapter::class.simpleName}"
465
+
val checkAnnotation = (typeElement.isClass() && typeElement.hasSuperClass()) ||!allowJavaObject
450
466
451
-
if (xmlAnnotation ==null&& genericAdapterAnnotation ==null&& compileTimeChecks) {
467
+
if (xmlAnnotation ==null&& genericAdapterAnnotation ==null&& compileTimeChecks&& checkAnnotation) {
452
468
throwProcessingException(field,
453
469
"The type ${typeElement.qualifiedName} used for field '$field' in ${field.getSurroundingClassQualifiedName()} can't be used, because it is not annotated with $annotationName. Annotate ${typeElement.qualifiedName} with $annotationName!")
if (!currentElement.isXmlElementAccessableFromOutsideTypeAdapter()) {
89
-
throwProcessingException(currentElement.element, "Element $currentElement can't have attributes that are accessed from outside of the TypeAdapter that is generated from @${Element::class.simpleName} annotated class! Therefore attribute $attributeField can't be added. Most likely the @${Path::class.simpleName} is in conflict with an @${Element::class.simpleName} annotation.")
val existingAttribute = currentElement.attributes[attributeField.name]
87
+
val currentElement = getXmlElementForPath(path)
88
+
if (!currentElement.isXmlElementAccessableFromOutsideTypeAdapter()) {
89
+
throwProcessingException(currentElement.element,
90
+
"Element $currentElement can't have attributes that are accessed from outside of the TypeAdapter that is generated from @${Element::class.simpleName} annotated class! Therefore attribute $attributeField can't be added. Most likely the @${Path::class.simpleName} is in conflict with an @${Element::class.simpleName} annotation.")
91
+
}
93
92
94
-
if (existingAttribute !=null) {
95
-
throwProcessingException(attributeField.element, "Conflict: $attributeField has the same xml attribute name "
96
-
+"'${attributeField.name}' as the $existingAttribute. "
throwProcessingException(variableField, "Conflict: $toInsert is in conflict with $existingElement. Maybe both have the same xml name '${toInsert.name}' (you can change that via annotations) or @${Path::class.simpleName} is causing this conflict.")
120
-
}
112
+
val existingElement = currentElement.childElements[toInsert.name]
"Conflict: $toInsert is in conflict with $existingElement. Maybe both have the same xml name '${toInsert.name}' (you can change that via annotations) or @${Path::class.simpleName} is causing this conflict.")
for ((name, attributeField) in toMerge.attributes) {
132
-
if (into.attributes[name] !=null) {
133
-
// Should not be possible, because we can't build a path with two PlaceholderXmlElement. Hence, this should never be reached.
134
-
throwProcessingException(toMerge.element, "Conflict: $toMerge has the same xml attribute name '$name' as $into . You can specify another name via annotations.")
for ((name, attributeField) in toMerge.attributes) {
134
+
if (into.attributes[name] !=null) {
135
+
// Should not be possible, because we can't build a path with two PlaceholderXmlElement. Hence, this should never be reached.
136
+
throwProcessingException(toMerge.element,
137
+
"Conflict: $toMerge has the same xml attribute name '$name' as $into . You can specify another name via annotations.")
138
+
}
139
139
140
-
// merge child elements
141
-
for ((name, element) in toMerge.childElements) {
142
-
if (into.childElements[name] !=null) {
143
-
// Should not be possible, because we can't build a path with two placeholder PlaceholderXmlElement. Hence, this should never be reached.
144
-
throwProcessingException(toMerge.element, "Conflict: $toMerge is in conflict with $into. Maybe both have the same xml name '$name' (you can change that via annotations) or @${Path::class.simpleName} is causing this conflict.")
// Should not be possible, because we can't build a path with two placeholder PlaceholderXmlElement. Hence, this should never be reached.
147
+
throwProcessingException(toMerge.element,
148
+
"Conflict: $toMerge is in conflict with $into. Maybe both have the same xml name '$name' (you can change that via annotations) or @${Path::class.simpleName} is causing this conflict.")
0 commit comments