Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Object Oriented programming in Java states that your service is scope for invocation, nothing else (forget <code>static</code>). So there is no normal way to find who is calling instance's method other than passing <code>S</code> instance as argument.</p> <p>But that does not mean it is impossible. </p> <p><strong>If you only need to know what is the type of caller</strong>, you can use <code>Thread.currentThread().getStackTrace()</code>:</p> <pre><code>StackTraceElement[] elements = Thread.currentThread().getStackTrace() StackTraceElement caller = elements[elements.length - 2]; printLn(caller.getClassName()); </code></pre> <p>As I said at the beginning it is totally counter objective Java code.</p> <p><strong>If you need to refer exact instance</strong>, you probably should add caller as call parameter. I assume that if you want to refer to caller, callee's code is written by you, so you are able to do it. <strong><em>As for me it would be best choice, because if you need caller in scope of callee, you should pass it directly.</em></strong> Other option is to set caller on ThreadLocal in <code>U</code>, but you don't have confidence that developer will do it each time.</p> <p><strong>If interface cannot be changed, and U is an interface</strong>, you could create <code>U</code> builder object:</p> <pre><code>public class UBuilder { public U getS(final S caller) { Proxy.newProxyInstance(getClass().getClassLoader(), U.class, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // store caller value on some ThreadLocal variable try { method.invoke(proxy, args); } finally { // remove caller from ThreadLocal variable } } } } } </code></pre> <p>After that you have additional invocation context (<code>ThreadLocal</code>) referring <code>S</code> instance.</p> <p><strong>If <code>U</code> is not interface</strong>, <code>Javassist</code>, <code>CgLib</code> or something similar would help, but only if invoked method is not static or final.</p> <p><strong>If <code>F</code> is static or final</strong> I see only dramatically hackish answers. Maybe creating own interface imitating <code>U</code>, and forwarding method invocations in <code>InvocationHandler</code> could be some way. Of course S should refer to this interface, not <code>U</code> directly.</p> <p>Hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload