Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to obtain fully qualiifed name of IAnnotation?
How to obtain fully qualiifed name of IAnnotation? [message #1807764] Fri, 07 June 2019 19:29 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Hello,

Consider this snippet which logs some information for an IMethod from JDT's model:

private void processMethod(IMethod method) throws JavaModelException {
	IType type = method.getDeclaringType();
	IPackageFragment packageFragment = type.getPackageFragment();

	IAnnotation[] annotations = method.getAnnotations();
	if ((annotations != null) && (annotations.length > 0)) {
		System.err.format("METHOD: %s%n", method);
		System.err.format("PACKAGE FRAGMENT PATH: %s %n", packageFragment.getPath());
		System.err.format("TYPE FQN: %s %n", type.getFullyQualifiedName());
		System.err.format("METHOD ELEMENT NAME: %s %n", method.getElementName());
		for (IAnnotation annotation : annotations) {
			System.err.format("    ANNOTATION NAME: %s %n", annotation.getElementName());
			// String fqn = annotation.getFullyQualifiedName(); ???
		}

		System.err.flush();
	}
}


I noticed that unlike IType, there is no method for obtaining the fully-qualified name of the annotation. Furthermore, the getElementName() does NOT return consistent information:


  • SourceMethod will return the non-qualified name (so if the source importe some.package.Anno then the IAnnotation will just return "Anno")
  • BinaryMethod (e.g. from an application library of a JAR archive in an m2eclipse project) always returns the fully-qualified name for the IAnnotation (e.g you would get the entire "some.package.Anno" from it)


How would you suggest I get the fully qualified name of the annotation in this snippet where my input is just the IMethod?

P.S. The interface itself is quite simple (just 3 methods) and only extends IJavaElement. Am I supposed to somehow locate the imports of the file that contains IType and actually resolve the short name based on the source scope? That seems like too much work that probably JDT already has in its Java model.
Re: How to obtain fully qualiifed name of IAnnotation? [message #1807765 is a reply to message #1807764] Fri, 07 June 2019 19:46 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
The light-weight Java Model doesn't have full resolved information, so the inconsistencies you are seeing are by design (binary class files use qualified names everywhere, but source files don't).

To resolve the name of the annotation try
type.resolveType(annotation.getElementName())

If 'type' is the context where the annotation occurs, it should be able to leverage the imports in that file.
Re: How to obtain fully qualiifed name of IAnnotation? [message #1809016 is a reply to message #1807765] Fri, 05 July 2019 22:15 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Just realised I never replied to thank you. This works great.
Re: How to obtain fully qualiifed name of IAnnotation? [message #1809017 is a reply to message #1809016] Fri, 05 July 2019 22:24 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Quote:
Just realised I never replied to thank you. This works great.


:)
Previous Topic:Very slow debugging of native calls JDK 12.0.1
Next Topic:Crazy Eclipse Update & Install Behaviour
Goto Forum:
  


Current Time: Thu Apr 25 15:35:56 GMT 2024

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

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

Back to the top