Get enumeration in Java

1
How can I retrieve an enumeration in Java, e.g. based on a string? Currently, I use the following code: String enumName = "module.proxies.EnumerationName"; Class enumClass = Class.forName(enumName); if (enumClass.isEnum()) { doSomething(); } What I am looking for is some kind of neater, more Mendix like solution: e.g. for microflows, I can call Core.getMicroflowNames() and filter to find the microflow. Is there such a method for enumerations? Alternatively, a TypeParameter for a Java action of AnyEnumeration would work as well. Edit To be more specific: I want to have a Java action with two parameters: AnyEnumeration (in the above code example, this is the string enumName) String, microflow name This Java action will then iterate over every member of the enumeration in AnyEnumeration and execute the microflow for each member.
asked
4 answers
3

For a pretty flexible solution you could pass 3 strings, one as the name of the entity containing the enumeration and the other the actual enumeration attribute name and last the microflow name.

String 1 EntityContainingEnumAttribute format 'ModuleName.EntityName'

String 2 EnumerationAttributeName format 'AttributeName'

String 3 MicroflowToExecute format 'ModuleName.MicroflowName'

    IMetaObject metaEntity = Core.getMetaObject(EntityContainingEnumAttribute);
    IMetaPrimitive primitive = metaEntity.getMetaPrimitive(EnumerationAttributeName);
    IMetaEnumeration metaEnum  = primitive.getEnumeration();
    // In case the attribute name passed was not an enumeration
    if(metaEnum == null)
        throw new MendixRuntimeException("'" + EnumerationAttributeName +  "' is not an enumeration type.");
    Map<String, IMetaEnumValue> enumValues = metaEnum.getEnumValues(); 
    for(String  key : enumValues.keySet()) {
            //Execute MicroflowToExecute for each key in enum. 
            //Didn't include this because perhaps you want to run the microflows async or whatever.
    }
answered
2

Rom, you could do the following:

Pass only the name of the microflow. As long as this microflow has only an enum as parameter, you can easily determine what enum this is in your Java action (by looking the microflow up in the Microflows generated Java class) and then call the microflow for every value of the enum.

It seems to me this achieves what you are trying to achieve.

answered
0

Proxies are available for that purpose and can easily create conversion java-actions like

    try {
        MyEnum rt = MyEnum.valueOf(value);
        return rt.toString();
    } catch (Exception e) {
        return null;
    }
answered
0

I could not find a way to get to an enum directly, however, Java interface IMetaObject can return the enum type. Maybe this is a nice new feature for MxModelReflection? The enum values are already processed, but the enum name itself not.

answered