我正在研究一种包裹扫描。这可能不是最好的路线,但我在谷歌上找到的解决方案似乎比我需要的要多得多。
我正在努力实例化一个类,这让我发疯了。这是我要实例化的代码:
String[] packageList = {
getPackageName(),
"com.socket.event.handler"
};
for (String packageListName : packageList) {
try {
String className = this.getType(); // getDashboard
className = className.substring(0, 1).toUpperCase() + className.substring(1)+"Handler"; //GetDashboardHandler
clazz = Class.forName(packageListName+"."+className) //shows correct package/class name
.asSubclass(EventHandler.class);
return (EventHandler) clazz.getConstructor(clazz.getClass()).newInstance(getData());
//return (EventHandler) clazz.newInstance();
} catch (ClassNotFoundException e) {
//swallow, this is ok
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
那里使用的getData()
看起来像这样: public HashMap getData() { return data; }
被实例化的类是:
public class GetDashboardHandler extends EventHandler<GetDashboardHandler> {
public String workstationUuid;
public GetDashboardHandler() {
super();
}
public GetDashboardHandler(HashMap<String, Object> data) {
super(data, GetDashboardHandler.class);
}
我有一个默认构造函数和一个带有参数以满足特定需求的构造函数。我仍然看到这个异常:
java.lang.NoSuchMethodException: com.socket.event.handler.GetDashboardHandler.<init>(java.lang.Class)
at java.lang.Class.getConstructor0(Class.java:2730)
at java.lang.Class.getConstructor(Class.java:1676)
at com.socket.SocketMessage.getEventHandler(SocketMessage.java:88)