Generating Enumeration Types at runtime

Discussion in 'Java' started by Markus Schickler, May 4, 2007.

  1. Hi everyone!

    I need to generate enum types dynamically.
    I have the enumeration's name and its constant's names, and ordering.
    Now I want to generate and load a class (via reflection) that implements
    this type.

    Is there a way? Maybe even an easy one :) ?

    Any hints or pointer appreciated!

    TIA

    Markus
     
    Markus Schickler, May 4, 2007
    #1
    1. Advertising

  2. Markus Schickler

    Guest

    On May 4, 6:22 pm, Markus Schickler <> wrote:
    > I need to generate enum types dynamically.


    Bullshit. Your software architecture is broken. Fix it.

    > I have the enumeration's name and its constant's names, and ordering.
    > Now I want to generate and load a class (via reflection) that implements
    > this type.
    >
    > Is there a way? Maybe even an easy one :) ?


    Yes, but it is a rather pointless exercise. The rest of your
    application does not know about the names. How could it? You are
    making the names up at runtime, so how do you expect to be able to
    type code of the form

    DynamicallyGeneratedEnumType e =
    A_DYNAMICALLY_CHOSEN_NAME_MADE_UP_LATER;

    and get it compiled? You can't without a time machine. You would have
    to resort to reflection and some magic heuristics all over the code.
     
    , May 4, 2007
    #2
    1. Advertising

  3. wrote:

    >> I need to generate enum types dynamically.

    > Bullshit. Your software architecture is broken. Fix it.
    >
    > Yes, but it is a rather pointless exercise. The rest of your
    > application does not know about the names. How could it? You are


    I'm writing an interpreter for a language.
    It takes variables from the java environment and puts others back there.

    Enumerations are specified in the language to interpret. They can
    correspond to Enumerations in the Java environment (in which case the
    user has to register/map them of course) or purely used internally.

    I was thinking there might be an easy way to generate Enumeration
    classes on the fly - In that case I could have used Java Enumerations,
    if given; or simply have generated them.

    So now I have handle seperate cases, but that seems to be less ugly.
     
    Markus Schickler, May 4, 2007
    #3
  4. Markus Schickler wrote:
    > wrote:
    > I was thinking there might be an easy way to generate Enumeration
    > classes on the fly - In that case I could have used Java Enumerations,
    > if given; or simply have generated them.


    You can use a byte code generation tool like BCEL.

    Or if you are on Java 1.6 you can simply compile
    Java source code (you can do memory->memory instead
    of file->file).

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jun 9, 2007
    #4
  5. Markus Schickler

    Tom Hawtin Guest

    Arne Vajhøj wrote:
    > Markus Schickler wrote:
    >> wrote:
    >> I was thinking there might be an easy way to generate Enumeration
    >> classes on the fly - In that case I could have used Java Enumerations,
    >> if given; or simply have generated them.


    I'm sure it would be far easier to treat Java enums and non-Java enums
    as separate cases.

    > You can use a byte code generation tool like BCEL.


    BCEL is evil. ASM is much better.

    > Or if you are on Java 1.6 you can simply compile
    > Java source code (you can do memory->memory instead
    > of file->file).


    Well, you can compile Java with earlier versions of the JDK (or just
    tools.jar) anyway. 1.6 just gives a standardised interface for doing that.

    Tom Hawtin
     
    Tom Hawtin, Jun 9, 2007
    #5
  6. Tom Hawtin wrote:
    > Arne Vajhøj wrote:
    >> Markus Schickler wrote:
    >>> wrote:
    >>> I was thinking there might be an easy way to generate Enumeration
    >>> classes on the fly - In that case I could have used Java
    >>> Enumerations, if given; or simply have generated them.

    >
    > I'm sure it would be far easier to treat Java enums and non-Java enums
    > as separate cases.
    >
    >> You can use a byte code generation tool like BCEL.

    >
    > BCEL is evil. ASM is much better.


    :)

    >> Or if you are on Java 1.6 you can simply compile
    >> Java source code (you can do memory->memory instead
    >> of file->file).

    >
    > Well, you can compile Java with earlier versions of the JDK (or just
    > tools.jar) anyway. 1.6 just gives a standardised interface for doing that.


    Also memory->memory ?

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jun 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charlie

    enumeration types

    Charlie, Feb 8, 2005, in forum: VHDL
    Replies:
    4
    Views:
    566
    Charlie
    Feb 9, 2005
  2. Pleg

    Enumeration types and bits

    Pleg, Mar 7, 2006, in forum: VHDL
    Replies:
    5
    Views:
    847
  3. Torsten Bronger

    Generating data types automatically

    Torsten Bronger, Mar 12, 2005, in forum: Python
    Replies:
    5
    Views:
    388
    Torsten Bronger
    Mar 14, 2005
  4. puvit82
    Replies:
    4
    Views:
    763
    puvit82
    Feb 1, 2008
  5. Marcel Müller

    Extend enumeration types?

    Marcel Müller, Sep 13, 2009, in forum: C++
    Replies:
    4
    Views:
    1,069
    Pascal J. Bourguignon
    Sep 14, 2009
Loading...

Share This Page