On Friday, 2 January 2015 at 16:48, ghristov wrote:
> Bug Report
> I have the following situation where the code is throwing error because of no
> binding existence. In my scenario I used annotation. Following what's
> described in the documentation and I quote,
> > When specifying binding annotations, you must still add the target binding,
> > even it is the same concrete > class. For example:
> > bind(MyConcreteClass.class)
> > .annotatedWith(Names.named("foo"))
> > .to(MyConcreteClass.class);
> > bind(AnotherConcreteClass.class)
> > .annotatedWith(Names.named("foo"))
> > .to(AnotherConcreteClass.class)
> > .in(Singleton.class);
> there is a mismatch.
> public class Main { public static void main(String[] args) { Injector
> injector = Guice.createInjector(new CalculatorModule()); SimpleCalculator
> calculator = injector.getInstance(SimpleCalculator.class);
> calculator.calculate(4,6,"+"); } }
>
> public class SimpleCalculator { private Calculation addition; @Inject public
> SimpleCalculator(@Addition Calculation addition) { this.addition = addition;
> } public Integer calculate(Integer first,Integer second,String sign) {
> if("+".equals(sign)){ return addition.applyOn(first, second); } return 0; } }
>
> @BindingAnnotation @Target({ ElementType.METHOD, ElementType.FIELD,
> ElementType.PARAMETER, ElementType.CONSTRUCTOR})
> @Retention(RetentionPolicy.RUNTIME) public @interface Addition {}
>
> @ImplementedBy(Sum.class) public interface Calculation { Integer
> applyOn(Integer first, Integer second); }
>
> public class CalculatorModule extends AbstractModule { @Override protected
> void configure() {
> bind(Sum.class).annotatedWith(Addition.class).to(Sum.class);
>
>
>
This should be:
bind(Calculation.class).annotatedWith(Addition.class).to(Sum.class);
to match the injection point in SimpleCalculator
> } }
>
> public class Sum implements Calculation { @Override public Integer
> applyOn(Integer first, Integer second) { return first+second; } }
>
> There is the following error when I try to run the application:
> > Exception in thread "main" com.google.inject.ConfigurationException: Guice
> > configuration errors:
> > 1) No implementation for bug.Calculation annotated with @bug.Addition() was
> > bound.
> > while locating bug.Calculation annotated with @bug.Addition()
> > for parameter 0 at bug.SimpleCalculator.(SimpleCalculator.java:13)
> > while locating bug.SimpleCalculator
> > 1 error
> > at
> > com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
> > at
> > com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
> > at
> > com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
> > at bug.Main.main(Main.java:12)
> > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > at
> > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> > at
> > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> > at java.lang.reflect.Method.invoke(Method.java:606)
> > at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
>
> —
> Reply to this email directly or view it on GitHub
> (https://github.com/google/guice/issues/890).
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> (mailto:[email protected]).
> To post to this group, send email to [email protected]
> (mailto:[email protected]).
> Visit this group at http://groups.google.com/group/google-guice-dev.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice-dev.
For more options, visit https://groups.google.com/d/optout.