Using enum type
To use .NET enums in your Java project, javonet API provides special NEnum type.
Using this class you can keep the reference of particular enum value, get/set the enum value, pass the enum value as method argument or compare enum values.
To initialize reference to .NET enum type you can use the NEnum(String enumTypeName, String enumValue) constructor. As first argument you must provide enum type name with or without namespace.
When namespace is not provided Javonet will automatically lookup the enum type in all loaded assemblies, if there is only one type with provided name it will be used.
In second argument selected enum value should be provided.
Assuming we have a custom JAR library with the following enum inside:
Enum usage:
How to pass enum as method argument:
Assuming we have a custom JAR library with the following class inside:
public class TestClass {
public TestClass() {
}
public static int MyStaticField;
public int MyInstanceField;
public static String SayHello(String name) {
return "Hello " + name;
}
public static int MethodExpectingPrimitiveInt(int arg) {
return arg * 2;
}
public static int MethodExpectingClassInteger(Integer arg) {
return arg * 2;
}
public int MultiplyByTwo(Integer arg) {
return arg * 2;
}
public <T> T MyGenericMethod(T arg1)
{
return arg1;
}
}
To use enum as method argument:
Was this article helpful?