Reflection/Get source: Difference between revisions

No edit summary
Line 157:
 
Note that these mechanisms can be disabled (using [http://www.jsoftware.com/help/dictionary/dx003.htm 3!:6]).
 
=={{header|Java}}==
 
A java exception will contain a list of <code>StackTraceElement</code>'s. Each element is one method call in the call stack. The element contains information on the location of the code. Samples are shown below.
 
Note that the file name is not the absolute path on the file system, but is relative to the java CLASSPATH.
 
<lang Java>
 
public class ReflectionGetSource {
 
public static void main(String[] args) {
new ReflectionGetSource().method1();
 
}
public ReflectionGetSource() {}
public void method1() {
method2();
}
public void method2() {
method3();
}
public void method3() {
Throwable t = new Throwable();
for ( StackTraceElement ste : t.getStackTrace() ) {
System.out.printf("File Name = %s%n", ste.getFileName());
System.out.printf("Class Name = %s%n", ste.getClassName());
System.out.printf("Method Name = %s%n", ste.getMethodName());
System.out.printf("Line number = %s%n%n", ste.getLineNumber());
}
}
 
}
</lang>
{{out}}
<pre>
File Name = ReflectionGetSource.java
Class Name = ReflectionGetSource
Method Name = method3
Line number = 20
 
File Name = ReflectionGetSource.java
Class Name = ReflectionGetSource
Method Name = method2
Line number = 16
 
File Name = ReflectionGetSource.java
Class Name = ReflectionGetSource
Method Name = method1
Line number = 12
 
File Name = ReflectionGetSource.java
Class Name = ReflectionGetSource
Method Name = main
Line number = 5
</pre>
 
=={{header|JavaScript}}==