从 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);
,但我不认为这是要走的路......
The question is short and simple: Is there a way to get the Method object from an apsectj ProceedingJoinPoint?
Currently I am doing
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);
but I don't think this is the way to go ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的方法没有错,但是还有更好的方法。您必须转换为
方法签名
Your method is not wrong, but there's a better one. You have to cast to
MethodSignature
你应该小心,因为
Method method =signature.getMethod()
会返回接口的方法,你应该添加这个以确保获取实现类的方法:(catch中的代码是自愿为空,你最好添加代码来管理异常)
有了这个,如果你想访问方法或参数注释(如果接口中没有这个注释),你就可以实现
You should be careful because
Method method = signature.getMethod()
will return the method of the interface, you should add this to be sure to get the method of the implementation class:(The code in catch is voluntary empty, you better add code to manage the exception)
With this you'll have the implementation if you want to access method or parameter annotations if this one are not in the interface