RFR: Generate Java enums
Vivek Narang
duke at openjdk.org
Wed Jul 2 21:30:26 UTC 2025
We are using jextract in our [cuVS Java](https://github.com/rapidsai/cuvs/tree/branch-25.08/java) project. As of now, jextract generates individual getter functions for the C enums. This results in code where it is difficult to disambiguate which getter functions are logically grouped together for a particular Enum. Additionally, given the lack of this capability, we also have to manually create and maintain Java enums for each enum in the C layer.
In this PR, I wish to introduce an option to additionally generate Java enums by passing an optional flag `--generate-java-enums`. In case a Java user wishes to use bitwise operations on the enums, they can use the enum's `.ordinal()` method for this. The original static getter methods will still be generated as before.
Example C enum:
enum SIZE {
S,
M,
L
};
Produces the following with jextract:
private static final int S = (int)0L;
public static int S() {
return S;
}
private static final int M = (int)1L;
public static int M() {
return M;
}
private static final int L = (int)2L;
public static int L() {
return L;
}
With this feature enabled using the `--generate-java-enums` flag, the following enum is generated (in addition to the above):
public enum Size {
S(0),
M(1),
L(2);
private final int value;
private Size(int value) {;
this.value = value;
}
public int getValue() {
return this.value;
}
}
This PR is created against the `jdk22` branch. I will rebase it to `master` branch if needed.
-------------
Commit messages:
- Remove extra space in messages.properties
- Generate enums in header file instead
- Initial work
Changes: https://git.openjdk.org/jextract/pull/284/files
Webrev: https://webrevs.openjdk.org/?repo=jextract&pr=284&range=00
Stats: 184 lines in 12 files changed: 168 ins; 0 del; 16 mod
Patch: https://git.openjdk.org/jextract/pull/284.diff
Fetch: git fetch https://git.openjdk.org/jextract.git pull/284/head:pull/284
PR: https://git.openjdk.org/jextract/pull/284
More information about the jextract-dev
mailing list