Trying to solve a use case in Guice that's similar to Robot Legs, but the
existing solutions don't seem to map.

Use case (code below):
- Have an existing Robot class with @Injected constructor that takes a
RobotHeart
- Have existing RobotModule that provides a set of bindings for robot parts,
including a binding for RobotHeart
- Can't modify RobotModule or Robot
- I want to be able to inject a Robot instance with a different RobotHeart
binding, i.e.
@Inject @TitaniumHeart Robot;
creates a robot with a different injected heart.

The PrivateModule solution doesn't seem to work, because it would create a
duplicate key for RobotHeart.class and duplicate keys aren't allowed.

Any thoughts on how to implement?

Evan

Example:
  public static void main(String[] args) {
    // This works
    Injector i = Guice.createInjector(new RobotModule());
    Robot robby = i.getInstance(Robot.class);
    System.out.println("Heart: " + robby.heart.type);

    // Unsure how to make this work
    Injector i2 = Guice.createInjector(????); // Tried PrivateModule,
override modules and can't make it work cleanly
    Robot titaniumRobby = i.getInstance(Key.get(Robot.class,
Titanium.class));
    System.out.println("Heart: " + titaniumRobby.heart.type);
  }

  public static class RobotModule extends AbstractModule {
    @Override
    protected void configure() {}

    @Provides
    public RobotHeart provideRobotHeart() {
      return new RobotHeart("standard");
    }
    // + a lot more here
  }
  public static class Robot {
    public final RobotHeart heart;

    @Inject
    public Robot(RobotHeart heart) {
      // Also simplified for the example, would be a more complex
constructor
      this.heart = heart;
    }
  }
  public static class RobotHeart {
    public final String type;

    public RobotHeart(String type) {
      this.type = type;
    }
  }

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to