dimanche 19 juillet 2015

Get Class Type from Reflection Java

When I call on a method in a class, the method will get the java.lang.Class that called on it using sun.reflect.Reflection.getCallerClass(2). This is not what I want. I want the Reflection to return the class Object that called on it (i.e. if I call the method from the Bar class, the Reflection.getCallerClass() returns an object of type Bar)

Let's suppose I have this class:

public class Foo {
    public static void printOutCallerObject() {
        System.out.println(classTypeThatCalledOnMethod);
    }
}

Called by:

public class Bar {
    public static void main(String[] args) {
        Foo.printOutCallerObject();
    }
}

And then the program would print out "Bar".





Aucun commentaire:

Enregistrer un commentaire