Description

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 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 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.

In a 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.

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.

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获取当前类名和方法名的更多相关文章

  1. PHP 获取当前类名、方法名、URL地址

      1.PHP获取当前类名.方法名  __CLASS__ 获取当前类名  __FUNCTION__ 当前函数名(confirm)  __METHOD__ 当前方法名 (bankcard::confir ...

  2. java 获取 正在执行的方法名

    //获取调用该方法的方法名.... String method = Thread.currentThread().getStackTrace()[2].getMethodName(); //获取正在执 ...

  3. JAVA中获取当前运行的类名,方法名,行数

    JAVA中获取当前运行的类名,方法名,行数 public static String getTraceInfo(){ StringBuffer sb = new StringBuffer(); Sta ...

  4. Java中获取运行代码的类名、方法名

    以下是案例,已运行通过 package com.hdys; /* * 1.获取当前运行代码的类名,方法名,主要是通过java.lang.StackTraceElement类 * * 2. * [1]获 ...

  5. python-获取类名和方法名,动态创建类和方法及属性

    获取类名和方法名1.在函数外部获取函数名称,用.__name__获取2.在函数内部获取当前函数名称,用sys._getframe().f_code.co_name方法获取3.使用inspect模块动态 ...

  6. PHP 获取当前所在的类名、方法名等

    PHP获取当前类名.方法名  __CLASS__ 获取当前类名  __FUNCTION__ 当前函数名(confirm)  __METHOD__ 当前方法名 (bankcard::confirm) _ ...

  7. Java获取当前类名的两种方法

    适用于非静态方法:this.getClass().getName() 适用于静态方法:Thread.currentThread().getStackTrace()[1].getClassName() ...

  8. 将string转为同名类名,方法名。(c#反射)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace stri ...

  9. C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute

    需要反射的DLL里的一个类: namespace ElegantWM.WebUI.Areas.Admin.Controllers { [Description("功能模块管理")] ...

随机推荐

  1. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  2. 十五、mac 中登陆mysql忘记密码解决办法

    mac 中登陆mysql忘记密码解决办法 1.打开终端,输入命令:cd /usr/local/mysql/bin 2.mysql -uroot -p,用这条命令登陆时报错信息: 报错:Enter pa ...

  3. ionic2升级到ionic3并打包APK

    通过IONIC2升级到3的时候,经过我一系列的测试,以及网上各种办法,现将新测有效的方法记录如下,本人按如下方法,对多个项目升级后,都能正常打包成APK IONIC 2到3的升级: 1.拷贝ionic ...

  4. centos 服务器内存管理 服务于端口状态

    du su /目录/ 查看改目录大小 ls -lht /  查看文件详情,显示文件大小(直观) df -h 查看系统内存占用情况 centos 版本 lsb_release -a cat /etc/i ...

  5. selenium IDE录制

    一.        安装 可以用Firefox打开https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,在这里边找到和当前firef ...

  6. Python3.7在win10下安装PyAudio库以及实现音频的录制与播放

    Python3.7 无法安装pyaudio 度娘的结果基本都是这个,pip install pyaudio.....然而十有八九你的电脑不买账,会报错. 报错信息: running install r ...

  7. Bat 修改 xml 文件标签值

    xml 文件如下: <ConfigurationData> <ReportsFolder>\Reports</ReportsFolder> <Helpfold ...

  8. 机器学习框架Tensorflow数字识别MNIST

    SoftMax回归  http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 我们的训练集由  个已标记的样本构成: ,其 ...

  9. 解决windows文件名过长无法删除的问题

    删除windows文件时,系统提示如下错误: 从网上找到下面的一种方法,顺利解决(原理不清楚),现记录删除方法如下: . 在要删除的文件夹(delete_dir)同级新建一个空文件夹(empty_di ...

  10. TensorFlow——深度学习笔记

    深度学习与传统机器学习的区别 传统机器学习输入的特征为人工提取的特征,例如人的身高.体重等,深度学习则不然,它接收的是基础特征,例如图片像素等,通过多层复杂特征提取获得. 深度学习.人工智能.机器学习 ...