Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Generating Enumeration Types at runtime

Thread view: 
Markus Schickler - 04 May 2007 17:22 GMT
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
a24900@googlemail.com - 04 May 2007 19:04 GMT
> 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.
Markus Schickler - 04 May 2007 19:45 GMT
>> 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.
Arne Vajhøj - 09 Jun 2007 18:39 GMT
> 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
Tom Hawtin - 09 Jun 2007 18:50 GMT
>> 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
Arne Vajhøj - 09 Jun 2007 19:09 GMT
>>> I was thinking there might be an easy way to generate Enumeration
>>> classes on the fly - In that case I could have used Java
[quoted text clipped - 6 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.