File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Getting Annotations via JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Getting Annotations via JNI" Watch "Getting Annotations via JNI" New topic
Author

Getting Annotations via JNI

Parth Mehta
Greenhorn

Joined: Mar 02, 2007
Posts: 7
Hello all,

Does JNI provide functions for accessing field annotations? I easily found functions to get values of fields but could not locate anything for accessing annotations associated with a particular field. Any help appreciated

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Annotations can be retrieved through reflection, but only if they are specified with RetentionPolicy.RUNTIME. You can use reflection from JNI as well. However, I don't think this is something you should be doing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Parth Mehta
Greenhorn

Joined: Mar 02, 2007
Posts: 7
Yes that is correct. The retention policy is runtime. What I am actually looking for is an API that can retrieve the annotation on a field. There are two methods GetFieldID and Get<TYPE>Field that deal with fields in the JNI as far as I know. The Get<TYPE>Field (For example GetIntField) simply returns the value held by a field identified by the field ID of type <TYPE>. There are no functions to get annotations.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Like I said, use reflection. java.lang.Class, java.lang.reflect.Method, java.lang.reflect.Field - all allow you to retrieve their annotations. You would just need to call this code from JNI, which makes me think it may not be a good idea what you're trying to do. So the main question is - what exactly are you trying to do?
Parth Mehta
Greenhorn

Joined: Mar 02, 2007
Posts: 7
Hey Rob,

I guess this is a fair question...This is basically on the Android platform. We have some very performance critical code that deals with marshalling/unmarshalling of bytes streams from/to objects. They are, unfortunately not in the standard java serialization format as this is a proprietary serialization protocol that works with systems designed using C++ or .NET as well. This depends on the ordering of fields in a certain way for it to be decoded correctly ( Cannot reveal how, my boss will fire me ). I achieve this ordering by annotating fields and giving the annotations some ordering value. To my utter dismay, I discovered that getting the value of an annotation is EXTREMELY slow in pure java code via reflection in Android. I ran tests using JNI and reflection stuff seems to be very very fast there (like calling methods and accessing fields). Now I need to do the same via annotation. Hence the original question: How to get annotations using JNI...

I tried all your suggestions before already. The problem is that in Java, there is a Field object obviously which contains the annotation. There is nothing equivalent in JNI (atleast not that I have managed to find out) No C struct of the equivalent Field (such as jField or something like that ) and also no GetFieldAnnotation... type function call in the JNIEnv struct...I am therefore stuck
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

You just need to convert the Java reflection code to JNI code:
- get the object's class
- get the Class class
- get the Class.getMethod method from the Class class
- invoke the Class.getMethod method on the object's class
- get the method's class
- get the Method.getAnnotation or Method.getDeclaredAnnotations method from the method's class
- invoke this method on the method
- you now have your annotation(s)

However, I doubt that is going to give you much performance improvement, because it's still reflection. Most of that is already implemented in native code.

Parth Mehta wrote:Cannot reveal how, my boss will fire me

Fair enough. Some code and algorithms need to remain private, because that's where profit comes from.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Annotations via JNI