Java获取当前类名的两种方法
适用于非静态方法:this.getClass().getName()
适用于静态方法:Thread.currentThread().getStackTrace()[1].getClassName()
获取类名:
1、在类的实例中可使用this.getClass().getName();但在static method中不能使用该方法;
2、在static method中使用方法:Thread.currentThread().getStackTrace()[1].getClassName();
获取方法名:Thread.currentThread().getStackTrace()[1].getMethodName();
获取代码行号:Thread.currentThread().getStackTrace()[1].getLineNumber();
Log 代码:
System.out.println("Class: "+this.getClass().getName()+" method: "+
Thread.currentThread().getStackTrace()[1].getMethodName() +" line:"+
Thread.currentThread().getStackTrace()[1].getLineNumber());
http://blog.sina.com.cn/s/blog_4a4f9fb50101eyfp.html
Description
- Using Thread
- Using getClass()
getClass() method present in every Javaobject. Like here:
String clazz = this.getClass().getName();
static method. It won't work. Even the keyword this is 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.
static method you can instead use the following:
String clazz = Thread.currentThread().getStackTrace()[1].getClassName();
Which uses the static methodgetStackTrace() 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.
String method = Thread.currentThread().getStackTrace()[1].getMethodName();
The code
package org.wikijava.reflection;
public class MethodName { public static void main(String[] args) {
MethodName methodName = new MethodName();
String clazz = Thread.currentThread() .getStackTrace()[1].getClassName();
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method);
methodName.anotherMethod(); }
private void anotherMethod() {
String clazz = this.getClass().getName();
String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
System.out.println("class name: " + clazz + " Method Name " + method); } }
Java获取当前类名的两种方法的更多相关文章
- JAVA 集合 List 分组的两种方法
CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- tensorflow中的函数获取Tensor维度的两种方法:
获取Tensor维度的两种方法: Tensor.get_shape() 返回TensorShape对象, 如果需要确定的数值而把TensorShape当作list使用,肯定是不行的. 需要调用Tens ...
- APP自动化测试获取包名的两种方法
获取包名的两种方法: 一.通过aapt获取 1.进入aapt.exe所在路径 2.在地址栏输入cmd回车,打开dos命令窗口. 3.在命令窗口输入 aapt dump badging 拖入apk 回车 ...
- java字符串大小写转换的两种方法
转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString { pu ...
- Java获取ip地址的几种方法
以下内容介绍下java获取ip地址的几种思路. 1.直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址( ...
- 微信网页开发之获取用户unionID的两种方法--基于微信的多点登录用户识别
假设网站A有以下功能需求:1,pc端微信扫码登录:2,微信浏览器中的静默登录功能需求,这两种需求就需要用到用户的unionID,这样才能在多个登录点(终端)识别用户.那么这两种需求下用户的unionI ...
- Java获取文件Content-Type的四种方法
HTTP Content-Type在线工具 有时候我们需要获取本地文件的Content-Type,已知 Jdk 自带了三种方式来获取文件类型. 另外还有第三方包 Magic 也提供了API.Magic ...
- docker inspect获取详细参数的两种方法
docker inspect xx 返回的是一个json格式的数据 以下为部分返回值 [ { "Id": "706813b0da107c4d43c61e3db9da908 ...
随机推荐
- 测试开发之Django——No4.Django中前端框架的配置与添加
我们在开发一个web项目的时候,虽然我们不是专业开发,但是我们也想要做出来一个美美的前端页面. 这种时候,百度上铺天盖地的前端框架就是我们的最好选择了. 当然,在网上直接下载的框架,我们是不能直接用的 ...
- linux下cpu物理个数、多核、超线程判断解析
在Linux体系中,供给了proc文件体系显示体系的软硬件信息.若是想懂得体系中CPU的供给商和相干设备信息,则可以经由过程/proc/cpuinfo文件获得.本文章针对该文件进行简单的总结. 基于指 ...
- oracle创建表空间并赋予权限
CREATE TEMPORARY TABLESPACE 表空间 TEMPFILE 数据存储路径('D://oracle//NEW_NAMESPACE.DBF') SIZE 32M AUTOEXTEN ...
- java Comparator和Comparable(比较器)
Comparable: 一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序,sort方法调用compareTo()方法里定义 ...
- tenaorflow函数(1)
TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测.如果检测 ...
- HDOJ题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- linux诡异的硬盘不足
phpmyadmin页面登录不进去,ftp也连不上.而服务端的service都开着的.直觉是看一下硬盘使用情况. df -TH 发现可用空间几乎为0 但是查看各个目录使用情况: du -sh /* | ...
- **CI中自动类加载的用法总结
总结: 哪一个类中用到某一个类,就在构造函数中加载这个类,比如m_attach,C_Feed类中有用到,那么就在构造函数中加载 控制器: class C_Feed extends CI_Control ...
- 墨刀 vs Axure RP
https://www.jianshu.com/p/b4b9c1f15304 墨刀https://modao.cc/ Axure RP https://www.axure.com/https://ww ...
- bnu 10809 聚餐
Lolilu大牛又要请客了~~有些同学呢,是果断要去的,而有些同学呢,只有确定心中的大牛会参加,他才会参加.Lolilu决定请大家去吃金钱豹,因此希望你告诉他一共会有多少人参加,他才知道带多少钱比较合 ...