I am writing a program to simulate cities from a game called Civilization 4. In order to do this I have several Enums to represent types of terrain, resources, improvements etc for each plot owned by said city.
The problem is I want to program to be compatible with Fan made mods which may add things to the Game that need to be accepted into my independant utility. So I thought of creating a Enum style class to hold the new types defined by the loaded mods (as Enums cannot be changed at runtime) which is created during runtime when the user enters in a mod to be loaded (which is a txt file that is parsed to read the new additions)
So is there a way to simulate Enums that are created and added to at runtime? I take it static member variables cannot be used as they are done before runtime…
You can make a
enumimplement an interface.This way you can have your defined values in the enum, but new values can be any class which implements the interface.
An alternative is that you generate or load the
enumat runtime using a byte code generator or the Compiler API. I wrote a library to make it easier to take a String and compile&load it.http://vanillajava.blogspot.co.uk/2010_11_01_archive.html