0
我正在進行一種包掃描。這可能不是最好的路線,但我在Google上找到的解決方案似乎比我需要的要多得多。無法實例化類,因爲構造函數
我正在努力實例化一個類,它驅使我堅果。這裏是我的代碼實例:
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()
使用有看起來像這樣: 公衆的HashMap的getData(){ 返回數據; }
的類實例化是:
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)