从java代码获取类名_java代码获取当前类类名、方法名

获取类名:

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

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);   }   }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/562919.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux将mysql导出表数据导入另一台服务器_远程linux服务器mysql数据库导入和导出.sql文件...

今天对需要将服务器上的数据库放到本地环境,之前没怎么这么操作过,ORZ,弱鸡,这篇帖子不错,收藏了!!!远程linux服务器mysql数据库导入和导出.sql文件大部分情况本地开发环境为windows…

java cpu高_Java中的CPU占用高和内存占用高的问题排查

下面通过模拟实例分析排查Java应用程序CPU和内存占用过高的过程。如果是Java面试,这2个问题在面试过程中出现的概率很高,所以我打算在这里好好总结一下。1、Java CPU过高的问题排查举个例子,如下:package com.classloading;public…

solr 时间转成java_solr时区设置解决时间多8小时问题

今天发现我博客里文章的时间与实际时间差了8小时,正好比原来时间多出8小时,想想应该是时区不一致的问题。用solr web工具查询出来的数据原样:"create_time": "2018-01-18T11:22:06Z"{"original": 1,"crea…

php if 单行,php – 单行if语句或if语句没有大括号坏习惯?

最好的做法是编写别人可以轻松读取和更新的代码。您的第一个表单是有问题的,因为它不遵循大多数PHP开发人员使用的表单:if (condition) {// code} else {// code}// ... or ...if (condition){// code}else{// code}// ... or ...if (condition) { /* sh…

php 错误 异常,php中的异常和错误解析

PHP错误是属于php程序自身的问题,一般是由非法的语法,环境问题导致的,使得编译器无法通过检查甚至无法运行的情况。PHP异常一般是业务逻辑上出现的不合预期、与正常流程不同的状况,不是语法错误。本文介绍了php中异常和错误的相关…

php变量原格式输出,PHP格式化输出打印变量

PHP 常用的输入变量函数print_r,但是输出没有换行,看起来很费力 我们可以自定义一个函数来实现变量的格式化输出,代码如下: function dump($vars, $label , $return false) {if (ini_get(html_errors)) {$content "\n&quo…

一个控制器2套模版php,如何不用一个自定义的控制器渲染一个模板

如何不用一个自定义的控制器渲染一个模板通常,当您需要创建一个页面,您需要创建一个控制器并且从该控制器中呈现模板。但如果您仅仅呈现一个简单的模板,并且不需要传递给它的任何数据,则完全没必要创建一个控制器,通过…

JAVA语言写的,用java语言写

Caballariistatic int argument;static int count0;public static void main(String[] args){argument5;printResult("",argument);System.out.println("总共"count"种");}public static void printResult(String result,int left){if(left1){Sy…

accept标头 php,解决PHP中缺少“授权”请求标头的问题

我目前正在为一个uni项目开发PHP REST API,该项目使用通过PhoneGap从移动Web应用程序传递的JSON Web令牌,或者在开发过程中使用我的桌面.使用ajax将令牌发送到我的服务器页面“ friends / read.PHP”时,服务器使用以下命令正确获取了Authorization标头$headers getallheaders(…

handsome对应php文件,基于handsome主题的一些美化总结

用了handsome主题有一段时间了,挺不错的主题!在无数次更换各种博客程序后最终还是选择使用Typecho&Handsome主题搭建博客如果长久发展的话建议就用wordpress进入正题:代码没标明的都是添加至handsome主题后台主题设置 自定义CSS。首页头像转动并放大.thumb-lg{w…

php语录网站,杨泽业:给你的wordpress博客添加经典语录功能,适合所有php网站

我们做一个网站就是要不断给访客提供最佳的用户体验,同时提升自己的品牌价值,而文章结束的经典语录的功能,完全可以实现我们所要达到的效果。(如下图)给你的wordpress博客添加经典语录功能这个是怎样实现的呢?其实很简单&#xff…

matlab搭建的应用层架构逻辑,软件设计的三层架构

1:三层架构是什么?三层架构是一个分层式的软件体系架构设计理念。把软件架构分为三层;1:UI层 (user interface layer) 界面层2:BLL层 (business logic layer) 业务逻辑层3:DAL层 (data access laye…

mysql null 0 空,MySQL的空值和NULL区别

从本质上区别:1、空值不占空间2、null值占空间通俗的讲:空值就像是一个真空转态杯子,什么都没有,而null值就是一个装满空气的杯子,虽然看起来都是一样的,但是有着本质的区别。例子:创建一个test…

mysql 数据库中数据去重,oracle数据库中如何达到像mysql数据库中group by 那种去重的效果..求大神解答...

满意答案oracle和mysql,group by是由区别的。oracle的gruop by 后跟的必须是select查询出的字段而且group by语句中select指定的字段必须是“分组依据字段”,其他字段若想出现在select中则必须包含在聚合函数中聚合函数比如:sum(列名) 求和ma…

强制关机对电脑的影响_笔记本电脑需要每天关机吗?长期开机对电脑有影响么?...

笔者之前写过一些选购笔记本的文章,就有不少网友问了这个问题:笔记本电脑需要每天关机么?一直开着有没有问题?这里说得一直开着是选择了合盖睡眠(或者休眠)的方式。使用时翻开笔记本盖子就可以工作,工作做完后合上盖子…

matlab 矩阵与向量乘,matlab问题,关于矩阵向量的乘问题

问题描述:matlab问题,关于矩阵向量的乘问题mxwtbx*T;mywtby*T;mzwtbz*T;%%%通过四元素解微分方程pmx.^2my.^2mz.^2;//这个地方有问题,我在下面说a1-p/8;b0.5-p/48;c(1)b*(a*q(1)/b-mx*q(2)-my*q(3)-mz*q(4));//提示这里有问题.c(2)b*(mx*q(1)a*q(2)/bmz*q(3)-my*q(4…

python关闭exe程序_Win 10 中使用 Python 碰到的奇怪现象

优质文章,第一时间送达!作者:流光飞舞来源:https://shuhari.dev/blog/2019/11/win10-store-python最近在使用 Python 的时候发生了很奇怪的现象:从命令行执行 python.exe并不会进入 REPL,似乎也没有其他反应…

Matlab语音采集与读写程序,基于MATLAB的语音信号录制采集和分析的程序设计

理 论广 角 ● I 基于 MATLAB的语音信号录制采集和分析的程序设计 刘 晓炯 (西北民族大学电气工程学院 甘肃 兰州I 730030) [摘 要]语音信号处理技术是语音处理领域中新近发展起来的一个学科分支,MATLAB是一个数据分析和处理功能十分强大的工程使用软件,…

mulitpartfile怎么接收不到值_手机有时接收不到微信消息通知,怎么办?超全解决方案...

不知道大家在使用微信的过程中有没有遇到以下情况,在自己看视频或者刷其他网页的时候,没有接收到微信的任何通知和提示的消息。等到你进入微信页面后,突然发现有很多未读信息。这种情况时有发生,很多人都反应说微信接收消息总是会…

生活 list.php,list.php

require_once config.php;// 接收传来的分类id$categoryId$_GET[categoryId];//3// 查询当前分类下的文章// echo $categoryId;// 连接数据库$connectmysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);// 写sql$sql"select p.id,p.title,p.feature,p.content,p.created,p.…