fmgasparino / google-gin

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

Binding a class with an annotation in a Singleton scope does not compile without a redundant to(…) #125

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1.  Create an annotation:

import com.google.inject.BindingAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface MyAnnotation {
}

2.  Attempt to bind a class with that annotation in the Singleton scope:

    bind(MyUiWidget.class).annotatedWith(MyAnnotation.class).in(Singleton.class);

3.  Compilation fails with:

Invoking generator com.google.gwt.inject.rebind.GinjectorGenerator
               [ERROR] No implementation bound for key Key[type=com.example.MyUiWidget, annotation=@com.example.MyAnnotation]

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

This should work.  If I include a redundant to(…), then it works:

    bind(MyUiWidget.class).annotatedWith(MyAnnotation.class).to(MyUiWidget.class).in(Singleton.class);

What version of the product are you using? On what operating system?

r146, Ubuntu

Original issue reported on code.google.com by mapa...@google.com on 3 Nov 2010 at 12:10

GoogleCodeExporter commented 9 years ago
Strange, this should work, in all its variations. I'll take a look today.

Original comment by aragos on 3 Nov 2010 at 4:24

GoogleCodeExporter commented 9 years ago
Turns out this problem exists because Guice doesn't support untargetted 
annotated bindings. In pure Guice, the below test yields and error. I'll follow 
up with the Guice guys whether this is intentional.

  public void testUntargettedAnnotatedBinding() {
    String annotated = Guice.createInjector(new AbstractModule() {

      @Override
      protected void configure() {
        bind(String.class).annotatedWith(Names.named("foo"));
      }
    }).getInstance(Key.get(String.class, Names.named("foo")));

    assertNotNull(annotated);
  }

1) No implementation for java.lang.String annotated with 
@com.google.inject.name.Named(value=foo) was bound.
  at com.google.gwt.inject.rebind.GuiceTest$1.configure(GuiceTest.java:33)

Original comment by aragos on 3 Nov 2010 at 5:52

GoogleCodeExporter commented 9 years ago
Appears also that "redundant" by virtue of the existence of an @ImplementedBy 
annotation on the target interface also has this problem. e.g.

bind(IMyWidgetUI.class).annotatedWith(Names.named("blah")).to(MyWidgetUI.class).
in(Singleton.class);

where:

@ImplementedBy(MyWidgetUI.class)
public interface IMyWidget {
...
}

Original comment by craig...@gmail.com on 21 Dec 2010 at 8:20

GoogleCodeExporter commented 9 years ago
I'll mark this issue as "Won't fix" for now since Guice exhibits the same 
behaviour and Gin generally follows Guice. Please take this up with the Guice 
guys if you'd like to get it fixed.

Original comment by aragos on 21 Jan 2011 at 9:23