I have an Enum called Plugins:
public enum Plugins {
ROTATING_LINE (plugin.rotatingline.RotatingLine.class),
SNOW_SYSTEM (plugin.snow.SnowSystem.cla
Create an interface for the plugins
public interface Plugin{
setShapeType();
setX();
setY();
...
}
Create a method on the enum that creates an instance of Plugin.
public enum Plugins {
ROTATING_LINE (plugin.rotatingline.RotatingLine.class),
SNOW_SYSTEM (plugin.snow.SnowSystem.class);
private Class<? extends Plugin> c;
private Plugins (Class<? extends Plugin> c) {
this.c = c;
}
// This acts as a constructor that takes args, I am assuming the args you need
public Class<? extends Plugin> createInstance(String shapeType,int x, int y) {
Plugin plugin = c.newInstance();
plugin.setShapeType(shapeType);
plugin.setX(x);
plugin.setY(y);
return plugin;
}
}
Instantiate in loop
List<Plugin> myPlugins = new ArrayList<Plugin>();
for (Plugins plugins : Plugins.values()) {
myPlugins.add(plugin.createInstance(Shaped.parent, 400, 400));
}
Please note this is Psuedo Code
There are 2 ways:
use Enum.valueOf()
static function, then cast it into your enum type.
Enum v = Enum.valueOf(TheEnumClass, valueInString);
Use class.getEnumConstants()
function to get the list of the enum constants, and loop this list and get.
Plugins[] plugins = Plugins.class.getEnumConstants();
for (Plugins plugin: plugins) {
// use plugin.name() to get name and compare
}