How would you get a reference to an executing class several stack frames above the current one? For example, if you have:
Class a { foo() { new b().bar(); } } Class b { bar() { ... } }
Is there a way to get the value that would be retrieved by using ‘this’ in foo() while the thread is executing bar()?
No, you can’t. In all the languages that use a stack that I know of, the contents of other stack frames are hidden from you. There are a few things you can do to get it, beyond the obvious passing it as a parameter. One of the aspect oriented frameworks might get you something. Also, you can get a bit of debugging info from
Thread.getStackTrace().