1

I've defined a IDL which I am compiling via IDLJ.exe. The IDL contains various different enum types. I notice that after code generation into .java source files the enum types are not actually enums but just regular classes with a set of final constants of the same type of the class.

I notice multiple different IDL code generators do this same thing including RTI's rtiddsgen tool.

Is there any particular reason why code generators are not actually generating enums in Java using the java 'enum' keyword? Fields, constructors, and methods are supported inside enum types in java so I don't think that would be a restriction.

Could it be class hierarchy restrictions? I know IDLJ generates classes that extend CORBA utilities, but RTI's rtiddsgen doesn't extend anything like that. Yet they both don't use real enum types.

Jake Henry
  • 303
  • 1
  • 10
  • 2
    I know that there was an "enum pattern," I think in Josh Bloch's "Effective Java" book, that were similar to that, before enums were added in Java 1.4. It is possible these code generators were written back in that era and haven't been updated, or they are trying to support even very old versions of Java, or maybe there is a setting where you can specify the version of Java you want to target, and if set correctly, will generate proper enums. – David Conrad Jun 06 '22 at 19:07
  • 1
    Interesting. Yeah that's probably the reason. Pathetically, I wasn't even aware that enums weren't added to Java until then. Thanks for the pointer. I don't think there are any java version arguments I can pass to the code generators I've been using but I'll take a look. – Jake Henry Jun 06 '22 at 19:16

1 Answers1

3

The code generation tools that you have mentioned are based on OMG's IDL to Java Language Mapping specification. This spec mentions that "It is based upon versions of the Java JDK 1.1 and above" and therefore predates the use of native java enums.

OMG has a newer IDL4 to Java Language Mapping specification. In Table 7.1 Java Language Versions and Features it explicitly mentions the enumerations feature requiring java version J2SE 5.0. That spec is currently a beta release.

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
  • 1
    Cool. Thanks for the link to that spec. Someone made a .g4 grammar that complies to the IDL4 spec you linked above. So I'm messing around with it in ANTLR4 to see if I can make my own code generator for Java. LMK if you know of any existing code generator implementations that implement the IDL4 spec (even though its currently in beta). – Jake Henry Jun 10 '22 at 17:44