How to use java reflection with Mendix?

When we create an enumeration under Mendix, the plateform build in the module package, an enum class (in a proxies subfolder). I would like extract directly, with a java action, the label of an enumeration in a specific language. So, i send two String arguments : the enumeration (myModule.MyEnum.NAME) and the language target (frCA or enCA). In java, the code is : package enumerations; import java.lang.reflect.Field; import java.lang.reflect.Method; import static java.lang.System.out; public class GetTranslatedEnumerationLabel { public static void main(String... args) { try { String className = null; String enumValue = null; String language_ISO639 = "en_CA"; if(args!=null && args.length>0) { // or String myTable[] = args[0].split("\\."); // Separate the class name and the enum value. In Mendix we should add *proxies* subfolder.... className = args[0].substring(0, args[0].lastIndexOf('.')); enumValue = args[0].substring(args[0].lastIndexOf('.')+1); } else { return; } Class<?> clazzMendixEnum = Class.forName(className); if (!clazzMendixEnum.isEnum()) { out.format("%s is not an enum type%n", clazzMendixEnum); return; } Field mendixField = getFieldClass(clazzMendixEnum, enumValue); Method m = clazzMendixEnum.getMethod("getCaption", String.class); m.setAccessible(true); System.out.println(m.invoke(mendixField.get(null), "fr_CA")); System.out.println(m.invoke(mendixField.get(null), language_ISO639)); } catch (ClassNotFoundException x) { x.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } public static Field getFieldClass(Class<?> clazz, String name) { if (clazz==null || name==null || name.isEmpty()) { return null; } for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); if (field.getName().equalsIgnoreCase(name)) { return(field); } } return(null); } } How we do that with Mendix ?
2 answers

Perhaps I don't understand what you are trying to do, but to get the label of an enumeration value, you could use the following code (where inboekingStatus is an enumeration and language is a System.Language object):

    return inboekingStatus.getCaption(language.getCode());

Your code is already achieving what you desire. I would however change your enum class parameter from string to an enum that contains all possible enums that you want to pass to this JavaAction, so that typing mistakes can't occur and future refactoring is easy. Example: alt text

Then your code already works (abridged version):

            String enumerationParam = "mymodule_proxies_myenum";    //Example of first parameter
                                                                    //caption will be mymodule.proxies.myenum
            String localeParam = "en_US";
            Class<?> clazzMendixEnum = Class.forName(campaign.proxies.Enumerations.valueof(enumerationParam));
            Field mendixField;

            for (Field field : clazzMendixEnum.getDeclaredFields()) {
                if (field.getName().equalsIgnoreCase("String")) {
                    mendixField = field;
                    Method m = clazzMendixEnum.getMethod("getCaption", String.class);

                    // Return locale version of enum caption
                    return (String)m.invoke(mendixField.get(null), localeParam);
            return null;

Ideally though, Mendix should provide this as a first-rate microflow expression function, similar to getCaption but with a locale as a second argument.