4

Imagine you have a PlayN game which uses Guice bindings in its core project:

/* PlayN core Guice module */
public class MyGameModule extends AbstractModule {

    @Override
    protected void configure() {
        bindConstant().annotatedWith(Title.class).to("My game title");
        // Other bindings...
    }

}

What if you want to use those same bindings, but on your GWT project of the game (with Gin)?

In the Java project it's as easy as it gets:

/* PlayN Java Guice module */
public class MyGameModuleJava extends MyGameModule {

    // **OK**: no manual intervention required.

}

Can this be achieved without having to manually copy-paste the Guice module's configure method to the Gin module? For example, this works as a workaround, but is not what I'd prefer to do:

/* PlayN GWT Gin module */
public class MyGameModuleHtml extends AbstractGinModule {

    @Override
    protected void configure() {
        // **NOK**: copy-paste the Guice module, as shown below.
        bindConstant().annotatedWith(Title.class).to("My game title");
        // Other bindings...
    }

}

It works, but it's not pretty to say the least.

Bottom line: I want to use same bindings across all PlayN projects.

Solution

So what I did is use the GinModuleAdapter in my Java main, based on the answer:

Injector injector = Guice.createInjector(new GinModuleAdapter(new MyGameModuleHtml()));

This way I could remove the Guice bindings in the core classes as well.

levivanzele
  • 726
  • 1
  • 13
  • 33

1 Answers1

2

You should try to do it in reverse. E.g. bindings will be defined in GWT project and used in Guice via GinModuleAdapter (it will allow you to create Guice module from Gin module)

jusio
  • 9,850
  • 1
  • 42
  • 57