CODE-常用程式碼片段-顯示程式碼行數

  • 95
  • 0

CODE-常用程式碼片段-顯示程式碼行數

This.java

public class This {
    private static final Throwable throwable = new Throwable();
    public static synchronized String getLineMessage() {
        throwable.fillInStackTrace();
        StackTraceElement[] elements = throwable.getStackTrace();
        if(elements == null || elements.length <= 1)
            return null;
        else
        return  "印出"+elements[1].getFileName()+"--"+elements[1].getMethodName()+"--"+elements[1].getLineNumber();
    }

USE

 System.out.println(This.getLineMessage());