0

Say I have a class called MyClass, within this class, I want to find out the class name that instantiated an object of MyClass, for example:

class MyClass {
    final String whoCreatedMe;
    public MyClass() {
        whoCreatedMe = ???
    }
}

public class Driver {
    public static void main(String[] args) {
        System.out.println(new MyClass().whoCreatedMe); // should print Driver
    }
}
bohanl
  • 1,885
  • 4
  • 17
  • 33
  • 2
    Strings are immutable - there simply isn't any method that would enable you to change a String (unless you use Reflection - and go to hell). – Jordi Castilla Jul 02 '15 at 07:25
  • Lets step back: why do you think you want to do that? What is the problem that you actually want to solve? – GhostCat Jul 02 '15 at 07:47

4 Answers4

3

This is not advisable, and probably breaks in the most unexpected (and expected) ways. So I hope you're not going to use this in production code.

public class Temp {

    static class TestClass {
        public final String whoCreatedMe;
        public TestClass() {
            StackTraceElement ste = Thread.getAllStackTraces().get(Thread.currentThread())[3];
            whoCreatedMe = ste.getClassName();
        }
    }

    public static void main(String[] args) throws Exception {

        System.out.println(new TestClass().whoCreatedMe);
    }
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

pass Caller class name in constructor..

class MyClass {
    String whoCreatedMe;
    public MyClass() {
    }
    public MyClass(String mCallerClass) {
        this.whoCreatedMe = mCallerClass;
        System.out.println(this.whoCreatedMe+" instantiated me..");
    }
}

public class Driver {
    public static void main(String[] args) {
        System.out.println(new MyClass(this.getClass().getName())); // should print Driver but no quotes should be there in parameter
    }
}
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
Kushal
  • 8,100
  • 9
  • 63
  • 82
0

In plain Java, you could try to use the Stack Trace to get that kind of information. Of course, you should not hard-code 2, and it is probably quite fragile, and it may not work when you are using proxies, interceptors etc. you could put this in the constructor

        StackTraceElement[] stackTrace =  Thread.currentThread().getStackTrace();
        System.out.println(stackTrace[2].getClassName());

As for google-reflections which you mentioned in your tags, I don't think it supports such operations as this is not reflection.

user140547
  • 7,750
  • 3
  • 28
  • 80
0

I would not suggest this method but, if you really want to do that, here is a working example that I could think of.

public class Driver {
     public static void main(String[] args) {
            System.out.println(new MyClass().whoCreatedMe); // should print Driver
        }
}


public class MyClass {

    public String whoCreatedMe;

    public MyClass(){
        Exception ex = new Exception();
        StackTraceElement[] stackTrace = ex.getStackTrace();
        whoCreatedMe = stackTrace[1].getClassName();
    }

}
Evil Toad
  • 3,053
  • 2
  • 19
  • 28