Generating Enumeration Types at runtime

  • Thread starter Markus Schickler
  • Start date
M

Markus Schickler

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
 
A

a24900

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.
 
M

Markus Schickler

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Markus said:
(e-mail address removed) 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
 
T

Tom Hawtin

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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


BCEL is evil. ASM is much better.
:)


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top