Using Inner-Classed Enums with ColdFusion and Java

Written by Russell on . Posted in App Dev, Gotchas

I recently had to call a method from Java that required an Enum type. In my case I did not have control over the java code and the Enums were stored inside a Class called Enums. It was not readily apparent to me on how to access these inner-classed Enums and there wasn’t any specific documentation that I could find on the topic. I struggled a bit with trying to make a string work (the true end result), using JavaCast and trying to instantiate the class and Enum directly. Thankfully the final solution was actually easier than expected.

First we need to get the Enum from the Java Class.
<cfset local.FULL = createObject("Java", "com.my.path.datatypes.Enums$RegistrationResultsFormat").FULL />

Pay close attention to “$RegistrationResultsFormat”. This resolves to my Enum “public enum RegistrationResultsFormat { … }”.

If you perform a CFDUMP on the results of the createObject you will get a list of your possible options. In my case, RegistrationResultsFormat had FULL, COURSE and ACTIVITY. It is important to note that a CFDUMP of both the core createObject as well as the results of .FULL are exactly the same. Do not fret, you are still getting the desired end result and you may use it as is.

Next we need to pass the Enum value into that Java method. In my example it is the second argument.
<cfset local.results = anotherJavaMethod( javaCast("string", arguments.id), local.FULL) />

In my actual use case, I stored this result in the variables scope of my component and got the value inside the init method.

Russell

I am a rich internet application developer with 15 years of experience as a technical and team leader. I have extensive experience with enterprise level application development and project management with a strong proficiency in a wide variety of languages and technologies. My development focus of recent has been on delivering high end rich UI applications by pushing the limits of both front end and server side technologies such as Node.JS, Java and Python.