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

22 views
Skip to first unread message

googl...@googlecode.com

unread,
Sep 25, 2014, 5:32:29 AM9/25/14
to googl...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 200 by rx303...@gmail.com: Reduce code bloat for multiple
instances of the same PrivateGinModule
https://code.google.com/p/google-gin/issues/detail?id=200

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
@Inject
Service1 service1;
@Inject
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)
{
this.param=param;
}

public MyPrivateGinModule<T> setImplementation(Class<? extends
MyInterface<T>> implementation)
{
this.implementation=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()
{
install(new
MyPrivateGinModule<Integer>(Integer.class).setImplementation(MyInterfaceIntegerImpl.class));//
now we can @Inject MyInterface<Integer>
install(new
MyPrivateGinModule<String>(String.class).setImplementation(MyInterfaceStringImpl.class));//
now we can @Inject MyInterface<String>
install(new
MyPrivateGinModule<Double>(Double.class).setImplementation(MyInterfaceDoubleImpl.class));//
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:
https://code.google.com/hosting/settings
Reply all
Reply to author
Forward
0 new messages