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("功能模块管理")] ...
随机推荐
- Spring核心技术(十五)——BeanFactory
BeanFactory是Spring IoC功能的潜在基础,但是现在BeanFactory一般仅仅用于直接集成第三方的框架,对于大多数的Spring用户来说,BeanFactory已经算是一个历史了. ...
- 12 KLT算法
1 去除多余模块的 #-*- coding:utf-8 -*- ''' Lucas-Kanade tracker ==================== Lucas-Kanade sparse op ...
- Docker容器 - 容器时间跟宿主机时间同步
在Docker容器创建好之后,可能会发现容器时间跟宿主机时间不一致,这就需要同步它们的时间,让容器时间跟宿主机时间保持一致. 转载自:https://www.cnblogs.com/kevingrac ...
- 用Mapreduce求共同好友
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- 4.bootstrap的form表单的form-group和form-control的区别与联系
1. form-group一般用于div form-control一般用于置于div中的标签元素,为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form ...
- APP开发手记01(app与web的困惑)
文章链接:http://quke.org/post/app-dev-fragment.html (转载时请注明本文出处及文章链接) 最近在用博客园的wcf服务做博客园的android和ios的app, ...
- Ajax异步与JavaScript的一些初浅认识
向服务器请求数据的技术 有以下五种常用技术用于向服务器请求数据 XMLHttpRequest(XHR) Dynamic script tag insertion(动态脚本标签插入) iframes C ...
- LCS+LIS
#include<iostream> #include<string> using namespace std; string a,b; ][]; int main() { w ...
- flask利用session身份伪造
想研究很久了,这次终于初步了解了flask session伪造(得知道密钥). python2和python3 session解密不一样,而且不都是base64,脚本https://github.co ...
- Python学习5,三级菜单实例
_author_ = "Happyboy" data = { '北京':{ "昌平":{ "沙河":["Happyboy" ...