Accessing static enum fields with JNI call API

How can we access static fields of an enum using JNI APIs

I am trying to access the glassfish org.glassfish.api.embedded.ContainerBuilder.Type enum from the Glassfish api using the following code

jclass Type= env->FindClass( 
    "org/glassfish/api/embedded/ContainerBuilder$Type");
jfieldID Type_web=env->GetStaticFieldID(
    Type,"web","org/glassfish/api/embedded/ContainerBuilder$Type");

      

But it always gives me an error like Exception in thread "main" java.lang.NoSuchFieldError: web

how can I access this field?

+2


a source to share


3 answers


Actually I was missing the L

front and ;

end of the class name, I made the following changes



jfieldID Type_web=env->GetStaticFieldID(
    Type,"web","Lorg/glassfish/api/embedded/ContainerBuilder$Type;");

      

+6


a source


Java.lang.Class has getEnumConstants .

According to the doc:



Returns the members of this enumeration class, or null if this class object does not represent an enumeration type.

+2


a source


I haven't used reflection to explore enum classes on my own, but it's possible that they are stored in a strange way. In your situation, I'll call in Class.getFields()

and take a look at the list of class fields.

+1


a source







All Articles