java 获取当前函数名的实现代码
时间:2021-09-21 03:32:45
废话不多说,直接上代码
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Java实现类似C/C++中的__FILE__、__FUNC__、__LINE__等,主要用于日志等功能中。
*
* @version 1.0
*
*/
public abstract class CommonFunction {
/**
* 打印日志时获取当前的程序文件名、行号、方法名 输出格式为:[FileName | LineNumber | MethodName]
*
* @return
*/
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
StringBuffer toStringBuffer = new StringBuffer("[").append(
traceElement.getFileName()).append(" | ").append(
traceElement.getLineNumber()).append(" | ").append(
traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
// 当前文件名
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getFileName();
}
// 当前方法名
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}
// 当前行号
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}
// 当前时间
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java内存模型详解
![](https://img.aspxhome.com/file/2023/2/57592_0s.webp)
Java实现Excel文件转PDF(无水印无限制)
![](https://img.aspxhome.com/file/2023/5/58535_0s.png)
Java设计模式之工厂模式案例详解
![](https://img.aspxhome.com/file/2023/2/60342_0s.png)
Java应用开源框架实现简易web搜索引擎
浅析Java的Hibernate框架中的继承关系设计
![](https://img.aspxhome.com/file/2023/6/75796_0s.png)
详解AngularJs与SpringMVC简单结合使用
Mybatis配置之<typeAliases>别名配置元素解析
![](https://img.aspxhome.com/file/2023/7/60447_0s.png)
Winform控件优化Paint事件实现圆角组件及提取绘制圆角的方法
![](https://img.aspxhome.com/file/2023/9/77299_0s.png)
一文带你了解Java万物之基之Object类
![](https://img.aspxhome.com/file/2023/9/67119_0s.png)
C#模拟实现抽奖小程序的示例代码
![](https://img.aspxhome.com/file/2023/0/78130_0s.jpg)
JavaWeb使用Session和Cookie实现登录认证
SpringBoot文件上传大小设置方式(yml中配置)
基于WPF实现筛选下拉多选控件
![](https://img.aspxhome.com/file/2023/4/69784_0s.png)
Java使用Jdbc连接Oracle执行简单查询操作示例
![](https://img.aspxhome.com/file/2023/8/63408_0s.jpg)
Spring Boot 入门教程
SpringBoot Java后端实现okhttp3超时设置的方法实例
SpringBoot2.x 参数校验问题小结
![](https://img.aspxhome.com/file/2023/7/61567_0s.png)
Java中ArrayList和LinkedList区别
使用Spring boot + jQuery上传文件(kotlin)功能实例详解
![](https://img.aspxhome.com/file/2023/3/77963_0s.png)