2

How can we access static enum fields using JNI invocation API

I am trying to access glassfish org.glassfish.api.embedded.ContainerBuilder.Type enumeration from Glassfish api using 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 error as Exception in thread "main" java.lang.NoSuchFieldError: web, How can I access that field ?

Xinus
  • 29,617
  • 32
  • 119
  • 165

3 Answers3

6

Actually I was missing L at front and ; at end of classname, I done following changes

jfieldID Type_web=env->GetStaticFieldID(
    Type,"web","Lorg/glassfish/api/embedded/ContainerBuilder$Type;");
Xinus
  • 29,617
  • 32
  • 119
  • 165
2

There is a method in java.lang.Class getEnumConstants.

According to doc:

Returns the elements of this enum class or null if this Class object does not represent an enum type.

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
1

I've not used reflection to look at enum classes myself, but it's possible that they're being stored in a strange way. In your situation I'd call into Class.getFields() and have a look at the list of the class's fields.

crazyscot
  • 11,819
  • 2
  • 39
  • 40