Have you ever wanted to be able to display the __file__ name, __line__ number and __method__ name from within Java class for quick debugging? Well, perhaps the following utility will help:
Save the following code in a file name __.java
package org.cnci.util;
/*
* Simple utility class to display __LINE__ number, __FILE__, __METHOD__ and __CLASS__ name for debugging...
* @author TA Nguyen
*/
public class __ {
// __.LINE__() similar to __line__ in C++
public static int LINE__() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
// __.METHOD__()
public static String METHOD__() {
return Thread.currentThread().getStackTrace()[2].getMethodName();
}
// __.CLASS__()
public static String CLASS__() {
return Thread.currentThread().getStackTrace()[2].getClassName();
}
// __.FILE__() similar to __file__ in C++
public static String FILE__() {
return Thread.currentThread().getStackTrace()[2].getFileName();
}
private static String testOne() {
return "testOne:: " + " File:" + __.FILE__() + " Class:" + __.CLASS__()
+ " Method:" + __.METHOD__() + " Line:" + __.LINE__();
}
public static void main(String[] args) {
String info = testOne();
System.out.println("1. " + info);
System.out.println("2. File:" + __.FILE__() + " Class:" + __.CLASS__()
+ " Method:" + __.METHOD__() + " Line:" + __.LINE__());
}
}