java原始的反射是不能获取方法菜蔬的名称的..这里借住了地方jar包
使用maven管理jar
org.javassist
javassist
3.18.1-GA
java示范代码
Class clazz = IExample.class;
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(clazz.getName());
CtMethod cm = cc.getDeclaredMethod("getName");
// 使用javaassist的反射方法获取方法的参数名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
// exception
}
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++)
paramNames[i] = attr.variableName(i + pos);
// paramNames即参数名
for (int i = 0; i < paramNames.length; i++) {
System.out.println(paramNames[i]);
}
} catch (NotFoundException e) {
e.printStackTrace();
}