Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:32:45+00:00 2026-05-25T20:32:45+00:00

Suppose I have 5 classes A, B, C, D, E that all implement a

  • 0

Suppose I have 5 classes A, B, C, D, E that all implement a common interface X. Each of the classes B, C, D and E has a field of type X (so they can be seen as wrapper classes).

Which instances are created is determined at runtime, so I could have for example one of the following object graphs:

E -> D -> C -> B -> A
D -> B -> A
E -> A
A

(The order is fixed and the innermost instance is always of type A, but otherwise there are no restrictions.)

No I’d like to create this objects with Guice to avoid providing all their other dependencies by hand.

What’s the easiest way to do this? Currently it seems that I have to

  1. let Guice create instance of A
  2. create a module that binds X.class to this instance and a child injector with this additional module
  3. let Guice create the next instance (e.g., of type B)
  4. repeat 2., now binding X.class to the instance of B
  5. repeat 3. and 4. until all objects are created

Is there an easier way? Could I perhaps somehow automatically register a new instance of a subclass of X as a binding for X each time it is created?

Edit: Clarification of “Which instances are created is determined at runtime“:

My current code looks like this:

X createX() {
  X x = new A(dependencies);
  if (useB) x = new B(x, some, dependencies);
  if (useC) x = new C(x, further, dependencies);
  if (useD) x = new D(x, dependency);
  if (useE) x = new E(x, yet, other, dependencies);
  return x;
}

The value of the flags useB, useC, useD, and useE comes from a properties file.

My main goal is to save providing all the dependencies to the constructors manually.

Edit: Solution:

I have added my own solution which I have found in the meantime. Thanks to all answerers!

A way to improve my solution would be to make it possible to remove the @InInstance annotation on the constructor parameters. I have experimented with type listeners, but I haven’t found a way to do this. Hints would be welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:32:46+00:00Added an answer on May 25, 2026 at 8:32 pm

    I have developed the following solution to the problem:

    First, I create a marker annotation @InInstance, which takes a Class object as value. I use this annotation to annotate the parameters of type X in all wrapper classes (i.e., B to E). Example:

    class B {
      B(@InInstance(B.class) X x,
        Other dependencies) {
      ...
      }
    }
    

    Now if I want for example an instance of the full chain (A to E), I bind

    • X.class to E.class
    • X.class annotatedWith InInstance(E.class) to D.class
    • …
    • X.class annotatedWith InInstance(B.class) to A.class

    and then call createInstance(X.class).

    This solves the problem of letting Guice create my instances and provide the dependencies.

    Now for the problem of creating the appropriate bindings at runtime according to the properties file, I created a Guice extension that allows me to create “wrapper bindings”. Such a binding takes a type (here X), a wrapper implementation type (here one of B to E) and a base module, which needs to contain a binding for X. It then creates a new module that contains two bindings:

    • the same binding for X as in the base module, but annotatedWith(InInstance(wrapper type))
    • a binding from X to the wrapper type

    Such a module can then be used to override the bindings of the base module with Modules.override(base module).with(wrapper module).

    With some nice Guice-style EDSL, this looks like:

    Module baseModule = ... // contains binding for X to A
    
    if (useB) {
      Module wrapperModule = new ChainingModule() {
        void configure() {
          wrap(X.class).from(baseModule).with(B.class); // possible to use .in(...) etc. here
          // In this case, this line is equivalent to
    
          // bind(X.class).annotatedWith(InInstance(B.class)).to(A.class);
          // bind(X.class).to(B.class);
    
          // It has the advantage of automatically looking up the current binding
          // for X in the base module, which makes it easy to chain this.
        }
      }
      baseModule = Modules.override(baseModule).with(wrapperModule);
    }
    
    ...
    

    And with some helper methods for common cases it looks like:

    Module module = ... // contains binding for X to A
    if (useB) {
      module = Chaining.wrap(X.class).from(module).with(B.class);
    }
    if (useC) {
      module = Chaining.wrap(X.class).from(module).with(C.class);
    }
    if (useD) {
      module = Chaining.wrap(X.class).from(module).with(D.class);
    }
    if (useE) {
      module = Chaining.wrap(X.class).from(module).with(E.class);
    }
    
    Injector injector = Guice.createInjector(module);
    X x = injector.createInstance(X.class);
    

    Each line creates the appropriate wrapper module and returns a new module containing the given module with some bindings overriden by the wrapper module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have interface IFoo, and have several classes that implement it, what is
Suppose I have a number of related classes that all have a method like
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose classes 'Apple', 'Banana' and 'Orange' all inherit from 'Fruit'. Now suppose we have
Suppose that you have the following hierarchy of statistics-related classes, structured in a manner
Suppose I have three classes. It is valid to instantiate A, but there are
In Java, suppose I have 3 classes, C extends from B which extends from
suppose you have two (or more) classes with private member vectors: class A {
Suppose you have a collection of Foo classes: class Foo { public string Bar;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.