Is it possible to get information about class that invoking the other one?
class Bar{
public Bar{}
public String getInvokingClassInfo(){
return "...";
}
}
class Foo{
public Foo(){
Bar bar = new Bar();
System.out.println("Invoking class is: "+bar.getInvokingClassInfo());
}
}
How to get in the place:
System.out.println(bar.getInvokingClassInfo());
info about class that invoking (Foo) this one (Bar):
Invoking class: Foo
This is getting the stacktrace for the current thread. As noted in the comments, there are implementation differences, so if you fear such, you can implement something like this:
StackTraceElementarray (using a counter variable declared outside the loop)breakindexstands for in the above code)new Exception().getStackTrace()