public class method1 {
private int f1( Object obj, int x)
throws NullPointerException
{
if (obj == null)
throw new NullPointerException();
return x;
}
// main方法
public static void main(String args[])
{ try { Class cls = Class.forName("method1");
Method methlist[] = cls.getDeclaredMethods();
for (int i = 0; i < methlist.length; i++)
{
Method m = methlist[i];
System.out.println("name = " + m.getName());
System.out.println("decl class = " + m.getDeclaringClass());
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++)
{
System.out.println( j + "#参数类型是:" + pvec[j],);
//如何在这里得到参数的名字,如:obj
}
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++)
{
System.out.println("异常#" + j + " " + evec[j]);
}
System.out.println("return type = " + m.getReturnType());
System.out.println("-----");
} } catch (Throwable e) { System.err.println(e); } }
}
请问:如何获取参数的名字,如obj?
private int f1( Object obj, int x)
throws NullPointerException
{
if (obj == null)
throw new NullPointerException();
return x;
}
// main方法
public static void main(String args[])
{ try { Class cls = Class.forName("method1");
Method methlist[] = cls.getDeclaredMethods();
for (int i = 0; i < methlist.length; i++)
{
Method m = methlist[i];
System.out.println("name = " + m.getName());
System.out.println("decl class = " + m.getDeclaringClass());
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++)
{
System.out.println( j + "#参数类型是:" + pvec[j],);
//如何在这里得到参数的名字,如:obj
}
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++)
{
System.out.println("异常#" + j + " " + evec[j]);
}
System.out.println("return type = " + m.getReturnType());
System.out.println("-----");
} } catch (Throwable e) { System.err.println(e); } }
}
请问:如何获取参数的名字,如obj?
不行啊,必须要得到参数的详细名
那你不可以使用反射机制了,你应该使用反编译的技术,而且必须保证在编译这个类的时候没有打开任何优化,否则是无法获得参数名称的。
正解,想得到参数名,你需要在 class 文件的常量池里面找,而不是用反射
你找找一些 aop 相关的东西,有一些可以动态修改 class 文件的,可能会得到名称
比如 cglib , bcel , javassist 这类的 lib