Monday, November 7, 2016

Get caller class name from inherited static method using java

Base class to test caller call name extractor


package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
abstract class StaticMethodCallerClassName {
    static String returnCallerClassNameUsingThreadStackTrace() {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        return stackTraceElements[2].getClassName();
    }

    static String returnCallerClassNameUsingThrowable() {
        return new Throwable().getStackTrace()[2].getClassName();
    }

    static String returnCallerClassNameUsingSecurityManager() {
        Class[] classes = CallingClassManager.INSTANCE.getCallingClasses();
        return classes[classes.length - 2].getCanonicalName();
    }

    private static class CallingClassManager extends SecurityManager {
        static final CallingClassManager INSTANCE = new CallingClassManager();

        Class[] getCallingClasses() {
            return getClassContext();
        }
    }

    static void printElapsedTime(String title, Long startTime, String className) {
        startTime = System.nanoTime() - startTime;
        System.out.println(title + " took=" + ((double) startTime) / 1000000D + " ms and class_name=" + className);
    }
}

Example class 1

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest1 extends StaticMethodCallerClassName {
    public static void main(String[] args) {
        x1();
    }

    static void x1() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}


Example class 2

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest2 extends StaticMethodCallerClassNameTest1 {
    public static void main(String[] args) {
        x2();
    }

    static void x2() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}

Example class 3

package com.pritom.kumar;

/**
 * Created by pritom on 10/10/2016.
 */
public class StaticMethodCallerClassNameTest3 extends StaticMethodCallerClassNameTest2 {
    public static void main(String[] args) {
        x3();
    }

    static void x3() {
        long startTime = System.nanoTime();
        String className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThreadStackTrace();
        }
        printElapsedTime("ThreadStackTrace", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingThrowable();
        }
        printElapsedTime("Throwable", startTime, className);

        startTime = System.nanoTime();
        className = null;
        for (int i = 0; i < 10000; i++) {
            className = returnCallerClassNameUsingSecurityManager();
        }
        printElapsedTime("SecurityManager", startTime, className);
    }
}

No comments:

Post a Comment