In C# we have Caller Information
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
System.Diagnostics.Trace.WriteLine("message: " + message);
System.Diagnostics.Trace.WriteLine("member name: " + memberName);
System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
MSDN Link : CallerInfo
I want to know Java has equivalent annotations or not ?
these features can help us for better tracing
UPDATE :
I wrote a class :
public class Trace {
public static void trace() {
StackTraceElement[] stk = Thread.currentThread().getStackTrace();
System.out.println(String.format("LineNumber : %s, ClassName : %s, MethodName : %s, SourceLocation : %s",
stk[1].getLineNumber(), stk[1].getClassName(), stk[1].getMethodName(), stk[1].getFileName())
);
}
}
and Call trace() method :
public class Main {
public static void main(String[] args) throws Exception {
Trace.trace();
}
}
and RESULT :
LineNumber : 8, ClassName : Trace, MethodName : trace, SourceLocation : Trace.java
it is not true for me I want a result like this :
LineNumber : 3, ClassName : Main, MethodName : main, SourceLocation : Main.java
In fact I want to now how class and method call my trace() (The Parent)