Return enum from jni?

Discussion in 'Java' started by John Smith, Aug 18, 2005.

  1. John Smith

    John Smith Guest

    I'm in the process of creating a wrapper for my existing C library to make
    it usable from java.

    Lets say you have a native function which return an integer (part of this
    library) which I want to call from my jni function.

    This function has the prototype of:

    typedef int X_STATUS;
    X_STATUS f1(...);

    Then the possible errorcodes are encoded into an enum like (on the java
    side):

    public enum Errors
    {
    A,
    B,
    C
    }


    So now when I declared my native function in java it wants jobject as return
    type. This jobject referes to the enum which it expects. But this is
    actually rather silly since it requires so much more work to create the enum
    on the native side as return value then just return the integer directly.

    So is it possible to make it return the integer and then typecast this value
    on java side to the enum errorcode? Essentially the two things are the same
    but it was not clear from the JNI documentation how to create this errorcode
    on the native side and make it into a jobject.

    What I tried was also to make the native function return an 'int' and cast
    that into the Errors enum but java does not seem to like these type of
    typecasts.

    Thanks in advance.
    -- John
    John Smith, Aug 18, 2005
    #1
    1. Advertising

  2. John Smith

    Chris Uppal Guest

    John Smith wrote:

    > public enum Errors
    > {
    > A,
    > B,
    > C
    > }
    > [...]
    > So is it possible to make it return the integer and then typecast this
    > value on java side to the enum errorcode?


    The Java compiler creates a public method as part of each enum class. In the
    above case it would have signature

    public static Errors[] values();

    which returns a (newly created each time) Array of the enum's elements in the
    order that they were declared. If your C code's values happen to be suitable
    then you could index directly into such an array, otherwise you could easily
    build a little table mapping integer error codes onto the corresponding Errors.

    It also creates a
    public static Errors valueOf(String name);
    method, but I mention that only for completeness.

    BTW <unsolicited code advice alert/> you would probably find that your code
    read better if the enun type were named Error, rather than Errors. An enum is
    a single class with multiple instances so, just as a dog class would be called
    "Dog", not "Dogs", you have a class Error with instances called A, B, and C.

    -- chris
    Chris Uppal, Aug 18, 2005
    #2
    1. Advertising

  3. John Smith

    Roedy Green Guest

    On Thu, 18 Aug 2005 10:10:05 +0200, "John Smith"
    <> wrote or quoted :

    >I'm in the process of creating a wrapper for my existing C library to make
    >it usable from java.
    >
    >Lets say you have a native function which return an integer (part of this
    >library) which I want to call from my jni function.
    >
    >This function has the prototype of:
    >
    >typedef int X_STATUS;
    >X_STATUS f1(...);
    >
    >Then the possible errorcodes are encoded into an enum like (on the java
    >side):
    >
    >public enum Errors
    >{
    >A,
    >B,
    >C


    to understand how you might make that work in JNI, I suggest you look
    at how it is implemented. I have posted several decompilations that
    will explain what goes on under the hood.

    See http://mindprod.com/jgloss/enum.html
    Roedy Green, Aug 18, 2005
    #3
  4. John Smith

    John Smith Guest

    > The Java compiler creates a public method as part of each enum class. In
    the
    > above case it would have signature
    >
    > public static Errors[] values();
    >
    > which returns a (newly created each time) Array of the enum's elements in

    the
    > order that they were declared. If your C code's values happen to be

    suitable
    > then you could index directly into such an array, otherwise you could

    easily
    > build a little table mapping integer error codes onto the corresponding

    Errors.
    >


    Thanks it works perfect.

    I did the following:

    Error.values()[Init(...)];

    The native function just returns a 'int' and then convert it.

    > BTW <unsolicited code advice alert/> you would probably find that your

    code
    > read better if the enun type were named Error, rather than Errors. An

    enum is
    > a single class with multiple instances so, just as a dog class would be

    called
    > "Dog", not "Dogs", you have a class Error with instances called A, B, and

    C.
    >


    Thanks for the suggestion. I will take it into account for future use. Right
    now it's a little hard since the software is already deployed but it might
    be an option for next release.

    -- John
    John Smith, Aug 18, 2005
    #4
  5. John Smith

    John Smith Guest


    > to understand how you might make that work in JNI, I suggest you look
    > at how it is implemented. I have posted several decompilations that
    > will explain what goes on under the hood.
    >
    > See http://mindprod.com/jgloss/enum.html


    Thanks for the article.

    -- John
    John Smith, Aug 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    533
  2. Jerminia
    Replies:
    3
    Views:
    622
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    457
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    414
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    516
    David Harmon
    Jan 7, 2006
Loading...

Share This Page