You can replace that w/ Class::getDeclaredConstructor() + Constructor::newInstance():
- Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...)
- Constructor (Java SE 11 & JDK 11 )
I’m basing this new alternative version from these 2 old forum threads:
- String to Class: ClassNotFoundException - Processing 2.x and 3.x Forum
- [SOLVED] Instantiating a particular class constructor using reflection - Coding Questions - Processing Foundation
// https://Discourse.Processing.org/t/
// how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/5
// GoToLoop (2020/Aug/02)
static final Class[] dumbClasses = {
Dumb.class, Dummy.class, Dumber.class
};
final Class<? extends PApplet> sketchClass = getClass();
void draw() {
final Class<Dumb> dumbClass = dumbClasses[(int) random(dumbClasses.length)];
final Dumb d = getDumb(dumbClass);
print(d, TAB);
noLoop();
background((color) random(#000000));
}
void mousePressed() {
redraw();
}
<T> T getDumb(final Class<T> dumbClass) {
try {
return dumbClass.getDeclaredConstructor(sketchClass).newInstance(this);
}
catch (final ReflectiveOperationException e) {
System.err.println(e);
return null;
}
}
class Dumb {
@Override String toString() {
return getClass().getSimpleName();
}
}
class Dummy extends Dumb {
}
class Dumber extends Dumb {
}