Get the IType for an IAnnotation? [message #257652] |
Wed, 03 December 2008 20:46  |
Eclipse User |
|
|
|
Hello,
I am wondering how to get the IType for a given IAnnotation. That is, if a
method is annotated somehow:
@Pure
void getSize() {...}
I'd like the IType associated with @Pure:
public @interface Pure {...}
I am assuming this is possible, because when I ctrl-click on @Pure in the
Java IDE, it takes me to the annotation declaration. However, as far as I
can tell, the only way to tell what kind of annotation I have is by
calling IAnnotation.getElementName(), which returns a string. At that
point, I am stuck.
I apologize if this is a strange question. It's probably because I've been
working in ITypeBinding/IAnnotationBinding land, where this isn't
particularly difficult.
Thanks,
Nels
|
|
|
|
Re: Get the IType for an IAnnotation? [message #257682 is a reply to message #257652] |
Thu, 04 December 2008 17:59  |
Eclipse User |
|
|
|
Okay, I think I may have figured it out from similar posts about how to
get the IType from an IField.
It seems like we can call getElementName() on the IAnnotation to get its
name and then query the type from which it came. This will then return a
String[][], which is basically a list of pairs. Each pair holds the
package name and the type name for one of the types that your element
could have been. Finally, you ask your IJavaProject to give you an IType
for that package name/type name. All together, it looks sort of like this:
public void printAnnotationTypes(IType annotated_type, IJavaProject
project) {
try {
for( IAnnotation anno : annotated_type.getAnnotations() ) {
for( String[] name : annotated_type.resolveType(anno.getElementName())
) {
String pack = name[0];
String clazz = name[1];
IType anno_type = project.findType(pack , clazz); // Other versions
you may want to use
System.out.println("Got it! " + anno_type);
}
}
} catch (JavaModelException e) {}
}
|
|
|
Powered by
FUDForum. Page generated in 0.04127 seconds