java - 当前方法的完全限定方法名称

标签 java reflection callback

我想获取当前方法的所有注释,为此我需要方法实例,以便我可以 get annotation from method .

我能够Get Current Method名称,但我需要方法或方法实例的完全限定名称。

我也对其他面向对象的解决方案感兴趣,例如回调。

最佳答案

下面的代码应该适合您:

    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

    // The 2nd element of the array is the current method
    String className = stackTrace[1].getClassName();
    Class clazz = Class.forName(stackTrace[1].getClassName());

    String methodName = stackTrace[1].getClassName()+"."+stackTrace[1].getMethodName();
    String simpleMethodName = stackTrace[1].getMethodName();

    // This will work only when there are no parameters for the method
    Method m = clazz.getDeclaredMethod(simpleMethodName, null);

    // Fetch the annotations 
    Annotation[] annotations = m.getDeclaredAnnotations();

关于java - 当前方法的完全限定方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444609/

相关文章:

java - 关于反射包的问题

java - hibernate : @SecondaryTable doesn't work

c# - 我如何动态引用一个程序集来寻找另一个程序集?

javascript - Node.js 中的 "done"回调函数是什么?

c# - 使用 Steamworks.NET 包装器时不会触发回调

java - 使用 JPA 在 Spring 中进行事务和回滚

java - Uncaught ReferenceError : Define is not defined in Ember/Springboot

spring - Spring 如何在没有调试信息的情况下获取参数名称

c# - 如何检查两个对象的属性是否相等

c - 指针函数返回值问题