📜  获取在Java中正在执行的当前方法的名称

📅  最后修改于: 2022-05-13 01:54:51.943000             🧑  作者: Mango

获取在Java中正在执行的当前方法的名称

获取当前执行方法的名称对于处理异常和调试目的很有用。
以下是获取当前执行方法的不同方法:

  1. 使用可抛出堆栈跟踪:
    • 使用 Throwable 类:在Java中,Throwable 类是Java.lang 包中所有异常和错误的超类。 Java Throwable 类提供了几种方法,如 addSuppressed()、getMessage()、getStackTrace()、getSuppressed()、toString()、printStackTrace() 等。
      我们可以通过在 Throwable 实例上调用 getStackTrace() 来获取表示与 throwable 相关的堆栈跟踪的堆栈跟踪元素数组。数组的第 0索引元素表示堆栈的顶部,这是序列中最新的方法调用。
      // Java program to demonstrate
      // Getting name of current method 
      // using Throwable Class
      public class GFG {
        
          public static void foo()
          {
              // getStackTrace() method return
              // current method name at 0th index
              String nameofCurrMethod = new Throwable()
                                            .getStackTrace()[0]
                                            .getMethodName();
        
              System.out.println("Name of current method: "
                  + nameofCurrMethod);
          }
        
          public static void main(String[] args)
          {
              // call function
              foo();
          }
      }
      

      输出:

      Name of current method: foo
    • 使用异常类
      我们还可以使用扩展 Throwable 类的 Exception 类。
      // Java program to demonstrate
      // Getting name of current method 
      // using Throwable Class
        
      public class GFG {
        
          public static void foo()
          {
              // getStackTrace() method return current
              // method name at 0th index
              String nameofCurrMethod = new Exception()
                                            .getStackTrace()[0]
                                            .getMethodName();
        
              System.out.println("Name of current method: " 
                   + nameofCurrMethod);
          }
        
          public static void main(String[] args)
          {
              // call function
              foo();
          }
      }
      

      输出:

      Name of current method: foo
  2. 使用 getEnclosureMethod() 方法
    • 按 Object Class :我们可以使用 Class.getEnclosureMethod(),该方法返回一个 Method 对象,表示 Prime 类的即时封闭方法。但这会带来额外的开销,因为它涉及在幕后创建一个新的匿名内部类。
      // Java program to demonstrate
      // Getting name of current method 
      // using Object Class
      public class GFG {
        
          // create a static method foo
          public static void foo()
          {
              // this method return a Method object representing
              // the instantly enclosing method of the method class
              String nameofCurrMethod = new Object() {}
                                            .getClass()
                                            .getEnclosingMethod()
                                            .getName();
        
              System.out.println("Name of current method: " 
                 + nameofCurrMethod);
          }
        
          public static void main(String[] args)
          {
              // call function
              foo();
          }
      }
      

      输出:

      Name of current method: foo
    • 通过内部类:我们还可以在方法中定义一个内部类来获取类引用。
      // Java program to demonstrate
      // Getting name of current method 
      // using Inner Class 
        
      public class GFG {
        
          // create a static method foo
          public static void foo()
          {
              // Local inner class
              class Inner {
              };
        
              // this method return a Method object representing
              // the instantly enclosing method of the method class
              String nameofCurrMethod = Inner.class
                                            .getEnclosingMethod()
                                            .getName();
        
              System.out.println("Name of current method: "
                    + nameofCurrMethod);
          }
        
          public static void main(String[] args)
          {
              // call function
              foo();
          }
      }
      

      输出:

      Name of current method: foo
      //This java program on our machine because inner class
      // have some restriction for security purpose   
      
  3. 使用线程堆栈跟踪: Thread.getStackTrace() 方法返回堆栈跟踪元素数组。返回的堆栈跟踪数组的第二个元素包含当前线程的方法名称。
    // Java program to demonstrate
    // Getting name of current method 
    // using Thread.getStackTrace() 
    public class GFG {
      
        // create a static method foo
        public static void foo()
        {
            // Thread.currentThread() return current method name
            // at 1st index in Stack Trace
            String nameofCurrMethod = Thread.currentThread()
                                          .getStackTrace()[1]
                                          .getMethodName();
      
            System.out.println("Name of current method: "
                + nameofCurrMethod);
        }
      
        public static void main(String[] args)
        {
            // call function
            foo();
        }
    }
    

    输出:

    Name of current method: foo