0

現在のメソッドのすべての注釈を取得したいので、method から注釈を取得できるようにメソッド インスタンスが必要です。

現在のメソッド名を取得できますが、メソッドまたはメソッド インスタンスの完全修飾名が必要です。

コールバックなど、他のオブジェクト指向ソリューションにも興味があります。

4

1 に答える 1

2

以下のコードはあなたのために働くはずです:

    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();
于 2013-10-18T13:45:23.723 に答える