Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Get the IType for an IAnnotation?
Get the IType for an IAnnotation? [message #257652] Wed, 03 December 2008 20:46 Go to next message
Eclipse UserFriend
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 #257658 is a reply to message #257652] Wed, 03 December 2008 21:14 Go to previous messageGo to next message
Eclipse UserFriend
Sorry, this example probably makes more sense if the getter returns an int!
@Pure
int getSize() {...}
Re: Get the IType for an IAnnotation? [message #257682 is a reply to message #257652] Thu, 04 December 2008 17:59 Go to previous message
Eclipse UserFriend
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) {}
}
Previous Topic:Java SE Development Kit 6
Next Topic:Adding tools.jar to the classpath
Goto Forum:
  


Current Time: Fri Apr 18 05:33:55 EDT 2025

Powered by FUDForum. Page generated in 0.04127 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top