java - 获取调用方法的注解

标签 java reflection

这是我的代码示例:

class foo extends afoo{

    @HTTPPost
    returnClass runTransaction(RequestData req){
        return sendData(req, returnClass.class)
    }

    @HTTPGet
    returnClass runTransaction2(RequestData req){
        return sendData(req, returnClass.class)
    }
}


abstract class afoo {

public <T> T sendData(ARestMessage req, Class<T> returnClassType)
    //here i need the annotation of the calling method
}

基本上我正在构建一个非常复杂的消息传递系统,我想尽可能多地在注释中进行切换和配置。

是的,我知道那里有一些库(比如 Google 反射)可以使这更容易,但为了让我使用它们,我必须做 4-6 个月的文书工作并与企业架构师会面以获得批准使用它们。眼看着项目要在2个月内完成,我手忙脚乱。

所以我正在做的是创建注释,开发人员可以通过指示结果服务期望发送数据的方式来注释方法。这可能是 get、post、put 等。在所有服务类扩展的抽象类内部,是一个 senddata 方法。该方法必须能够找出调用它的方法,也就是调用它的方法是 runTransaction 还是 runTransaction2,因此 sendData 提取该方法注释,因此确切地知道如何将数据发送到服务。

现在我找到了这个(这是我的 sendData 方法中的第一行代码)

final Method callingMethod = this.getClass().getEnclosingMethod();

但它一直返回 null。我已经多次阅读有关它的 javadoc,但我不明白为什么它一直返回 null。

我知道我可以让父调用者使用堆栈,但我不想这样做,因为这个应用程序与另一个执行大量 AOP 工作的应用程序共享应用程序服务器内存。 AOP 的工作非常擅长以意想不到的方式弄乱堆栈,所以我宁愿使用直接反射来解决这个问题。

有谁知道为什么这个方法一直返回null?是因为它包含在一个抽象类中而不是我的 foo 类本身吗?有没有一种方法可以使用我更喜欢使用的技术来完成此任务?

谢谢

最佳答案

Class.getEnclosingMethod() 方法并不像您想象的那样。这是它的 Javadoc:

If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class. Returns null otherwise. In particular, this method returns null if the underlying class is a local or anonymous class immediately enclosed by a type declaration, instance initializer or static initializer.

具体来说,它返回在该方法的上下文中定义的匿名内部类的外部封闭方法。我没有在您的描述中的任何地方看到这些消息传递方法是从匿名/本地内部类调用的。这是代码示例(需要 jUnit):

import java.lang.reflect.Method;

import org.junit.Assert;
import org.junit.Test;

interface Introspector {
    public Method getEnclosingMethod();
}

public class Encloser {

    public Encloser() {
        super();
    }

    public Method noop() {

        final Introspector inner = new Introspector() {
            @Override
            public Method getEnclosingMethod() {
                return getClass().getEnclosingMethod();
            }
        };

        return inner.getEnclosingMethod();
    }

    @Test
    public void testEnclosingMethods() throws Exception {
        final Encloser encloser = new Encloser();
        Method method = encloser.getClass().getEnclosingMethod();
        Assert.assertNull(method);

        method = encloser.noop();
        Assert.assertNotNull(method);
    }
}

您当前的解决方案听起来很复杂。您是否计划沿着方法调用链向上走(顺便说一下,您只能通过转储堆栈跟踪来做到这一点)并在进行大量反射(reflection)后寻找注释?我预见到很多错误。坦率地说,采用某种构建器模式可能更适合您的场景。

关于java - 获取调用方法的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033845/

相关文章:

java - 如何获取 List 或 Set 的第一个元素?

java - Hibernate HT_ 临时表 ON JOINED 继承,从 Hibernate 3.4.0.GA 迁移到 5.1

java - 在已编译的 Java 类中重写方法调用

haskell - 以组合方式将异构提升类型反射回值

java - 如何从 Java 中的 TreeMap 中提取 BigDecimals?

java - 将参数传递给类,同时将类作为 `class literal` 传递

java - JTable setPreferredWidth 导致额外的列

reflection - 如何使用 Kotlin 中的默认构造函数参数值实例化对象?

java - 保留使用反射 Android Proguard 的类

reflection - 如何打开 reflect.Type?