Issue 200 in google-gin: Reduce code bloat for multiple instances of the same PrivateGinModule

Skip to first unread message

Sep 25, 2014, 5:32:29 AM9/25/14
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 200 by Reduce code bloat for multiple
instances of the same PrivateGinModule

What steps will reproduce the problem?

1. Create a hierarchy of classes which you need to bind to the same
interface with different parameterizing type:

public interface MyInterface<T>
void doSomething();

public class MyInterfaceImpl<T> implements MyInterface<T>
//External services (usually singletons), defined in a global GinModule
Service1 service1;
Service2 service2;

//some base logic

public class MyInterfaceStringImpl extends MyInterfaceImpl<String>
//some logic for String implementation

public class MyInterfaceIntegerImpl extends MyInterfaceImpl<Integer>
//some logic for Integer implementation

public class MyInterfaceDoubleImpl extends MyInterfaceImpl<Double>
//some logic for Double implementation

2. Make PrivateGinModule which exposes interface with different
parameterizing type:

public class MyPrivateGinModule<T> extends PrivateGinModule
private final Class<T> param;
private Class<? extends MyInterface<T>> implementation;

public MyPrivateGinModule(Class<T> param)

public MyPrivateGinModule<T> setImplementation(Class<? extends
MyInterface<T>> implementation)
return this;

protected void configure()
bind(typeLiteral(MyInterface.class, param)).to(implementation);
expose(typeLiteral(MyInterface.class, param));
//typeLiteral is utility method which makes
TypeLiteral<MyInterface<T>> during rebind phase using
Types.newParameterizedType() and TypeLiteral.get()

3. Install several instances of that module:

public class MyGlobalGinModule extends AbstractGinModule
protected void configure()
now we can @Inject MyInterface<Integer>
now we can @Inject MyInterface<String>
now we can @Inject MyInterface<Double>

4. Compile the code.

What is the expected output? What do you see instead?

Each installed instance of MyPrivateGinModule generates fragments for
dependencies defined inside - that's OK.
But also it generates fragments for all external dependencies (Service1 and
Service2) which do not differ from one MyPrivateGinModule instance bindings
to another.
That causes a lot of duplication in generated code and increases total size
of compiled js-code.

You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
Reply all
Reply to author
0 new messages