7 Replies Latest reply: Jul 21, 2011 7:27 PM by EJP RSS

    Pass enum argument to Java method

    834191
      Hi all,

      I need to invoke a Java method from my C code via JNI. The method is a setter for an enum type field:

      enum Shape {
      ROUND, SQUARE;
      }

      public void setShape(Shape shape) {
      this.shape = shape;
      }

      But when I try to invoke it from C I have no clue how to do this. How do I get a reference to, for instance, Shape.ROUND?

      Thanks in advance for helping,
      Den
        • 1. Re: Pass enum argument to Java method
          jschellSomeoneStoleMyAlias
          This was answered in this forum I believe after the Oracle move. So it should be easy to find manually even if google doesn't find it.
          • 2. Re: Pass enum argument to Java method
            834191
            Yeah, tried both before posting (keywords "jni" and "enum") and found somewhat related answers but not what I'm looking for.
            Thanks anyway!
            • 3. Re: Pass enum argument to Java method
              EJP
              Run Javah on the enum itself. This produces a .h with the ordinals as #defines. Use the one you need and call MyEnum.values[ordinal] via JNI and pass the result to your Java method.
              • 4. Re: Pass enum argument to Java method
                834191
                Great tip, thank you!
                Den
                • 5. Re: Pass enum argument to Java method
                  876582
                  Can you please provide the command line example for generating the header file. When I use the javah -jni MyTest.Shape the header file is generated but does not contain any #defines or other references

                  java
                  public enum Shape
                  {
                       ROUND, SQUARE;
                  }


                  /* DO NOT EDIT THIS FILE - it is machine generated */
                  #include <jni.h>
                  /* Header for class MyTest_Shape */

                  #ifndef IncludedMyTest_Shape
                  #define IncludedMyTest_Shape
                  #ifdef __cplusplus
                  extern "C" {
                  #endif
                  #ifdef __cplusplus
                  }
                  #endif
                  #endif
                  • 6. Re: Pass enum argument to Java method
                    jschellSomeoneStoleMyAlias
                    raxtolis wrote:
                    Can you please provide the command line example for generating the header file. When I use the javah -jni MyTest.Shape the header file is generated but does not contain any #defines or other references
                    I would like to see such an example myself.

                    I took the other comment at face value but tried it in response to your question and nothing I did seemed to get java enum values to appear.

                    As I suggested however there are examples about how one can access enum values.
                    • 7. Re: Pass enum argument to Java method
                      EJP
                      Sorry guys I am crazy. I finally checked my source code from 3 years ago and it is full of kludges to get this to happen:
                      public enum LoopMode
                      {
                        Loop,
                        Dispatch;
                      }
                      /**
                       * These are just here to cause the corresponding JNI constants to be generated.
                       * Sadly that doesn't happen if we assign the ordinal values directly.
                       */
                      static final int     LOOP_MODE = 0,     // LoopMode.Loop.ordinal(),
                           DISPATCH_MODE = 1;     // LoopMode.Dispatch.ordinal();
                           
                      // ... so we add a sanity check.
                      static
                      {
                           assert LOOP_MODE == LoopMode.Loop.ordinal() : "LoopMode.Loop ordinal is broken";
                           assert DISPATCH_MODE == LoopMode.Dispatch.ordinal() : "LoopMode.Dispatch ordinal is broken";
                      }
                      
                      // This causes the following to be generated in the .h file:
                      #undef net_sourceforge_jpcap_capture_JWinPcap_LOOP_MODE
                      #define net_sourceforge_jpcap_capture_JWinPcap_LOOP_MODE 0L
                      #undef net_sourceforge_jpcap_capture_JWinPcap_DISPATCH_MODE
                      #define net_sourceforge_jpcap_capture_JWinPcap_DISPATCH_MODE 1L
                      My apologies. I thought this was automatic.