Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Getting annotations from package-info.java via IType
Getting annotations from package-info.java via IType [message #1707210] Thu, 03 September 2015 06:35 Go to next message
yev xxxxx is currently offline yev xxxxxFriend
Messages: 17
Registered: July 2014
Junior Member
I am trying to get list of annotation defined above package-info.

IType mytype = javaProject.findType("mypackage.package-info");
mytype.getAnnotations();



package-info.java looks like this:

@XmlJavaTypeAdapters(...),
})
package mypackage;

import someimport;




getting exception (on getAnnotations()):

Java Model Exception: Java Model Status [package-info [in package-info.java [in mypackage [in src/main/java [in TEST_Proj]]]] does not exist]

Re: Getting annotations from package-info.java via IType [message #1707319 is a reply to message #1707210] Thu, 03 September 2015 16:59 Go to previous messageGo to next message
Erick Hagstrom is currently offline Erick HagstromFriend
Messages: 107
Registered: April 2014
Location: USA
Senior Member
package-info isn't a type, so you can't get an IType for it.

Can you get an IPackageDeclaration for mypackage? That implements IAnnotatable, just like IType.
Re: Getting annotations from package-info.java via IType [message #1707328 is a reply to message #1707319] Thu, 03 September 2015 18:45 Go to previous messageGo to next message
yev xxxxx is currently offline yev xxxxxFriend
Messages: 17
Registered: July 2014
Junior Member
But mytype is not null, it doesn't fail on that line.

It fails on mytype.getAnnotations();
Re: Getting annotations from package-info.java via IType [message #1707335 is a reply to message #1707328] Thu, 03 September 2015 19:41 Go to previous messageGo to next message
Erick Hagstrom is currently offline Erick HagstromFriend
Messages: 107
Registered: April 2014
Location: USA
Senior Member
Look at your code. Notice that the annotations that you're trying to get are actually on the package declaration. There is no package-info type.
Re: Getting annotations from package-info.java via IType [message #1707373 is a reply to message #1707328] Fri, 04 September 2015 10:13 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
yev xxxxx wrote on Thu, 03 September 2015 20:45
But mytype is not null, it doesn't fail on that line.


I agree that this looks fishy. While the findType() method starts by looking at names of compilation units (as spec'ed), it should not return a non-existent type. Please consider filing a bug against JDT/Core.

For your task I'd try these steps:


  1. get the IPackageFragment (assuming that no split packages are involved)
  2. get an ICompilationUnit via getCompilationUnit(String)
  3. get the IPackageDeclaration(s) via getPackageDeclarations()
  4. use its getAnnotations()


I'm aware that this looks a bit clumsy, but package-info.java is a funny animal to begin with.

HTH
Stephan
Re: Getting annotations from package-info.java via IType [message #1707507 is a reply to message #1707373] Sun, 06 September 2015 10:32 Go to previous message
yev xxxxx is currently offline yev xxxxxFriend
Messages: 17
Registered: July 2014
Junior Member
thanks this approach worked,

[Updated on: Sun, 06 September 2015 10:32]

Report message to a moderator

Previous Topic:Configuring HTML Editor on where to resolve css/js files
Next Topic:Birt bar chart report called with time interval
Goto Forum:
  


Current Time: Thu Mar 28 09:06:58 GMT 2024

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

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

Back to the top