@@ -32,6 +32,7 @@ import com.tickaroo.tikxml.processor.field.Namespace
32
32
import com.tickaroo.tikxml.processor.field.PolymorphicSubstitutionField
33
33
import com.tickaroo.tikxml.processor.field.PolymorphicSubstitutionListField
34
34
import com.tickaroo.tikxml.processor.utils.isList
35
+ import com.tickaroo.tikxml.processor.xml.PlaceholderXmlElement
35
36
import com.tickaroo.tikxml.processor.xml.XmlChildElement
36
37
import com.tickaroo.tikxml.typeadapter.AttributeBinder
37
38
import com.tickaroo.tikxml.typeadapter.ChildElementBinder
@@ -86,13 +87,15 @@ class TypeAdapterCodeGenerator(
86
87
87
88
88
89
for ((xmlName, xmlElement) in annotatedClass.childElements) {
89
- if (xmlElement is PolymorphicSubstitutionField ) {
90
- val childElementBinderPrefix = if (xmlElement is PolymorphicSubstitutionListField ) {
91
- xmlElement.element.simpleName
92
- } else {
93
- val orginalElementTypeName = xmlElement.originalElementTypeMirror.toString().split(" ." ).last()
94
- " ${orginalElementTypeName.substring(0 , 1 ).toLowerCase(Locale .GERMANY )}${orginalElementTypeName.substring(1 ,
95
- orginalElementTypeName.length)} "
90
+ if (xmlElement is PolymorphicSubstitutionField || xmlElement.generateGenericChildBinder) {
91
+ val childElementBinderPrefix = when (xmlElement) {
92
+ is PolymorphicSubstitutionListField -> xmlElement.element.simpleName
93
+ is PlaceholderXmlElement -> xmlName
94
+ else -> {
95
+ val orginalElementTypeName = (xmlElement as PolymorphicSubstitutionField ).originalElementTypeMirror.toString().split(" ." ).last()
96
+ " ${orginalElementTypeName.substring(0 , 1 ).toLowerCase(Locale .GERMANY )}${orginalElementTypeName.substring(1 ,
97
+ orginalElementTypeName.length)} "
98
+ }
96
99
}
97
100
constructorBuilder.addStatement(" ${CodeGeneratorHelper .childElementBindersParam} .put(\$ S, \$ N)" , xmlName,
98
101
" ${childElementBinderPrefix} ChildElementBinder" )
@@ -214,10 +217,11 @@ class TypeAdapterCodeGenerator(
214
217
isList -> {
215
218
val genericListType = (firstConcreteType as PolymorphicSubstitutionListField ).genericListTypeMirror
216
219
fieldName = " ${genericName} ChildElementBinder"
220
+
217
221
fromXmlMethodSpecBuilder
218
- .beginControlFlow(" if (${CodeGeneratorHelper .valueParam} . $genericName == null)" )
219
- .addStatement (" ${CodeGeneratorHelper .valueParam} . $genericName = new \$ T()" ,
220
- ParameterizedTypeName .get(ClassName .get(ArrayList ::class .java), ClassName .get(genericListType)))
222
+ .beginControlFlow(" if (${firstConcreteType.accessResolver.resolveGetterForReadingXml()} == null)" )
223
+ .addCode (" ${firstConcreteType.accessResolver.resolveAssignment( " new \$ T()" ,
224
+ ParameterizedTypeName .get(ClassName .get(ArrayList ::class .java), ClassName .get(genericListType)))} " )
221
225
.endControlFlow()
222
226
223
227
if (! isRootList) {
@@ -231,11 +235,11 @@ class TypeAdapterCodeGenerator(
231
235
typeUtils.asElement(
232
236
(concreteType as PolymorphicSubstitutionListField ).typeMirror).simpleName.toString().toLowerCase(
233
237
Locale .GERMANY ) != concreteType.name.toLowerCase(Locale .GERMANY )
234
- }, fromXmlMethodSpecBuilder, genericListType, " v" , ! isRootList)
238
+ }, fromXmlMethodSpecBuilder, genericListType, " v" , true , isRootList)
235
239
236
240
fromXmlMethodSpecBuilder
237
241
.beginControlFlow(" if (v != null)" )
238
- .addStatement(" ${CodeGeneratorHelper .valueParam} . $genericName .add(v)" )
242
+ .addStatement(" ${firstConcreteType.accessResolver.resolveGetterForReadingXml()} .add(v)" )
239
243
if (! isRootList) {
240
244
fromXmlMethodSpecBuilder
241
245
.addStatement(" ${CodeGeneratorHelper .readerParam} .endElement()" )
@@ -255,7 +259,7 @@ class TypeAdapterCodeGenerator(
255
259
typeUtils.asElement(
256
260
(concreteType as PolymorphicSubstitutionField ).typeMirror).simpleName.toString().toLowerCase(
257
261
Locale .GERMANY ) != concreteType.name.toLowerCase(Locale .GERMANY )
258
- }, fromXmlMethodSpecBuilder, genericType!! , " ${CodeGeneratorHelper .valueParam} .$genericName " , false )
262
+ }, fromXmlMethodSpecBuilder, genericType!! , " ${CodeGeneratorHelper .valueParam} .$genericName " , false , isRootList )
259
263
fieldName =
260
264
" ${(genericType as DeclaredType ).asElement().simpleName.toString().decapitalize(Locale .GERMANY )} ChildElementBinder"
261
265
}
@@ -282,16 +286,18 @@ class TypeAdapterCodeGenerator(
282
286
fromXmlMethodSpecBuilder : MethodSpec .Builder ,
283
287
genericType : TypeMirror ,
284
288
valueParam : String ,
285
- isGenericListAndNotRoot : Boolean ) {
289
+ isGenericList : Boolean ,
290
+ isRootList : Boolean ) {
286
291
287
292
var hasSpecialMapping = false
293
+ val firstConreteType = concreteTypes.first() as PolymorphicSubstitutionField
288
294
concreteTypes
289
295
.filter { concreteType -> filter(concreteType) }
290
296
.takeIf { it.isNotEmpty() }
291
297
?.forEachIndexed { index, specialNamingType ->
292
298
if (! hasSpecialMapping) {
293
299
hasSpecialMapping = true
294
- if (isGenericListAndNotRoot ) {
300
+ if (isGenericList && ! isRootList ) {
295
301
fromXmlMethodSpecBuilder
296
302
.addStatement(" \$ T elementName" , String ::class .java)
297
303
.beginControlFlow(" if (${CodeGeneratorHelper .readerParam} .hasElement())" )
@@ -314,25 +320,41 @@ class TypeAdapterCodeGenerator(
314
320
315
321
val specialType = (specialNamingType as ? PolymorphicSubstitutionField )?.typeMirror
316
322
? : (specialNamingType as ? PolymorphicSubstitutionListField )?.typeMirror ? : genericType
317
- fromXmlMethodSpecBuilder.addStatement(
318
- " $valueParam = (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} )" ,
319
- specialType, specialType)
323
+ if (isGenericList) {
324
+ fromXmlMethodSpecBuilder
325
+ .addStatement(
326
+ " $valueParam = (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} , ${! isRootList && ! hasSpecialMapping} )" ,
327
+ specialType, specialType)
328
+ } else {
329
+ fromXmlMethodSpecBuilder
330
+ .addCode(firstConreteType.accessResolver.resolveAssignment(
331
+ " (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} , false)" ,
332
+ specialType, specialType))
333
+ }
320
334
}
321
335
322
336
if (hasSpecialMapping) {
323
337
fromXmlMethodSpecBuilder.nextControlFlow(" else" )
324
- } else if (isGenericListAndNotRoot) {
338
+ }
339
+ /* else if (isGenericList && !isRootList) {
325
340
fromXmlMethodSpecBuilder
326
341
.beginControlFlow("if (${CodeGeneratorHelper.readerParam}.hasElement())")
327
342
.addStatement("${CodeGeneratorHelper.readerParam}.beginElement()")
328
343
.addStatement("${CodeGeneratorHelper.readerParam}.nextElementName()")
329
344
.endControlFlow()
330
- }
345
+ }*/
331
346
332
- fromXmlMethodSpecBuilder
333
- .addStatement(
334
- " $valueParam = (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class, true).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} )" ,
335
- genericType, genericType)
347
+ if (isGenericList) {
348
+ fromXmlMethodSpecBuilder
349
+ .addStatement(
350
+ " $valueParam = (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class, true).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} , ${! isRootList && ! hasSpecialMapping} )" ,
351
+ genericType, genericType)
352
+ } else {
353
+ fromXmlMethodSpecBuilder
354
+ .addCode(firstConreteType.accessResolver.resolveAssignment(
355
+ " (\$ T) ${CodeGeneratorHelper .tikConfigParam} .getTypeAdapter(\$ T.class, true).fromXml(${CodeGeneratorHelper .readerParam} , ${CodeGeneratorHelper .tikConfigParam} , false)" ,
356
+ genericType, genericType))
357
+ }
336
358
337
359
if (hasSpecialMapping) {
338
360
fromXmlMethodSpecBuilder.endControlFlow()
@@ -355,6 +377,7 @@ class TypeAdapterCodeGenerator(
355
377
.addAnnotation(Override ::class .java)
356
378
.addParameter(XmlReader ::class .java, reader)
357
379
.addParameter(TikXmlConfig ::class .java, config)
380
+ .addParameter(Boolean ::class .java, " isGenericList" )
358
381
.addException(IOException ::class .java)
359
382
.addStatement(" \$ T \$ L = new \$ T()" , targetClassToParseInto, value, targetClassToParseInto)
360
383
0 commit comments