我正在尝试从处理中做一个简单的Class.newInstance(),但我失败了:(
EDIT: as below instructed, the reason it didn’t work was that Processing wraps all
code into a class making all declared classes internal classes. The java
language specification states that newInstance() for internal classes
should be passed a reference to the container class, so after changing
the call to newInstance to newInstance(this); it worked as intended.
下面抛出InstantiationException的代码示例.
Docs说有一个公共构造函数,一切都会好的,但唉.
运行处理2.2.1
代码流:它填充了实现接口的所有类的列表.然后我们通过state.runnableTypes并尝试(但失败)实例化该类型的新实例.
请帮忙?
(对于笨重的状态变量道歉,与偶数笨重的ryo单元测试系统有关;))
State state;
/* It nicely finds the Wtf class and inserts that into its classes array,
* but when i try to instantiate a new one, whatever I try, it throws :(
*/
void setup() {
size(1024, 768, P3D);
noLoop();
//get all the classes
state = new State();
Class[] types = this.getClass().getDeclaredClasses();
state.allTypes = types;
//enumerate castable types
for (int i = 0, l = types.length; i
Class c = types[i];
if ( !c.isInterface() && IMenuRunnable.class.isAssignableFrom(c) ) {
println("adding "+c.toString());
state.runnableTypes.put(c.getSimpleName(), c);
}
}
//loop through the list and create some instances //FIXME: Exception handling ;p
for ( String s : state.runnableTypes.keySet () ) {
Class c = state.runnableTypes.get(s);
IMenuRunnable u = (IMenuRunnable) c.newInstance(); // ERR: calling Class.newInstance() here throws an error??
java.lang.reflect.Constructor[] ctors = c.getConstructors();
for ( java.lang.reflect.Constructor ctor : ctors ) {
ctor.setAccessible(true);
Object o = ctor.newInstance(); // ERR: going via constructor array, same error :(
}
}
}
void draw() {
background(0);
}
class State {
Class[] allTypes;
HashMap runnableTypes = new HashMap();
}
interface IMenuRunnable {
}
public class Wtf implements IMenuRunnable {
public Wtf() {
}
}
最佳答案 首先,您的代码无法编译,因为您需要在try / catch块中包装newInstance(). newInstance()函数可以抛出InstantiationException或IllegalAccessException,因此您必须捕获这些异常.这就是你的错误告诉你的.
如果你发布一个MCVE,你会有更好的运气,比如这个:
void setup() {
try {
Class c = Wtf.class;
IMenuRunnable u = (IMenuRunnable) c.newInstance();
println(u.toString());
}
catch(Exception e) {
e.printStackTrace();
}
}
interface IMenuRunnable {}
public class Wtf implements IMenuRunnable{
public Wtf() {
}
}
但即使你修复了它,你也会得到一个运行时异常,因为在幕后,你在草图中声明的类是sketch类的内部类.你不能像你想要的那样在内部类上使用反射.
因此,您需要更改声明这些类的方式,或者需要更改您进行反射的方式.
要确保Processing不会将这些类转换为草图的内部类,您必须在它们自己的.java选项卡中定义它们.使用类的名称后跟.java,因此Processing知道它是一个Java类,而不是“Processing class”,它被转换为内部类.您的设置如下所示:
使用这种方法,您必须将草图的实例传递给java类,以便访问任何Processing方法.相反,您可能只想改变您的反思方式.由于类是sketch类的内部类,因此必须通过sketch类来获取它们:
void setup() {
try {
Class> sketchClass = Class.forName("sketch_150702a");
Class> innerClass = Class.forName("sketch_150702a$Wtf");
java.lang.reflect.Constructor constructor = innerClass.getDeclaredConstructor(sketchClass);
IMenuRunnable u = (IMenuRunnable)constructor.newInstance(this);
println(u.toString());
}
catch(Exception e) {
e.printStackTrace();
}
}
interface IMenuRunnable {
}
public class Wtf implements IMenuRunnable {
public Wtf() {
}
}
或者,您可以将Wtf类声明为static:
void setup() {
try {
Class c = Wtf.class;
IMenuRunnable u = (IMenuRunnable) c.newInstance();
println(u.toString());
}
catch(Exception e) {
e.printStackTrace();
}
}
interface IMenuRunnable {}
static public class Wtf implements IMenuRunnable{
public Wtf() {
}
}
当然,那么您将无法访问草图类的非静态成员,因此您采用哪种方法实际上取决于您需要做什么.