Is there a way to find annotations on the type of a field?

42 views
Skip to first unread message

Christian Vogel

unread,
Jul 9, 2014, 11:04:04 AM7/9/14
to xtend...@googlegroups.com
Hi,

Is there a way to find annotations on the type of a field?

MutableClassDeclaration has getDeclaredFields(), which gives me a list of MutableFieldDeclaration. From a field declaration, I can get the type of the field through FieldDeclaration.getType(). I then have a TypeReference. I was expecting to be able to then call getAnnotations() on a TypeReference, but I can't.


Thanks,


Christian


Stefan Oehme

unread,
Jul 12, 2014, 4:14:44 AM7/12/14
to xtend...@googlegroups.com
You can call getType on that type reference to get the referenced type declaration. There you will get the annotations.

Christian Vogel

unread,
Jul 14, 2014, 10:02:45 AM7/14/14
to xtend...@googlegroups.com
Hi, I tried, but I cannot.

From a MutableFieldDeclaration field I can get the type, and the type from the type, but I do not get access to findAnnotation(), which only seems to be accessible from a MutableXXX. 

The reason I am trying to get annotations from a field type is that I am trying to detect if that field type is of some type I processed with another entity processor.

Since that processor adds an interface to the class, I tried using .isAssignableFrom, and that works, however with one caveat: suddenly the order in which classes are placed in the .xtend file start to matter. IsAssignableFrom then returns a false where it would give a true if the classes were in a reversed order.

So my fallback was to look for the annotations on the types of my fields.

Stefan Oehme

unread,
Jul 14, 2014, 10:09:22 AM7/14/14
to xtend...@googlegroups.com

Sorry I forgot a part in there. You have to check whether the returned type is instanceof AnnotationTarget. Only then can you use findAnnotation. Some subclasses of Type cannot have annotations.

--
You received this message because you are subscribed to a topic in the Google Groups "Xtend Programming Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/xtend-lang/FL0NS9aBWuc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Vogel

unread,
Jul 14, 2014, 10:19:57 AM7/14/14
to Stefan Oehme, xtend...@googlegroups.com

Alright, that did work :-)

Reply all
Reply to author
Forward
0 new messages