frjaeger220 / google-guice

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

Wrong scope visiting for LinkedKeyBinding #360

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello all,
I have a filling, Guice have a bug in scope visiting for LinkedKeyBinding if 
target is scoped by annotation.

My steps:
1) create interface
2) create class and implement interface
3) annotate by @Singleton
4) bind interface to class
5) try to accept scoping visitor for LinkedKeyBinding
6) visitNoScoping method was called instead of visitScope. 

If I bind only class, visitScope method is called correctly (see example).
If I bind interface to class and it's scoped by in(Scopes.SINGLETON) visitScope 
methods is called correctly.

Guice version - current trunk.

My test:

public class LinkedKeyVisiting {

  public static void main(String[] args) {
    Injector inj = Guice.createInjector(new AbstractModule() {
      @Override protected void configure() {
        bind(Interfaze.class).to(WithInterface.class);
        bind(WithoutInterface.class);
        bind(Visitor.class).asEagerSingleton();
      }
    });
  }

  static interface Interfaze {}

  @Singleton
  static class WithInterface implements Interfaze {}

  @Singleton
  static class WithoutInterface {}

  static class Visitor {

    @Inject
    public void visit(Injector inj) {

      final Binding<?> withoutInterfaceBinding = inj.getBinding(WithoutInterface.class);
      withoutInterfaceBinding.acceptScopingVisitor(new DefaultBindingScopingVisitor<Void>() {
        @Override public Void visitScope(Scope scope) {
          System.out.println(withoutInterfaceBinding.getKey() + " in scope " + scope);
          return null;
        }

        @Override public Void visitNoScoping() {
          System.out.println(withoutInterfaceBinding.getKey() + " without scope ");
          return null;
        }
      });

      final Binding<Interfaze> withInterfaceBinding = inj.getBinding(Interfaze.class);
      withInterfaceBinding.acceptTargetVisitor(new DefaultBindingTargetVisitor<Interfaze, Void>() {

        @Override
        public Void visit(final LinkedKeyBinding<? extends Interfaze> linkedKeyBinding) {
          linkedKeyBinding.acceptScopingVisitor(new DefaultBindingScopingVisitor<Void>() {
            public Void visitScope(Scope scope) {
              System.out.println(linkedKeyBinding.getKey() + " in scope " + scope);
              return null;
            }

            public Void visitNoScoping() {
              System.out.println(linkedKeyBinding.getKey() + " without scope ");
              return null;
            }
          });
          return null;
        }
      });

    }
  }
}

Class output:

Key[type=com.google.inject.LinkedKeyVisiting$WithoutInterface, 
annotation=[none]] in scope Scopes.SINGLETON
Key[type=com.google.inject.LinkedKeyVisiting$Interfaze, annotation=[none]] 
without scope 

It's looks like a bug.

Original issue reported on code.google.com by aleksey....@gmail.com on 17 Apr 2009 at 8:51

GoogleCodeExporter commented 9 years ago
Actually this is working as designed. To compute the effective scope on a 
linked binding, you need to follow 
all of the links and take their intersection.

For example, consider:
  bind(ArrayList.class).in(Scope1.class);
  bind(List.class).to(ArrayList.class).in(Scope2.class);
  bind(Collection.class).to(List.class).in(Scope3.class);

It wouldn't be unreasonable to create a helper method to aggregate all of the 
scopes for a binding. 

Original comment by limpbizkit on 22 Apr 2009 at 4:16