Active Annotations: How do I get an import to be added for my used class?

49 views
Skip to first unread message

Christian Vogel

unread,
Aug 23, 2016, 8:30:01 AM8/23/16
to Xtend Programming Language
Hi,

I am having to do a lot of code in active annotation processors where I have to write full class names in the source code for the generated Java code to compile, because there is no import statement generated. That means having to use the full classname everywhere:

body = ['''final «Promise.name» promise = new «Promise.name»();''']

Which ends up in quite ugly long full classname code in the generated Java.

I heard a trick that you can remove the closure brackets and use newTypeReference, like this:

body = '''final «Promise.newTypeReference» promise = new «Promise.newTypeReference»();'''

That works in some simple cases but not always. I get this errors like these: java.lang.IllegalStateException: Added adapter to resource set during code generation: org.eclipse.xtext.resource.CompilerPhases$IndexingAdapter@2a74a3c9

What would be the correct way to go about this?

Thank you,

Christian

Christian Dietrich

unread,
Aug 24, 2016, 1:12:29 AM8/24/16
to Xtend Programming Language
looks like you misunderstood the second feature

body = '''«List»<«Iterator»<String>> iterators = new «ArrayList»<>();'''

Christian Vogel

unread,
Aug 24, 2016, 7:32:06 AM8/24/16
to xtend...@googlegroups.com
Thanks, that clears up a lot!

Micael Pedrosa

unread,
Aug 29, 2016, 11:37:00 AM8/29/16
to Xtend Programming Language
I have the same problems. Using something like '''final «List»<String> list = new «ArrayList»<>();''' doesn't always work.
The most annoying problem I think, is that it has an unpredictable behavior! And when it doesn't work I have to use «List.canonicalName»

Christian Dietrich

unread,
Aug 30, 2016, 1:06:46 AM8/30/16
to Xtend Programming Language
it actually works if you understand the behaviour. unfortunately the ticket to make the api more general is not yet in work nor fixed.

basic pattern is body= ''' <<here it works>> '''

if you factor out parts to separete methods, fields, vars etc you have to explicitely type them as StringConcatenationClient.

can you share a minimal non working example?

Reply all
Reply to author
Forward
0 new messages