Search code examples
javareflectionannotationsstack-trace

Get method annotations by StackTrace


Is there any way to find out additional information about a method when u get exception (stacktrace)? I need exactly the annotations. How to get the method name and class name is understandably.


Solution

  • If you get a stack trace you can always get the information about the classes, its methods, as well as its annotations. You will need to write some extra code to get to that information; you will need to get the method from the stack trace element and then materialize a method with reflection and get its annotations.

    Here is some sample code that demonstrates how you can get the annotation information from a stack trace. The relevant code is in method printAnnotationsFromStacktrace():

    @Ignore
    public class SimpleTests2 {
    
        @Ignore
        @Deprecated
        public static void main(String[] args) throws ParseException, ClassNotFoundException {
            System.out.println(numUnique(new double[]{1.0, 1.0, 2.0, 3.0, 4.0, 3.0}));
        }
    
        @SuppressWarnings("test")
        private static int numUnique(double[] list) throws ClassNotFoundException {
            int unique = 0;
            for (int i = 0; i < list.length; i++) {
                boolean existsBefore = false;
                for (int j = i - 1; j >= 0; j--) {
                    if (list[i] == list[j]) {
                        existsBefore = true;
                        break;
                    }
                }
                if(!existsBefore) {
                    unique++;
                }
            }
            printAnnotationsFromStacktrace();
            return unique;
        }
    
        private static void printAnnotationsFromStacktrace() throws ClassNotFoundException {
            StackTraceElement[] stacktraces = Thread.currentThread().getStackTrace();
            for(StackTraceElement stackTraceElement : stacktraces) {
                Class<?> aClass = Class.forName(stackTraceElement.getClassName());
                System.out.println(aClass);
                printAnnotation("\t%s\n", aClass.getAnnotations());
                String methodName = stackTraceElement.getMethodName();
                Method[] methods = aClass.getMethods();
                for(Method method : methods) {
                    if(method.getName().equals(methodName)) {
                        System.out.printf("\t%s\n", method);
                        printAnnotation("\t\t%s\n", method.getDeclaredAnnotations());
                    }
                }
            }
        }
    
        private static void printAnnotation(String pattern, Annotation[] annotations) {
            for(Annotation annotation : annotations) {
                System.out.printf(pattern, annotation);
            }
        }
    }
    

    If you run this code, you will see a print out of the class names with corresponding annotations as well of the stack trace method together with its annotations. Something like this:

    class java.lang.Thread
        public java.lang.StackTraceElement[] java.lang.Thread.getStackTrace()
    class SimpleTests2
        @org.junit.Ignore(value="")
    class SimpleTests2
        @org.junit.Ignore(value="")
    class SimpleTests2
        @org.junit.Ignore(value="")
        public static void SimpleTests2.main(java.lang.String[]) throws java.text.ParseException,java.lang.ClassNotFoundException
            @org.junit.Ignore(value="")
            @java.lang.Deprecated(forRemoval=false, since="")