Return enum from jni?

J

John Smith

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
 
C

Chris Uppal

John said:
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
 
R

Roedy Green

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
 
J

John Smith

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top