You can use a switch statement in your JNI code as well, if you:
- Provide an integral value field in your Java enum class.
- Define a parallel set of integral constants in C or C++ (e.g., by another enum).
The redundant definition introduces a risk of divergence. You can mitigate this by:
- Heavily documenting the parallelism on both sides. This works best if the enumeration is small and changes infrequently.
- Generating the code from a single source.
For example, in Java, you could have:
public enum Foo {
FIRST(0),
SECOND(1);
public int getValue() { return m_value; }
private int m_value;
private Foo( int value ) { m_value = value; }
}
And in C++, you could have:
enum Foo {
FIRST = 0,
SECOND = 1
};
For a parallel enumeration, I personally always make the enum values explicit on the C/C++ side. Otherwise, a deletion of an enumerator on both sides can cause the values to diverge.