Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Identifying Annotated POJOs inside a Jar File
Identifying Annotated POJOs inside a Jar File [message #261283] Mon, 20 July 2009 16:02 Go to next message
Tilak Sharma is currently offline Tilak SharmaFriend
Messages: 48
Registered: July 2009
Member
Hi,

I am in need of a utility to parse a Jar file which can contain POJOs
which are annotated with JAX-RS and/or JAX-WS annotations.

I need to identify all such POJOs and display them on the UI.

I had tried APT (Sun’s Annotation Processing Tool, ObjectWeb’s ASM and
reflections). But all of them need the Class file to be loaded. I do not
need Annotation Processing or bytecode manipulation tool.

I just need to identify a POJO inside a Jar if it contains any of JAX-RS
or JAX-WS annotations without loading the class files (and also not
extracting the Jar file).

Please help me in finding such an API or an Open Source Tool.

Thanks a lot!
Re: Identifying Annotated POJOs inside a Jar File [message #261287 is a reply to message #261283] Mon, 20 July 2009 16:07 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Tilak a écrit :
> I am in need of a utility to parse a Jar file which can contain POJOs
> which are annotated with JAX-RS and/or JAX-WS annotations.
> I need to identify all such POJOs and display them on the UI.
> I had tried APT (Sun?s Annotation Processing Tool, ObjectWeb?s ASM and
> reflections). But all of them need the Class file to be loaded. I do not
> need Annotation Processing or bytecode manipulation tool.
> I just need to identify a POJO inside a Jar if it contains any of JAX-RS
> or JAX-WS annotations without loading the class files (and also not
> extracting the Jar file).
> Please help me in finding such an API or an Open Source Tool.
ASM doesn't force you to load the class files. You can scan the .class
files and detect what you need without extracting the jar file.
You could also use the org.eclipse.jdt.core.util.IClassFileReader API
through this API
org.eclipse.jdt.core.ToolFactory.createDefaultClassFileReade r(String,
String, int).
HTH,
--
Olivier
Re: Identifying Annotated POJOs inside a Jar File [message #261291 is a reply to message #261287] Tue, 21 July 2009 15:10 Go to previous messageGo to next message
Tilak Sharma is currently offline Tilak SharmaFriend
Messages: 48
Registered: July 2009
Member
Thanks a LOT, Olivier !

I was searching for this sort of code, which allows me to read a class
from a jar file without loading the class file. I am able to identify the
annotations from the attributes of the class now.

ClassFileReader classFileReader =
(ClassFileReader)ToolFactory.createDefaultClassFileReader(ja rFile.getName(),
entry.getName(), IClassFileReader.ALL);

IClassFileAttribute[] attributes = classFileReader.getAttributes();

RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnAttr = null;

List<String> annotationsOfEntry = new ArrayList<String>();

String annName = null;

for (int i = 0; i < attributes.length; i++) {

IClassFileAttribute attr = attributes[i];
if(attr instanceof RuntimeInvisibleAnnotationsAttribute){
runtimeInvisibleAnnAttr=
(RuntimeInvisibleAnnotationsAttribute)attributes[i];
IAnnotation[] subAnnotations =
runtimeInvisibleAnnAttr.getAnnotations();

char[] typeName = subAnnotations[0].getTypeName();
String annotation = new String( typeName).substring(1,
typeName.length-1).replace('/', '.');

annotationsOfEntry.add(annotation);
}
}


I guess all the annotations will either be
RuntimeInvisibleAnnotationsAttribute or RuntimeVisibleAnnotationsAttribute
inside the class's attributes.

Please correct me if i am wrong in the above approach.


Thanks again,
Tilak
Re: Identifying Annotated POJOs inside a Jar File [message #261295 is a reply to message #261291] Tue, 21 July 2009 16:08 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Tilak a écrit :
> Thanks a LOT, Olivier !
Np.

> IClassFileAttribute[] attributes = classFileReader.getAttributes();
With this, you only get the annotations on the type itself.
You need to iterate through all methods and fields in order to check if
they also have annotations.
Method can also have annotations on the parameters.

> I guess all the annotations will either be
> RuntimeInvisibleAnnotationsAttribute or
> RuntimeVisibleAnnotationsAttribute inside the class's attributes.
Yes, annotations are either visible or invisible depending on their
java.lang.annotation.Retention value.
If the retention is java.lang.annotation.RetentionPolicy.CLASS, then
they end up as invisible annotations. If the retention is
java.lang.annotation.RetentionPolicy.RUNTIME, then they end up as
visible annotations.

Same applies for parameters of methods with the corresponding
org.eclipse.jdt.core.util.IRuntimeVisibleParameterAnnotation sAttribute
and
org.eclipse.jdt.core.util.IRuntimeInvisibleParameterAnnotati onsAttribute.

Not that you should use the interface types in your code, not the
default implementations classes.
org.eclipse.jdt.core.util.IRuntimeVisibleAnnotationsAttribut e and
org.eclipse.jdt.core.util.IRuntimeInvisibleAnnotationsAttrib ute vs
org.eclipse.jdt.core.util.RuntimeVisibleParameterAnnotations Attribute
and org.eclipse.jdt.core.util.RuntimeVisibleParameterAnnotations Attribute.

HTH,
--
Olivier
Re: Identifying Annotated POJOs inside a Jar File [message #524186 is a reply to message #261295] Wed, 31 March 2010 06:21 Go to previous messageGo to next message
Tilak Sharma is currently offline Tilak SharmaFriend
Messages: 48
Registered: July 2009
Member
Thanks for all the very useful information, Olivier.
The information provided by you helped me solve my problem.

I have written a blog entry in my Techie Stuff blog about this. Hope it helps anyone who might face the same problem.
Re: Identifying Annotated POJOs inside a Jar File [message #636032 is a reply to message #524186] Fri, 29 October 2010 06:13 Go to previous messageGo to next message
Tilak Sharma is currently offline Tilak SharmaFriend
Messages: 48
Registered: July 2009
Member
I use JDT API org.eclipse.jdt.internal.core.util.ClassFileReader to identify annotated POJOs, which doesn't provide functionality to identify a POJO as annotated if it's parent (interface) is already annotated.

I quickly checked if it can be achieved by using any other alternative ways to create the ClassFileReader. That didn't help.

A JAXRS POJO can be implemented by a class that implements an interface, where the interface is annotated. This is a widely adopted pattern, it isolates the Annotations into an implemented interface.

If you try and import a POJO jar using JDT's IClassFileReader, these POJOs are not discovered. You need to annotate the classes directly for discovery.

Does JDT provde this functionality? or is there any other better way to identify a annotated POJO?

Thanks,
Tilak
Re: Identifying Annotated POJOs inside a Jar File [message #636986 is a reply to message #636032] Wed, 03 November 2010 16:30 Go to previous message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
On 10/28/10 11:13 PM, Tilak wrote:
> I use JDT API org.eclipse.jdt.internal.core.util.ClassFileReader to
> identify annotated POJOs, which doesn't provide functionality to
> identify a POJO as annotated if it's parent (interface) is already
> annotated.

If you're directly reading class files, you may have to navigate the
class hierarchy yourself, e.g., recursively find and open the parent
types to search for annotations.

If the annotation type you're looking for has
@Retention(RetentionType.RUNTIME) or @Retention(RetentionType.CLASS),
and if it has @Inherited, then the APT annotation processing API will
report a class as showing the annotation, if one of its superclasses is
annotated. Roughly the same is true of reading the class reflectively.
However, that does not apply to interfaces, only to superclasses. You
can read about that in the JDK docs [1].

I guess what I'm saying is that by the terms of Java, a POJO _is not_
annotated just because it implements an interface that happens to be
annotated. Therefore there is no direct way to ask a representation of
a class whether it is annotated in this way - rather, you have to walk
the supertype hierarchy and ask the individual interfaces whether they
have the annotation.


[1]
http://download.oracle.com/javase/1.5.0/docs/api/index.html? java/lang/annotation/Retention.html
Previous Topic:JRE/JDK unbound after the latest Mac Java update, how to fix
Next Topic:Hey - new to the forum and have a simple question!
Goto Forum:
  


Current Time: Thu Apr 25 04:29:48 GMT 2024

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

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

Back to the top