How to delegate creation of some classes from Guice injector to another factory? : <p>For instance, RESTEasy's ResteasyWebTarget class has a method <code>proxy(Class&lt;T&gt; clazz)</code>, just like Injector's <code>getInstance(Class&lt;T&gt; clazz)</code>. Is there a way to tell Guice that creation of some classes should be delegated to some instance?</p>

<p>My goal is the following behavior of Guice: when the injector is asked for a new instance of class A, try to instantiate it; if instantiation is impossible, ask another object (e. g. ResteasyWebTarget instance) to instantiate the class.</p>

<p>I'd like to write a module like this:</p>

<pre><code>@Override
protected void configure() {
    String apiUrl = "https://api.example.com";
    Client client = new ResteasyClientBuilder().build();
    target = (ResteasyWebTarget) client.target(apiUrl);

    onFailureToInstantiateClass(Matchers.annotatedWith(@Path.class)).delegateTo(target);
}
</code></pre>

<p>instead of</p>

<pre><code>@Override
protected void configure() {
    String apiUrl = "https://api.example.com";
    Client client = new ResteasyClientBuilder().build();
    target = (ResteasyWebTarget) client.target(apiUrl);

    bind(Service1.class).toProvider(() -&gt; target.proxy(Service1.class);
    bind(Service2.class).toProvider(() -&gt; target.proxy(Service2.class);
    bind(Service3.class).toProvider(() -&gt; target.proxy(Service3.class);
}
</code></pre>

<p>I've thought about implementing Injector interface and use that implementation as a child injector, but the interface has too much methods.</p>

<p>I <strong>can</strong> write a method enumerating all annotated interfaces in some package and telling Guice to use provider for them, but that's the backup approach.</p>
