I am developing an application. As a part of this, when the Java program executes, we want to display line numbers of the source of the Java program where the code has executed. I’m using the Code class to get the line number of the methods in the code. But I want to get the line number of the source that have been executed,that is ,code path.
I will add to this, the program I have written to display the line numbers of the methods.
class LineNum
{
LineNumber[] ln = new LineNumber[300];
JavaClass clazz ;
Code classinfo ;
public static void printCode(Method[] methods) {
System.out.println("Entering PrintCode");
for(int i=0; i < methods.length; i++) {
System.out.println(methods[i]);
Code code = methods[i].getCode();
if(code != null) // Non-abstract method
{
System.out.println(code.getLineNumberTable());
}
}
}
public static void main(String[] args) {
LineNum liner = new LineNum();
liner.clazz = Repository.lookupClass("package_name.File_name");
printCode(liner.clazz.getMethods());
}
}
You can call
an any time which gives you the previously calls. The StackTraceElement class has a method getLineNumer() to get the line number.
http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html