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

发布于 2024-11-02 19:24:19 字数 562 浏览 1 评论 0原文

问题很简短:有没有办法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-11-09 19:24:19

你的方法没有错,但是还有更好的方法。您必须转换为 方法签名

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

Your method is not wrong, but there's a better one. You have to cast to MethodSignature

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
差↓一点笑了 2024-11-09 19:24:19

你应该小心,因为 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中的代码是自愿为空,你最好添加代码来管理异常)

有了这个,如果你想访问方法或参数注释(如果接口中没有这个注释),你就可以实现

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:

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

(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文