我已经为Java reflection.What创建了一个Utility类,我的意思是如果方法名作为参数传递,那么它应该返回集合值。该实用程序类是在Eclipse插件项目(com.abc.utility)中创建的。我将这个实用程序类添加到另一个插件项目(com.abc.dao)中。现在,当我调用这个实用程序方法时,我得到的是ClassNotFoundException。我理解这个问题,我不想在com.abc.utility项目中添加对类的依赖。相反,com.abc.utility插件项目应该作为依赖项添加到其他项目中。
我不知道该怎么解决。你能帮我解决这个问题吗。
@SuppressWarnings({ "unchecked", "rawtypes" })
public <K>Collection<K> getCollection(T t, String methodName) {
Method[] methods =t.getClass().getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.equals(methodName)) {
String name2 = method.getReturnType().getName();
Object newInstance = null;
try {
newInstance = Class.forName(name2).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
e1.printStackTrace();
}
if (newInstance instanceof Collection) {
try {
return (Collection)method.invoke(t, null);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
return Collections.emptyList();
}https://stackoverflow.com/questions/41443945
复制相似问题