Advertisement
dwhitzzz

getMethodName()/getClassName()

Nov 13th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1. /* Utility class: Getting the name of the current executing method
  2.  * https://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method
  3.  *
  4.  * Provides:
  5.  *
  6.  *      getCurrentClassName()
  7.  *      getCurrentMethodName()
  8.  *      getCurrentFileName()
  9.  *
  10.  *      getInvokingClassName()
  11.  *      getInvokingMethodName()
  12.  *      getInvokingFileName()
  13.  *
  14.  * Nb. Using StackTrace's to get this info is expensive. There are more optimised ways to obtain
  15.  * method names. See other stackoverflow posts eg. https://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection/2924426#2924426
  16.  *
  17.  * 29/09/2012 (lem) - added methods to return (1) fully qualified names and (2) invoking class/method names
  18.  */
  19. package utility;
  20.  
  21. public class StackTraceInfo
  22. {
  23.     /* (Lifted from virgo47's stackoverflow answer) */
  24.     private static final int CLIENT_CODE_STACK_INDEX;
  25.  
  26.     static {
  27.         // Finds out the index of "this code" in the returned stack trace - funny but it differs in JDK 1.5 and 1.6
  28.         int i = 0;
  29.         for (StackTraceElement ste: Thread.currentThread().getStackTrace())
  30.         {
  31.             i++;
  32.             if (ste.getClassName().equals(StackTraceInfo.class.getName()))
  33.             {
  34.                 break;
  35.             }
  36.         }
  37.         CLIENT_CODE_STACK_INDEX = i;
  38.     }
  39.  
  40.     public static String getCurrentMethodName()
  41.     {
  42.         return getCurrentMethodName(1);     // making additional overloaded method call requires +1 offset
  43.     }
  44.  
  45.     private static String getCurrentMethodName(int offset)
  46.     {
  47.         return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getMethodName();
  48.     }
  49.  
  50.     public static String getCurrentClassName()
  51.     {
  52.         return getCurrentClassName(1);      // making additional overloaded method call requires +1 offset
  53.     }
  54.  
  55.     private static String getCurrentClassName(int offset)
  56.     {
  57.         return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getClassName();
  58.     }
  59.  
  60.     public static String getCurrentFileName()
  61.     {
  62.         return getCurrentFileName(1);     // making additional overloaded method call requires +1 offset
  63.     }
  64.  
  65.     private static String getCurrentFileName(int offset)
  66.     {
  67.         String filename = Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getFileName();
  68.         int lineNumber = Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + offset].getLineNumber();
  69.  
  70.         return filename + ":" + lineNumber;
  71.     }
  72.  
  73.     public static String getInvokingMethodName()
  74.     {
  75.         return getInvokingMethodName(2);
  76.     }
  77.  
  78.     private static String getInvokingMethodName(int offset)
  79.     {
  80.         return getCurrentMethodName(offset + 1);    // re-uses getCurrentMethodName() with desired index
  81.     }
  82.  
  83.     public static String getInvokingClassName()
  84.     {
  85.         return getInvokingClassName(2);
  86.     }
  87.  
  88.     private static String getInvokingClassName(int offset)
  89.     {
  90.         return getCurrentClassName(offset + 1);     // re-uses getCurrentClassName() with desired index
  91.     }
  92.  
  93.     public static String getInvokingFileName()
  94.     {
  95.         return getInvokingFileName(2);
  96.     }
  97.  
  98.     private static String getInvokingFileName(int offset)
  99.     {
  100.         return getCurrentFileName(offset + 1);     // re-uses getCurrentFileName() with desired index
  101.     }
  102.  
  103.     public static String getCurrentMethodNameFqn()
  104.     {
  105.         return getCurrentMethodNameFqn(1);
  106.     }
  107.  
  108.     private static String getCurrentMethodNameFqn(int offset)
  109.     {
  110.         String currentClassName = getCurrentClassName(offset + 1);
  111.         String currentMethodName = getCurrentMethodName(offset + 1);
  112.  
  113.         return currentClassName + "." + currentMethodName ;
  114.     }
  115.  
  116.     public static String getCurrentFileNameFqn()
  117.     {
  118.         String CurrentMethodNameFqn = getCurrentMethodNameFqn(1);
  119.         String currentFileName = getCurrentFileName(1);
  120.  
  121.         return CurrentMethodNameFqn + "(" + currentFileName + ")";
  122.     }
  123.  
  124.     public static String getInvokingMethodNameFqn()
  125.     {
  126.         return getInvokingMethodNameFqn(2);
  127.     }
  128.  
  129.     private static String getInvokingMethodNameFqn(int offset)
  130.     {
  131.         String invokingClassName = getInvokingClassName(offset + 1);
  132.         String invokingMethodName = getInvokingMethodName(offset + 1);
  133.  
  134.         return invokingClassName + "." + invokingMethodName;
  135.     }
  136.  
  137.     public static String getInvokingFileNameFqn()
  138.     {
  139.         String invokingMethodNameFqn = getInvokingMethodNameFqn(2);
  140.         String invokingFileName = getInvokingFileName(2);
  141.  
  142.         return invokingMethodNameFqn + "(" + invokingFileName + ")";
  143.     }
  144. }
  145.  
  146. /* Examples of usage */
  147.    /**
  148.      * Print in console the start of the method
  149.      * Utility function
  150.      */
  151.     public static void startMethodLog() {
  152.         //just passing 1 because we want the name of the caller method, not this one
  153.         System.out.println("\n### START " + getInvokingClassName() + "." + getInvokingMethodName() + " ###");
  154.     }
  155.  
  156.     /**
  157.      * Print in console the end of the method
  158.      * Utility function
  159.      */
  160.     public static void endMethodLog() {
  161.         //just passing 1 because we want the name of the caller method, not this one
  162.         System.out.println("### END " + getInvokingClassName() + "." + getInvokingMethodName() + " ###\n");
  163.     }
  164.  
  165. private static void helloWorld() {
  166.         startMethodLog();
  167.         System.out.println("Hello World !");
  168.         endMethodLog();
  169.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement