获取类名方法名的方法
2016-06-04 16:25
260 查看
Below I present you two different ways to get the current Class:
Using Thread
Using getClass()
The simplest way to get the name of the class where your code is being executed in is using the
This works only if executed in an Object, namely an instanciated class. If you try to execute the code above in a
Also, the class returned by the above method may actually be a subclass of the class in which the method is defined. This is because subclasses inherit the methods of their parents; and getClass() returns the actual runtime type of the object. To get the actual class in which a method is defined, use the method below also.
In a
Which uses the static method
A similar trick can be used to find out the name of the method currently executed:
It's exactly the same principle, just you dig out the name of the method instead of the class.
Using Thread
Using getClass()
The simplest way to get the name of the class where your code is being executed in is using the
getClass()method present in every java object. Like here:
String clazz = this.getClass().getName();
This works only if executed in an Object, namely an instanciated class. If you try to execute the code above in a
staticmethod. It won't work. Even the keyword
thisis meaningless in a static method.
Also, the class returned by the above method may actually be a subclass of the class in which the method is defined. This is because subclasses inherit the methods of their parents; and getClass() returns the actual runtime type of the object. To get the actual class in which a method is defined, use the method below also.
In a
staticmethod you can instead use the following:
String clazz = Thread.currentThread().getStackTrace()[1].getClassName();
Which uses the static method
getStackTrace()to get the whole stacktrace. This method returns an array, where the first element (index 0) is the getStackTrace() you called and the second element (index 1) is the method your code is in.
A similar trick can be used to find out the name of the method currently executed:
String method = Thread.currentThread().getStackTrace()[1].getMethodName();
It's exactly the same principle, just you dig out the name of the method instead of the class.
String className = this.getClass().getName(); 或者 Thread.currentThread().getStackTrace()[1].getClassName(); String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
String className = Thread.currentThread().getStackTrace()[1].getClassName(); String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); LOGGER.error(className+" : "+methodName+"+info) ;//这样就比较方便
相关文章推荐
- Android - 源码 - AsyncTask
- Java面向对象之内部类
- 总结C++中取成员函数地址的几种方法
- 字符串处理与正则表达式
- JavaScript:事件处理程序
- 按钮的可用状态
- Intent的学习
- Matlab命令行编译运行HelloWorld
- C++为什么用模板类,为什么用泛型?
- I2C总线
- Android之eclipse adt开发ndk没有NDK选项问题的解决方案
- 继续(三)
- Java设计模式(五) 原型模式详解
- Retina2(视网膜用于高动态范围亮度压缩)---opencv2.4.11
- Ruby面向对象编程中类的方法与类的扩展
- 算法竞赛入门第五章(竞赛题目选讲)解析:
- -------Java基础---异常------
- Android SharedPreference的使用
- java读取项目根路径下和任意磁盘位置下的properties文件
- 关于windbg的认识