Java编程实现获取当前代码行行号的方法示例
作者:念月思灵 发布时间:2021-06-28 06:51:14
标签:Java,行号
本文实例讲述了Java编程实现获取当前代码行行号的方法。分享给大家供大家参考,具体如下:
最近的项目中,为了实现自定义的log类,能够输出具体的代码行行号,我通过使用StackTraceElement对象实现了。
具体内容请参考下面的Demo代码。这里指出需要注意的几个问题:
1. 程序中返回的代码行行号,是新建StackTrackElement对象的那一行。
2. 可以通过传参的方法实现输出特定行行号。
具体实现代码:
/**
*
*/
package leo.demo.training;
/**
* Get current java file name and current code line number
* @author Leo Xie
*/
public class CurrentLine {
/**
* @param args
*/
public static void main(String[] args) {
StackTraceElement ste1 = null;
// get current thread and its related stack trace
StackTraceElement[] steArray = Thread.currentThread().getStackTrace();
int steArrayLength = steArray.length;
String s = null;
// output all related info of the existing stack traces
if(steArrayLength==0) {
System.err.println("No Stack Trace.");
} else {
for (int i=0; i<steArrayLength; i++) {
System.out.println("Stack Trace-" + i);
ste1 = steArray[i];
s = ste1.getFileName() + ": Line " + ste1.getLineNumber();
System.out.println(s);
}
}
// the following code segment will output the line number of the "new " clause
// that's to say the line number of "StackTraceElement ste2 = new Throwable().getStackTrace()[0];"
StackTraceElement ste2 = new Throwable().getStackTrace()[0];
System.out.println(ste2.getFileName() + ": Line " + ste2.getLineNumber());
// the following clause will output the line number in the external method "getLineInfo()"
System.out.println(getLineInfo());
// the following clause will output its current line number
System.out.println(getLineInfo(new Throwable().getStackTrace()[0]));
}
/**
* return current java file name and code line number
* @return String
*/
public static String getLineInfo() {
StackTraceElement ste3 = new Throwable().getStackTrace()[0];
return (ste3.getFileName() + ": Line " + ste3.getLineNumber());
}
/**
* return current java file name and code line name
* @return String
*/
public static String getLineInfo(StackTraceElement ste4) {
return (ste4.getFileName() + ": Line " + (ste4.getLineNumber()));
}
}
希望本文所述对大家java程序设计有所帮助。
来源:http://www.cnblogs.com/xxpal/articles/1209378.html


猜你喜欢
- spring 容器依赖<dependency> <groupId>org.springframework
- 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来进一步封装底部菜单。先看效果图:主要包括以下功能
- 在实际开发中经常需要了解具体对象的类型,所以经常会使用GetType()和typeof()、尽管可以得到相应的类型、但两者之间也存在一些差别
- 为什么要自定义缓存注解?Spring Cache本身提供@Cacheable、@CacheEvict、@CachePut等缓存注解,为什么还
- ViewPagerIndicator导航栏指示器运行效果:实现这个效果,我是看了很多大神写的博客和视频后自己敲的,欢迎指正github地址:
- C#实现:#region 计算字符串相似度 /// &l
- 概述AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分
- 在上一篇文章中,我们学习了Camera的基本用法,并借助它们编写了一个例子,实现了类似于API Demos里的图片中轴旋转功能。不过那个例子
- 一般振动时间的配置在如下文件:frameworks/base/core/res/res/values/config.xml &nb
- hystrixDashboard服务监控除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboar
- 1. 运算符是什么?1.1 定义:对常量和变量进行运算操作的符号程序对数据进行运算时要用运算符1.2 常见运算符的概述1.3 表达式1.3.
- 一、整合原理activiti的配置文件本身就是一个spring的配置文件,但默认情况下只讲ProcessEngineConfiguratio
- 引言对使用 lombok 还是有很多争议的,有些公司不建议使用,有些公司又大量使用。我们的想法是:可以使用,但是不要滥用。什么是 lombo
- 前言:最近对接了一个第三方的项目,该项目的数据传输格式是XML。由于工作多年只有之前在医疗行业的时候有接触过少量数据格式是XML的接口,之后
- 目录1.堆空间的基本结构:2.空间分配担保机制3.如何判断一个对象已经无效4 不可达的对象并非“非死不可”5 如何判断一个常量是废弃常量?6
- 一:hibernate-validator 基础1. 简介:通过使用注解Annotations 给类或者类的属性加上约束(constrain
- /// <summary>/// 获取数据缓存/// </summary>/// <param name=&q
- 暂停和恢复Activity(Pausing and Resuming an Activity)一个Activity是一个应用程序组件,提供一
- 前言首先,事务这个概念是数据库层面的,Spring只是基于数据库中的事务进行扩展,以及提供了一些能让程序员更新方便操作事务的方式Spring
- 一、在spring的应用中我们存在两种过滤的用法,一种是 * 、另外一种当然是过滤器。我们这里介绍过滤器在springboot的用法,在sp