Revision: 1217
Author: [email protected]
Date: Thu Aug 26 15:36:19 2010
Log: fix requireExplicitBindings so TypeLiteral<T> can be injected.
http://code.google.com/p/google-guice/source/detail?r=1217
Modified:
/trunk/src/com/google/inject/internal/InjectorImpl.java
/trunk/test/com/google/inject/JitBindingsTest.java
=======================================
--- /trunk/src/com/google/inject/internal/InjectorImpl.java Sat Jul 3
08:51:31 2010
+++ /trunk/src/com/google/inject/internal/InjectorImpl.java Thu Aug 26
15:36:19 2010
@@ -211,7 +211,8 @@
throws ErrorsException {
- if(options.jitDisabled && jitType == JitLimitation.NO_JIT
&& !isProvider(key)) {
+ boolean jitOverride = isProvider(key) || isTypeLiteral(key);
+ if(options.jitDisabled && jitType == JitLimitation.NO_JIT
&& !jitOverride) {
throw errors.jitDisabled(key).toException();
}
@@ -226,7 +227,7 @@
}
}
- if(options.jitDisabled && jitType !=
JitLimitation.NEW_OR_EXISTING_JIT && !isProvider(key)) {
+ if(options.jitDisabled && jitType !=
JitLimitation.NEW_OR_EXISTING_JIT && !jitOverride) {
throw errors.jitDisabled(key).toException();
} else {
return createJustInTimeBindingRecursive(key, errors);
@@ -238,6 +239,10 @@
private static boolean isProvider(Key<?> key) {
return key.getTypeLiteral().getRawType().equals(Provider.class);
}
+
+ private static boolean isTypeLiteral(Key<?> key) {
+ return key.getTypeLiteral().getRawType().equals(TypeLiteral.class);
+ }
private static <T> Key<T> getProvidedKey(Key<Provider<T>> key, Errors
errors) throws ErrorsException {
Type providerType = key.getTypeLiteral().getType();
=======================================
--- /trunk/test/com/google/inject/JitBindingsTest.java Mon Jun 21 18:15:42
2010
+++ /trunk/test/com/google/inject/JitBindingsTest.java Thu Aug 26 15:36:19
2010
@@ -1,8 +1,12 @@
package com.google.inject;
import static com.google.inject.Asserts.assertContains;
+import static com.google.inject.internal.util.ImmutableSet.of;
+
import junit.framework.TestCase;
+import java.util.Set;
+
/**
* Some tests for {...@link InjectorBuilder#requireExplicitBindings()}
*
@@ -302,6 +306,22 @@
assertTrue(expected.getMessage(), !expected.getMessage().contains("2) "));
}
}
+
+ public void testTypeLiteralsCanBeInjected() {
+ Injector injector = new InjectorBuilder()
+ .requireExplicitBindings()
+ .addModules(new AbstractModule() {
+ @Override protected void configure() {
+ bind(new TypeLiteral<WantsTypeLiterals<String>>() {});
+ bind(new TypeLiteral<Set<String>>() {}).toInstance(of("bar"));
+ }
+ })
+ .build();
+
+ WantsTypeLiterals<String> foo = injector.getInstance(new
Key<WantsTypeLiterals<String>>() {});
+ assertEquals(foo.literal.getRawType(), String.class);
+ assertEquals(of("bar"), foo.set);
+ }
private void ensureWorks(Injector injector, Class<?>... classes) {
for(int i = 0; i < classes.length; i++) {
@@ -387,4 +407,15 @@
return new ProvBy() {};
}
}
-}
+
+ private static class WantsTypeLiterals<T> {
+ TypeLiteral<T> literal;
+ Set<T> set;
+
+ @Inject WantsTypeLiterals(TypeLiteral<T> literal, Set<T> set) {
+ this.literal = literal;
+ this.set = set;
+
+ }
+ }
+}
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.