java获取当前类的类名和方法名

如果我们想要获取当前类的类名,那么可以使用如下代码:

String className = this.getClass().getName();// 这是完整的类路径名
String simpleClassName = this.getClass().getSimpleName();// 仅仅是类名

如果我们想要在某个方法内获取到该方法的方法名,可以使用如下代码:

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

完整代码如下:

package com.hello.demo;

public class TestA {
    public void hello() {
        System.out.println("hello world");
        String className = this.getClass().getName();
        String simpleClassName = this.getClass().getSimpleName();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        // hello world
        //className: com.hello.demo.TestA, simpleClassName: TestA, methodName: hello
        System.out.println("className: " + className + ", simpleClassName: " + simpleClassName + ", methodName: " + methodName);
    }

    public static void main(String[] args) {
        new TestA().hello();
    }
}

获取类名很容易理解,而获取到方法名是跟运行栈的结构层次有关系。我们尝试直接打印Thread.currentThread().getStackTrace()数组中的所有值:

package com.hello.demo;

public class TestA {
    public void hello() {
        for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        new TestA().hello();
    }
}
/*
    java.lang.Thread.getStackTrace(Thread.java:1559)
    com.hello.demo.TestA.hello(TestA.java:5)
    com.hello.demo.TestA.main(TestA.java:11)
 */

可以发现数组中第二个元素(下标为1)正是com.hello.demo.TestA.hello,所以我们可以得到运行的类名是TestA和方法名hello。所以我们能够根据调用栈来获取到方法的方法名信息。注意StackTraceElement有几个方法:

  • getClassName():获取类名。
  • getLineNumber():获取行号。
  • getMethodName():获取方法名。
  • getFileName():获取文件名。

拓展:既然我们可以通过调用栈来获取到类名和方法名,那么我们就可以利用这一点来打印更加精确的日志,如某个类的具体某方法的日志。上面的代码只能获取到方法所在类的类名和方法本身的方法名,打印日志的话我们更希望获取到调用了这个方法的类名和调用了这个方法的方法名。如下:

TestB.java:该类中有一个方法等待被调用。

package com.hello.demo;

public class TestB {
    public void world() {
        System.out.println("hello world");
    }
}

TestA.java:我们在TestA类中也有一个hello方法,在该方法内调用了TestB类的world方法,我们希望world方法能够打印hello方法所在类的类名和hello这个方法名

package com.hello.demo;

public class TestA {
    public void hello() {
        new TestB().world();
    }

    public static void main(String[] args) {
        new TestA().hello();
    }
}

如果按照上面的方式,则只会获取到world方法所在类的类名和方法名。

package com.hello.demo;

public class TestB {
    public void world() {
        System.out.println("hello world");
        String className = this.getClass().getName();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        // className: com.hello.demo.TestB, methodName: world
        System.out.println("className: " + className + ", methodName: " + methodName);
    }
}

如果想要获取到调用world方法的所在类类名和方法名,就必须往调用栈的上一层找,如下:

package com.hello.demo;

public class TestB {
    public void world() {
        for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
            String className = element.getClassName();
            String methodName = element.getMethodName();
            System.out.println("className: " + className + ", methodName: " + methodName);
        }
    }
}
/*
    className: java.lang.Thread, methodName: getStackTrace
    className: com.hello.demo.TestB, methodName: world
    className: com.hello.demo.TestA, methodName: hello
    className: com.hello.demo.TestA, methodName: main
 */

栈是先进后出,所以从上往下是数组中的第三个元素(下标为2)。即:

String className = Thread.currentThread().getStackTrace()[2].getClassName();
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值