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
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.
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.
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?
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
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.