Hariharan
Hariharan

Reputation: 881

Fully qualified method name of the current method

I want to get all the annotations of the current method, for that I need method instance so that I can get annotation from method.

I am able to Get Current Method name, but I need fully qualified name of the method or method instance.

I am also interested in other Object oriented solutions, for example call back.

Upvotes: 0

Views: 2334

Answers (1)

Rahul Bobhate
Rahul Bobhate

Reputation: 5092

The below code should work for you:

    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();

Upvotes: 2

Related Questions