You can create your enum
with a constructor so you can assign it the value you want, then use a JNI call to get the value for that enum. Something like this in Java (note that I have not tested this code at all, but I have written similar code in the past to do exactly what you need):
public class TestEnum
{
public enum Dummy
{
A( getA() );
B( getB() );
MAX( getMax() );
private final int value;
Dummy( int newValue )
{
this.value = newValue;
}
int getValue()
{
return( this.value );
}
private static native int getA();
private static native int getB();
private static native int getMAX();
}
...
}
On the native side:
enum Dummy
{
A = 100;
B = 102;
MAX = 912343;
};
...
JNIEXPORT jint JNICALL Java_TestEnum_00024TEST_getA( JNIEnv *env, jclass cls )
{
return( A );
}
JNIEXPORT jint JNICALL Java_TestEnum_00024TEST_getB( JNIEnv *env, jclass cls )
{
return( B );
}
JNIEXPORT jint JNICALL Java_TestEnum_00024TEST_getMAX( JNIEnv *env, jclass cls )
{
return( MAX );
}
Make sure you use javah
to generate your native function signatures and #include
the generated header in your code.
Or, you can pass something else to the native function to identify the enum value to return, such as a string that maps to the actual enum value:
public class TestEnum
{
public enum Dummy
{
A( get( "A" ) );
B( get( "B" ) );
MAX( get( "MAX" ) );
private final int value;
Dummy( int newValue )
{
this.value = newValue;
}
int getValue()
{
return( this.value );
}
private static native int get( String enumName );
}
...
}
and on the native side:
JNIEXPORT jint JNICALL Java_TestEnum_00024TEST_get( JNIEnv *env, jclass cls, jstring enumName )
{
char *cEnumName = (*env)->getStringUTFChars( env, enumName, NULL );
jint retVal = -1;
if ( 0 == strcmp( cEnumName, "A" ) )
{
retVal = A;
}
else if ( 0 == strcmp( cEnumName, "B" ) )
{
retVal = B;
}
else if ( 0 == strcmp( cEnumName, "MAX" ) )
{
retVal = MAX;
}
(*env)->ReleaseStringChars( env, cEnumName );
return( retVal );
}
You're still going to have to deal with some O&M overhead, but this way the actual value is only defined in one place. In this case, it's in the native code.