Eri*_*rik 45 aop spring aspectj spring-aop java-ee
问题很简单:有没有办法从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);
Run Code Online (Sandbox Code Playgroud)
但我不认为这是要走的路......
Boz*_*zho 87
你的方法没有错,但有一个更好的方法.你必须施展MethodSignature
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Run Code Online (Sandbox Code Playgroud)
小智 42
你应该小心因为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) {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
(catch中的代码是自愿为空的,您最好添加代码来管理异常)
如果您想要访问方法或参数注释(如果这个不在界面中),那么您将拥有该实现
| 归档时间: |
|
| 查看次数: |
28031 次 |
| 最近记录: |