Legend legend = ApplicationContext.getLegend();
TreePath[] treePath = getTreePath();
Method method = legend.getClass().getDeclaredMethod("getItems", TreePath[].class);
method.setAccessible(true);
method.invoke(legend, treePath);
在Legend.class中:
private List<LegendItem> getItems(TreePath[] treePath) {
List<LegendItem> items = new ArrayList();
if (treepath == null) {
return items;
}
for (int i = 0; i < treepath.length; i++) {
Object[] ob = treepath[i].getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)ob[(treepath[i].getPathCount() - 1)];
}
items.add((LegendItem)node.getUserObject());
return items;
}
为什么我
Java.lang.IllegalArgumentException:参数类型不匹配
我执行时?我将参数定义为TreePath[]
,我传递的参数是该类型的?!
并遵循警告:
类型TreePath []的方法调用的最后一个参数(Object,Object ...)与vararg参数类型并不完全匹配。转换为Object []以确认非varargs调用,或传递类型为Object的各个参数以进行varargs调用。
代表method.invoke(legend, treePath)
解决:
method.invoke(legend, new Object[] {treePath});