从ProceedingJoinPoint获取java.lang.reflect.Method?

问题是简短的:有没有办法从apsectj ProceedingJoinPoint获取Method对象?

目前我在做

Class[] parameterTypes = new Class[joinPoint.getArgs().length]; Object[] args = joinPoint.getArgs(); for(int i=0; i<args.length; i++) { if(args[i] != null) { parameterTypes[i] = args[i].getClass(); } else { parameterTypes[i] = null; } } String methodName = joinPoint.getSignature().getName(); Method method = joinPoint.getSignature() .getDeclaringType().getMethod(methodName, parameterTypes); 

但我不认为这是要走的路

你的方法没有错,但有一个更好的方法。 您必须转换为MethodSignature

 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); 

您应该小心,因为Method method = signature.getMethod()将返回接口的方法,您应该添加这个以确保获得实现类的方法:

  if (method.getDeclaringClass().isInterface()) { try { method= jointPoint.getTarget().getClass().getDeclaredMethod(jointPoint.getSignature().getName(), method.getParameterTypes()); } catch (final SecurityException exception) { //... } catch (final NoSuchMethodException exception) { //... } } 

(catch中的代码是自愿的,你最好添加代码来pipe理exception)

有了这个,如果你想访问方法或参数注释,如果这个不在接口中,你将得到实现