在java方法中获得当前方法的名称方法:
一、获得当前类名:
Java代码
this.getClass().getName();
二、获得当前方法名臣:
JDK1.4
Java代码
new Exception().getStackTrace()[i].getMethodName();//其中i = 0就是当前的类的方法名字 ;i == 1就是调用者的方法
JDK1.5之后可用
Java代码
Thread.currentThread().getStackTrace()[1].getMethodName();//具体使用数组的那个元素和JVM的实现有关,我在SUN JDK6下面测试的是第二个元素,具体说明可以查看Thread.getStackTrace方法的javadoc
参考如下:
// 获得当前类名
String clazz = Thread.currentThread() .getStackTrace()[1].getClassName();
// 获得当前方法名
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method);
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println(method);
this.getClass().getName()