获取方法和参数是错误的-我不知道为什么?
public class PageIntercepter implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args = invocation.getArguments();
//全路径名
String classType = invocation.getThis().getClass().getName();
- ------------this line is wrong,but idont know how to get classType-------------
Class<?> clazz = Class.forName(classType);
String clazzName = clazz.getName();
//获取方法名称
String methodName = invocation.getMethod().getName();
//获取参数名称和值
Map<String,Object > nameAndArgs = this.getFieldsName(this.getClass(), clazzName, methodName,args);
System.out.println("参数名和值:" + nameAndArgs.toString());
//需用到javassit包
private Map getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException {
Map map = new HashMap();
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath);
CtClass cc = pool.get(clazzName);
System.out.println(cc);
//接口全路径
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
UMSLogger.info("*** PaginationHelper attr is null === ");
return null;
}
// String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < cm.getParameterTypes().length; i++) {
//paramNames即参数名
map.put(attr.variableName(i + pos), args[i]);
}
return map;
}
}