Use JAR library enum type in Python app

This article provides an introduction to cross-technology handling of enum type. In programming, an enum (short for enumeration) type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Enums are used to create our own data types, just like classes. They are particularly useful when we want to represent a fixed set of constants, such as days of the week, states, colors, directions, and more. This makes the code more readable and less prone to errors.

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. If have not yet created your first project check Javonet overview and quick start guides for your technology.

With Javonet, you can interact with enum types from JAR library as if they were available in Python, but interaction must be performed through the Javonet SDK API. This allows you to handle and manipulate enum values, pass them to methods, and return them from methods, all while maintaining the type safety and readability of your Python code.

Custom JAR library with enum type

With Javonet it is possible to reference any custom JAR library and interact with its methods declared on types defined within that module almost the same as with any other Python library.

Snippet below represents the sample code from JAR library which contains enum type.

public enum Fruit {
    Apple,
    Banana,
    Orange,
    Mango
}

private static java.util.ArrayList<Fruit> Fruits = new java.util.ArrayList<Fruit>();

public static String addFruitsToList(Fruit[] fruits) {
    Fruits.addAll(java.util.Arrays.asList(fruits));
    return String.format("%d fruits on the list", Fruits.size());
}

Javonet SDK contains various methods to interact with enums and consume the results in Python:

# use activate only once in your app
Javonet.activate("your-license-key")

# create called runtime context
called_runtime = Javonet.in_memory().jvm()

# set up variables
library_path = resources_directory + '/TestClass.jar'

# load custom library
called_runtime.load_library(library_path)

# get enum
enum_type = called_runtime.get_type("TestClass$Fruit")

# create enum items
fruit1 = called_runtime.get_enum_item(enum_type, "Mango")
fruit2 = called_runtime.get_enum_item(enum_type, "Orange")

# get items' names and values
fruit1_name = fruit1.get_enum_name().execute().get_value()
fruit2_name = fruit2.get_enum_name().execute().get_value()
fruit1_value = fruit1.get_enum_value().execute().get_value()
fruit2_value = fruit2.get_enum_value().execute().get_value()

# write result to console
print(f'{fruit1_name}: {fruit1_value}, {fruit2_name}: {fruit2_value}')

This snippet uses in memory runtime bridging to load the JAR library and next retrieves reference to specific enum type. Then two enum items are created. Their values and names are checked.

Enum items can be passed to JAR library method:

# use activate only once in your app
Javonet.activate("your-license-key")

# create called runtime context
called_runtime = Javonet.in_memory().jvm()

# set up variables
library_path = resources_directory + '/TestClass.jar'
class_name = 'TestClass'

# load custom library
called_runtime.load_library(library_path)

# get enum
enum_type = called_runtime.get_type("TestClass$Fruit")

# create enum items
apple = called_runtime.get_enum_item(enum_type, "Apple")
mango = called_runtime.get_enum_item(enum_type, "Mango")

# create fruits array
fruits_list = [apple, mango]

# get type from the runtime
called_runtime_type = called_runtime.get_type(class_name).execute()

# invoke type's static method
response = called_runtime_type.invoke_static_method("addFruitsToList", fruits_list).execute()

# get value from response
result = response.get_value()

# write result to console
print(result)

In this example, two enum items are created, too. Then array of enums is created and passed to method which adds enum items to list.

The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your JAR library objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.

Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.