有了这个,我从以前用URLClassLoader加载的一些随机类中获取方法集合:
public static Collection<Method> getMethods(Class<?> clazz, String packageName) {
Collection<Method> found = new ArrayList<Method>();
while (clazz != null) {
for (Method m1 : clazz.getDeclaredMethods()) {
boolean overridden = false;
for (Method m2 : found) {
if (m2.getName().equals(m1.getName())
&& Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) {
overridden = true;
break;
}
}
if (!overridden)
if (m1.getParameterTypes().length != 0) {
if (m1.toString().contains(packageName)) {
found.add(m1);
}
}
}
clazz = clazz.getSuperclass();
}
return found;
}
之后我想获得该方法的方法名称,参数(参数)(名称,类型和值)。换句话说,我需要访问这些方法。制作插件。 试过这样:
/* Find class methods with input arguments */
foundMethods = getMethods(clazz, packageName);
/* Print class name and methods */
for (Method itterator : foundMethods) {
tempIndex++;
out.println("Class name: " + className + "Method [" + tempIndex + "] = " + itterator.getName() + "\n");
Parameter[] parameters = itterator.getParameters();
for(Parameter parameter : parameters)
{
System.out.println(parameter.getName());
}
}
但我只获得了类名,方法名和参数,但是像arg0,arg1 ...... 有什么想法怎么做?