java获取当前类名和方法名
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获取当前类名和方法名的更多相关文章
- PHP 获取当前类名、方法名、URL地址
1.PHP获取当前类名.方法名 __CLASS__ 获取当前类名 __FUNCTION__ 当前函数名(confirm) __METHOD__ 当前方法名 (bankcard::confir ...
- java 获取 正在执行的方法名
//获取调用该方法的方法名.... String method = Thread.currentThread().getStackTrace()[2].getMethodName(); //获取正在执 ...
- JAVA中获取当前运行的类名,方法名,行数
JAVA中获取当前运行的类名,方法名,行数 public static String getTraceInfo(){ StringBuffer sb = new StringBuffer(); Sta ...
- Java中获取运行代码的类名、方法名
以下是案例,已运行通过 package com.hdys; /* * 1.获取当前运行代码的类名,方法名,主要是通过java.lang.StackTraceElement类 * * 2. * [1]获 ...
- python-获取类名和方法名,动态创建类和方法及属性
获取类名和方法名1.在函数外部获取函数名称,用.__name__获取2.在函数内部获取当前函数名称,用sys._getframe().f_code.co_name方法获取3.使用inspect模块动态 ...
- PHP 获取当前所在的类名、方法名等
PHP获取当前类名.方法名 __CLASS__ 获取当前类名 __FUNCTION__ 当前函数名(confirm) __METHOD__ 当前方法名 (bankcard::confirm) _ ...
- Java获取当前类名的两种方法
适用于非静态方法:this.getClass().getName() 适用于静态方法:Thread.currentThread().getStackTrace()[1].getClassName() ...
- 将string转为同名类名,方法名。(c#反射)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace stri ...
- C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute
需要反射的DLL里的一个类: namespace ElegantWM.WebUI.Areas.Admin.Controllers { [Description("功能模块管理")] ...
随机推荐
- Windows环境下svn服务器的安装步骤
做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行有效的管理. 下载SVN服务器 下载地址是:http://subversion.apache.org/pa ...
- 网易OpenStack部署运维实战
OpenStack自2010年项目成立以来,已经有超过200个公司加入了 OpenStack 项目,目前参与 OpenStack 项目的开发人员有 17,000+,而且这些数字还在增加,作为一个开源的 ...
- c语言字符串内存分配小记
一.疑问 有这样一道题: #include "stdio.h" int main() { ]; ]; scanf("%s", word1); scanf(&qu ...
- TIDB介绍
TiDB 是什么? TiDB 是一个分布式 NewSQL 数据库.它支持水平弹性扩展.ACID 事务.标准 SQL.MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 O ...
- 最小化安装Linux的常用配置整理
基于安全性考虑,将服务器进行最小化安装,毕竟软件包越少,漏洞越少,相对来说就约安全,但是最小化安装会给运维带来一些问题和不便,下面是我总结的,常见的一些配置和工具的安装,仅供各位大神参考,如有新的id ...
- Python全栈工程师(数值类型、运算符)
ParisGabriel Python 入门基础 python的应用领域: 1.系统运维 2.网络编程(如:网络爬虫,搜索引擎,服务器编程) 3.科学计算 4.航空领域(如:卫星, ...
- NSIS编译报错:您可能有有一个或两个(大)的旧临时文件
一.有时在编译NSIS时会出现如下错误: 注意: 您可能有有一个或两个(大)的旧临时文件 残留在临时目录文件夹中 (通常这种情况只会发生在 Windows 9x 系统中). 二.本人遇到的问题原因: ...
- mysql中查询常用的关键字
最简单的查询: 1 select * from [where ] 1 select column1,column2....from [where] 这里需要注意的是where子句中条件过滤使用到的关键 ...
- PHP面向对象关键词static 、self
知识点: 一.static可以修饰类内的属性或方法,被修饰的属性或方法在类外部,不能被实例化成对象访问,而是使用类本身进行访问,而静态的方法如果想使用静态的属性,则需要用self::这样的写法来访问静 ...
- 【bzoj3894】文理分科 网络流最小割
原文地址:http://www.cnblogs.com/GXZlegend 题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用 ...