I need some way to get an instance of a class that called some method. For example, in this program, I want to get the instance of Person
(named Jack here) that called method Main.call()
.
public class Main {
public static Person p;
public static void main(String[] args) {
p = new Person("Jack");
p.call();
}
public static void call() {
Object caller = Magic.getTrace()[1].getObject(); // This is what I need
System.out.println(((Person)caller).getName()); // Output: Jack
Class<?> clazz = Magic.getTrace()[2].getClass(); // This too
System.out.println(clazz.getName()); // Output: Main
}
public class Person {
private String name;
public Person(String _name) {
name = _name
}
public String getName() { return name; }
public void call() {
Main.call();
}
}
So, is there any way to accomplish that? It should be possible because debuggers can use that.
For mods: I don't want the NAME of the class, but the INSTANCE...
Thanks!