Java中获取当前代码行行号的方法

最近的项目中,为了实现自定义的log类,能够输出具体的代码行行号,我通过使用StackTraceElement对象实现了。
具体内容请参考下面的Demo代码。这里指出需要注意的几个问题:
1. 程序中返回的代码行行号,是新建StackTrackElement对象的那一行。
2. 可以通过传参的方法实现输出特定行行号。具体实现见代码。
 1ExpandedBlockStart.gif/**
 2InBlock.gif * 
 3ExpandedBlockEnd.gif */

 4None.gifpackage leo.demo.training;
 5None.gif
 6ExpandedBlockStart.gifContractedBlock.gif/**
 7InBlock.gif * Get current java file name and current code line number
 8InBlock.gif * @author Leo Xie
 9ExpandedBlockEnd.gif */

10ExpandedBlockStart.gifContractedBlock.gifpublic class CurrentLine {
11ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
12InBlock.gif     * @param args
13ExpandedSubBlockEnd.gif     */

14ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args) dot.gif{
15InBlock.gif        StackTraceElement ste1 = null;
16InBlock.gif        
17InBlock.gif        // get current thread and its related stack trace
18InBlock.gif        StackTraceElement[] steArray = Thread.currentThread().getStackTrace();
19InBlock.gif        int steArrayLength = steArray.length;
20InBlock.gif
21InBlock.gif        String s = null;
22InBlock.gif        
23InBlock.gif        // output all related info of the existing stack traces
24ExpandedSubBlockStart.gifContractedSubBlock.gif        if(steArrayLength==0dot.gif{
25InBlock.gif            System.err.println("No Stack Trace.");
26ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
27ExpandedSubBlockStart.gifContractedSubBlock.gif            for (int i=0; i<steArrayLength; i++dot.gif{
28InBlock.gif                System.out.println("Stack Trace-" + i);
29InBlock.gif                ste1 = steArray[i];
30InBlock.gif                s = ste1.getFileName() + ": Line " + ste1.getLineNumber();
31InBlock.gif                System.out.println(s);
32ExpandedSubBlockEnd.gif            }

33ExpandedSubBlockEnd.gif        }

34InBlock.gif        // the following code segment will output the line number of the "new " clause
35InBlock.gif        // that's to say the line number of "StackTraceElement ste2 = new Throwable().getStackTrace()[0];"
36InBlock.gif        StackTraceElement ste2 = new Throwable().getStackTrace()[0];
37InBlock.gif        System.out.println(ste2.getFileName() + ": Line " + ste2.getLineNumber());
38InBlock.gif        // the following clause will output the line number in the external method "getLineInfo()"
39InBlock.gif        System.out.println(getLineInfo());
40InBlock.gif        // the following clause will output its current line number
41InBlock.gif        System.out.println(getLineInfo(new Throwable().getStackTrace()[0]));
42ExpandedSubBlockEnd.gif    }

43InBlock.gif    
44ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
45InBlock.gif     * return current java file name and code line number
46InBlock.gif     * @return String
47ExpandedSubBlockEnd.gif     */

48ExpandedSubBlockStart.gifContractedSubBlock.gif    public static String getLineInfo() dot.gif{
49InBlock.gif        StackTraceElement ste3 = new Throwable().getStackTrace()[0];
50InBlock.gif        return (ste3.getFileName() + ": Line " + ste3.getLineNumber());
51ExpandedSubBlockEnd.gif    }

52InBlock.gif    
53ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
54InBlock.gif     * return current java file name and code line name
55InBlock.gif     * @return String
56ExpandedSubBlockEnd.gif     */

57ExpandedSubBlockStart.gifContractedSubBlock.gif    public static String getLineInfo(StackTraceElement ste4) dot.gif{
58InBlock.gif        return (ste4.getFileName() + ": Line " + (ste4.getLineNumber()));
59ExpandedSubBlockEnd.gif    }

60ExpandedBlockEnd.gif}
ContractedBlock.gif /** */ /** */ dot.gif

转载于:https://www.cnblogs.com/xxpal/articles/1209378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值