bug about copy field of type HashMap from one class to another

20 views
Skip to first unread message

Nguyen Viet Hoa

unread,
Feb 20, 2015, 9:41:33 AM2/20/15
to xtend...@googlegroups.com
Hi,

    I'm not sure it's a bug or my code was wrong. Somebody can help answer this question pls. I've tried to copy fields from one class to another like this:

      clazz.declaredFields.forEach [ sourceField |
            extensionClass.addField(sourceField.simpleName) [ targetField |
                targetField.final = sourceField.final
                targetField.static = sourceField.static
                targetField.transient = sourceField.transient
                targetField.volatile = sourceField.volatile
                targetField.initializer = sourceField.initializer
                targetField.type = sourceField.type
                targetField.docComment = sourceField.docComment
                targetField.visibility = sourceField.visibility
                sourceField.annotations.forEach[annotationTypeDeclaration|
                targetField.addAnnotation(annotationTypeDeclaration)]
            ]
        ]


but the code generated is not correct, for example, for the source field : private final HashMap<ArrayList<?>, Model> _createCache_transform = CollectionLiterals.newHashMap();
will give : private final Object _createCache_transform = CollectionLiterals.newHashMap();

and the import expression for the HashMap, ArrayList and CollectionLiterals do not generated automatically for the target class.

Thanks,

Hoa

Stefan Oehme

unread,
Feb 21, 2015, 3:13:34 AM2/21/15
to xtend...@googlegroups.com
The problem is that you cannot "copy" the initializer. It is actually moved, so the source field no longer has one. And since it now lacks an initializer, it no longer knows its type.

Instead you could create an initializer that sets the value to the source fields value in the generated code.

target.initializer = '''«source.declaringType».«source.simpleName»'''

Stefan Oehme

unread,
Feb 21, 2015, 3:18:40 AM2/21/15
to xtend...@googlegroups.com
Of course that only works for constants that the target can see. Another possibility would be a getter call etc. It really depends on your use case.

Nguyen Viet Hoa

unread,
Feb 23, 2015, 3:46:30 AM2/23/15
to xtend...@googlegroups.com
Thank you Stefan so much for your response, i'll try your suggestions.
Reply all
Reply to author
Forward
0 new messages