我想获取当前方法的所有注释,为此我需要方法实例,以便我可以 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/