title: Is it possible to know who (what type) asked the dependency?
body: <p>I am using an <a href="http://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/spi/InjectionListener.html" rel="nofollow noreferrer">Injection Listener</a> in order to make a post-inject initiation. Let's say that I create this annotation:</p>
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  public static @interface PrintValue {
      String value() default &quot;&quot;;
  }
  </code></pre>
  <p>Now I want every time an object of a <code>PrintValue</code> class to print this value. For example if the class is like this:</p>
  <pre><code>@PrintValue(&quot;Something&quot;)
  public static class Foo {
  }
  </code></pre>
  <p>, every time a Foo is being inejcted, my &quot;post-inject&quot; initiation is to print its value. However, I want to add an exception to this. I have tried a lot of things, but I was not able to achieve it.</p>
  <p>If a class looks like this:</p>
  <pre><code>@PrintValue(&quot;Something else&quot;)
  public static class Bar {
  
      @Inject
      public Bar(Foo foo) {
          System.out.println(&quot;Constructed a bar&quot;);
      }
  }
  </code></pre>
  <p>I don't want Foo's post initiation to take place, because the object that asks the foo, is an object of a <code>PrintValue</code> class. In other words, this &quot;Something else&quot; &quot;@Overrides&quot; the <code>PrintValue</code> of Foo. The only way I can think of that makes me achieve it, is to get into the listener, the class of the object that asked for the dependency. See the full example and <strong>notice the comment in the <code>InjectionListener</code></strong>:</p>
  <pre><code>public class GuiceListener {
  
      @Retention(RetentionPolicy.RUNTIME)
      public static @interface PrintValue {
          String value() default &quot;&quot;;
      }
  
      @PrintValue(&quot;Something&quot;)
      public static class Foo {
  
      }
  
      @PrintValue(&quot;Something else&quot;)
      public static class Bar {
  
          @Inject
          public Bar(Foo foo) {
              System.out.println(&quot;Constructed a bar&quot;);
          }
      }
  
      public static void main(String[] args) {
          Module module = new AbstractModule() {
              @Override
              protected void configure() {
                  bind(Foo.class);
                  bind(Bar.class);
                  bindListener(Matchers.any(), new TypeListener() {
  
                      @Override
                      public &lt;I&gt; void hear(TypeLiteral&lt;I&gt; type, TypeEncounter&lt;I&gt; encounter) {
                          if (type.getRawType().isAnnotationPresent(PrintValue.class)) {
                              encounter.register(new PrintValueInjectionListener());
                          }
                      }
  
                  });
              }
          };
          Injector injector = Guice.createInjector(module);
          Foo foo = injector.getInstance(Foo.class);
  
          Bar bar = injector.getInstance(Bar.class);
      }
  
      public static class PrintValueInjectionListener implements InjectionListener&lt;Object&gt; {
  
          @Override
          public void afterInjection(Object injectee) {
              //get here who asked (its class) the injectee ??
              PrintValue printValue = injectee.getClass().getAnnotation(PrintValue.class);
              System.out.println(printValue.value());
          }
  
      }
  }
  </code></pre>
  <p>If I run this, I get as output:</p>
  <pre><code>Something
  Something
  Constructed a bar
  Something else
  </code></pre>
  <p>But the output I want is:</p>
  <pre><code>Something (printed because of the Foo foo = injector.getInstance(Foo.class))
  Constructed a bar
  Something else
  </code></pre>
  <p>Is it possible?</p>