yukihane / google-gin

Automatically exported from code.google.com/p/google-gin
Apache License 2.0
0 stars 0 forks source link

GIN does not respect injection order when subclassing #199

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
According to the Guice injection points document at:
https://github.com/google/guice/wiki/InjectionPoints

injection methods of super classes are called before the methods of a subclass.

Here is some small unit test for Guice that shows that the correct order is 
first ThingA.inject() and then ThingB.anotherInject().

If you use GIN, ThingB.anotherInject() is invoked before ThingA.inject()

public class InjectInheritance {

  static class ThingA {
    boolean initialized;

    @Inject
    public void inject() {
      this.initialized = true;
      System.out.println("ThingA:inject called");
    }
  }

  static class ThingB extends ThingA {
    private boolean testOK = false;

    @Inject
    public void anotherInject() {
      testOK = this.initialized;
      System.out.println("ThingB:anotherInject called");
    }

    public boolean isSuccess() {
      return testOK;
    }
  }

  @Test
  public void guiceMethodInjectionInheritance() {
    Module module = new AbstractModule() {
      @Override
      protected void configure() {
      }
    };
    Injector guice = Guice.createInjector(module);
    ThingB b = guice.getInstance(ThingB.class);
    assertTrue(b.isSuccess());
  }

  // This part of the code needs to be executed by GIN.
  public void ginInheritance() {
    App app = GWT.create( App.class);
    ThingB b = app.createB();
    assertTrue(b.isSuccess());
  }

  @GinModules(MyGinModule.class)
  static interface App extends Ginjector {
    public ThingB createB();
  }

  static class MyGinModule extends AbstractGinModule {
    @Override
    protected void configure() {
    }
  }

What is the expected output? What do you see instead?
I expect this order of invocation
ThingA.inject();
ThingB.anotherInject();

However I get this in Gin:
ThingB.anotherInject();
ThingA.inject();

What version of the product are you using? On what operating system?
GWT 2.6.1 / GIN 2.1.2

Please provide any additional information below.

Original issue reported on code.google.com by david.nouls on 24 Sep 2014 at 2:21

GoogleCodeExporter commented 8 years ago
I tried with GIN 2.1.2 and GWT 2.7.0 and the problem still exists.
I wrote a simple unittest that exposes the problem.
It looks like the name of the method influences the orde in which the methods 
are injected.
So if my base class has something like injectZ and the derived class has 
injectA then injectA is done before injectZ. Which can cause issues if you 
method injection depends on the base class to be fully injected.
I attached my example unittest to show the problem.

Original comment by david.nouls on 30 Jan 2015 at 9:37

Attachments: