vipx / google-guice

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

ConvertedConstantBinding should have a getSourceKey() method #287

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Repro steps:

public class MyModule extends AbstractModule {
  protected void configure() {
    bindConstant().annotatedWith(Names.named("year")).to("1955");
  }
}

Binding<?> integerBinding = Guice.createInjector(new 
MyModule()).getBinding(Key.get(Integer.class, Names.named("year"));
integerBinding.acceptTargetVisitor(visitor);

What happens:

The visitor's #visitConvertedConstant(Object) method is called with the Integer 
object that was 
created from the "1955" String.

What I expect to happen:

#visitConvertedConstant should be called with arguments that let me get back to 
the original 
bindConstant() binding, perhaps by passing the Key for the original Binding.

Note: it may be that only Strings can be converted into constants, in which 
case I could assume 
that the Key will be for String.class, annotation, and you could close this bug.

Original issue reported on code.google.com by phopkins on 24 Dec 2008 at 6:26

GoogleCodeExporter commented 9 years ago
Yeah, only strings can be converted to constants. But I'll fix this...

Original comment by limpbizkit on 25 Dec 2008 at 4:33

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 25 Dec 2008 at 4:42

GoogleCodeExporter commented 9 years ago
If only Strings undergo conversion, is it correct for the EDSL doc in 
Binder.java to state:

bindConstant().annotatedWith(ServerHost.class).to(args[0]);

Sets up a constant binding.  Constant bindings are typeless in Guice; you
can provide the values in a variety of types and the values can be injected
in a variety of types; Guice performs the standard type conversions for you
behind the scenes.

Anecdotally, binding an Integer w/ a @Named and trying to @Inject it as a Float 
did not seem to be successful.

Original comment by phopkins on 26 Dec 2008 at 3:48

GoogleCodeExporter commented 9 years ago
Code fixed with r743. I fixed the Javadoc with r744:
 * <pre>
 *     bindConstant().annotatedWith(ServerHost.class).to(args[0]);</pre>
 *
 * Sets up a constant binding. Constant injections must always be annotated.
 * When a constant binding's value is a string, it is eligile for conversion to
 * all primitive types, to {@link Enum#valueOf(Class, String) all enums}, and to
 * {@link Class#forName class literals}. Conversions for other types can be
 * configured using {@link #convertToTypes(Matcher, TypeConverter)
 * convertToTypes()}.

Original comment by limpbizkit on 28 Dec 2008 at 12:09