Author: limpbizkit
Date: Sun Nov 30 11:52:02 2008
New Revision: 711
Modified:
trunk/extensions/multibindings/src/com/google/inject/multibindings/MapBinder.java
trunk/extensions/multibindings/src/com/google/inject/multibindings/Multibinder.java
Log:
New type safety for multibindings... I changed the methods to take
TypeLiteral<T>s and Class<T>s rather than unsafe Type objects.
Modified:
trunk/extensions/multibindings/src/com/google/inject/multibindings/MapBinder.java
==============================================================================
---
trunk/extensions/multibindings/src/com/google/inject/multibindings/MapBinder.java
(original)
+++
trunk/extensions/multibindings/src/com/google/inject/multibindings/MapBinder.java
Sun Nov 30 11:52:02 2008
@@ -27,8 +27,9 @@
import static
com.google.inject.multibindings.Multibinder.checkConfiguration;
import static com.google.inject.multibindings.Multibinder.checkNotNull;
import com.google.inject.util.Types;
+import static com.google.inject.util.Types.newParameterizedType;
+import static com.google.inject.util.Types.newParameterizedTypeWithOwner;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -94,69 +95,98 @@
private MapBinder() {}
/**
- * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED]
- * valueType} in a [EMAIL PROTECTED] Map} that is itself bound with no
binding
- * annotation.
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with no binding annotation.
*/
public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
- Type keyType, Type valueType) {
+ TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
binder = binder.skipSources(MapBinder.class, RealMapBinder.class);
return newMapBinder(binder, valueType,
- Key.get(MapBinder.<K, V>mapOf(keyType, valueType)),
- Key.get(MapBinder.<K, V>mapOfProviderOf(keyType, valueType)),
- Multibinder.<Entry<K, Provider<V>>>newSetBinder(
- binder, entryOfProviderOf(keyType, valueType)));
+ Key.get(mapOf(keyType, valueType)),
+ Key.get(mapOfProviderOf(keyType, valueType)),
+ Multibinder.newSetBinder(binder, entryOfProviderOf(keyType,
valueType)));
}
/**
- * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED]
- * valueType} in a [EMAIL PROTECTED] Map} that is itself bound with [EMAIL
PROTECTED]
annotation}.
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with no binding annotation.
+ */
+ public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
+ Class<K> keyType, Class<V> valueType) {
+ return newMapBinder(binder, TypeLiteral.get(keyType),
TypeLiteral.get(valueType));
+ }
+
+ /**
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with [EMAIL PROTECTED]
annotation}.
*/
public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
- Type keyType, Type valueType, Annotation annotation) {
+ TypeLiteral<K> keyType, TypeLiteral<V> valueType, Annotation
annotation) {
binder = binder.skipSources(MapBinder.class, RealMapBinder.class);
return newMapBinder(binder, valueType,
- Key.get(MapBinder.<K, V>mapOf(keyType, valueType), annotation),
- Key.get(MapBinder.<K, V>mapOfProviderOf(keyType, valueType),
annotation),
- Multibinder.<Entry<K, Provider<V>>>newSetBinder(
- binder, entryOfProviderOf(keyType, valueType), annotation));
+ Key.get(mapOf(keyType, valueType), annotation),
+ Key.get(mapOfProviderOf(keyType, valueType), annotation),
+ Multibinder.newSetBinder(binder, entryOfProviderOf(keyType,
valueType), annotation));
}
/**
- * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED]
- * valueType} in a [EMAIL PROTECTED] Map} that is itself bound with [EMAIL
PROTECTED]
annotationType}.
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with [EMAIL PROTECTED]
annotation}.
*/
- public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
- Type keyType, Type valueType, Class<? extends Annotation>
annotationType) {
+ public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
+ Class<K> keyType, Class<V> valueType, Annotation annotation) {
+ return newMapBinder(binder, TypeLiteral.get(keyType),
TypeLiteral.get(valueType), annotation);
+ }
+
+ /**
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with [EMAIL PROTECTED]
annotationType}.
+ */
+ public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
TypeLiteral<K> keyType,
+ TypeLiteral<V> valueType, Class<? extends Annotation>
annotationType) {
binder = binder.skipSources(MapBinder.class, RealMapBinder.class);
return newMapBinder(binder, valueType,
- Key.get(MapBinder.<K, V>mapOf(keyType, valueType), annotationType),
- Key.get(MapBinder.<K, V>mapOfProviderOf(keyType, valueType),
annotationType),
- Multibinder.<Entry<K, Provider<V>>>newSetBinder(
- binder, entryOfProviderOf(keyType, valueType),
annotationType));
+ Key.get(mapOf(keyType, valueType), annotationType),
+ Key.get(mapOfProviderOf(keyType, valueType), annotationType),
+ Multibinder.newSetBinder(binder, entryOfProviderOf(keyType,
valueType), annotationType));
+ }
+
+ /**
+ * Returns a new mapbinder that collects entries of [EMAIL PROTECTED]
keyType}/[EMAIL PROTECTED] valueType} in a
+ * [EMAIL PROTECTED] Map} that is itself bound with [EMAIL PROTECTED]
annotationType}.
+ */
+ public static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
Class<K> keyType,
+ Class<V> valueType, Class<? extends Annotation> annotationType) {
+ return newMapBinder(
+ binder, TypeLiteral.get(keyType), TypeLiteral.get(valueType),
annotationType);
}
- @SuppressWarnings("unchecked")
- private static <K, V> TypeLiteral<Map<K, V>> mapOf(Type keyType, Type
valueType) {
- Type type = Types.mapOf(keyType, valueType);
- return (TypeLiteral<Map<K, V>>) TypeLiteral.get(type);
+ @SuppressWarnings("unchecked") // a map of <K, V> is safely a Map<K, V>
+ private static <K, V> TypeLiteral<Map<K, V>> mapOf(
+ TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
+ return (TypeLiteral<Map<K, V>>) TypeLiteral.get(
+ Types.mapOf(keyType.getType(), valueType.getType()));
}
+ @SuppressWarnings("unchecked") // a provider map <K, V> is safely a
Map<K, Provider<V>>
private static <K, V> TypeLiteral<Map<K, Provider<V>>> mapOfProviderOf(
- Type keyType, Type valueType) {
- return mapOf(keyType, Types.newParameterizedType(Provider.class,
valueType));
+ TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
+ return (TypeLiteral<Map<K, Provider<V>>>) TypeLiteral.get(
+ Types.mapOf(keyType.getType(),
newParameterizedType(Provider.class, valueType.getType())));
}
- private static Type entryOfProviderOf(Type keyType, final Type
valueType) {
- return Types.newParameterizedTypeWithOwner(Map.class, Map.Entry.class,
keyType,
- Types.providerOf(valueType));
+ @SuppressWarnings("unchecked") // a provider entry <K, V> is safely a
Map.Entry<K, Provider<V>>
+ private static <K, V> TypeLiteral<Map.Entry<K, Provider<V>>>
entryOfProviderOf(
+ TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
+ return (TypeLiteral<Entry<K, Provider<V>>>)
TypeLiteral.get(newParameterizedTypeWithOwner(
+ Map.class, Entry.class, keyType.getType(),
Types.providerOf(valueType.getType())));
}
- private static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
- Type valueType, Key<Map<K, V>> mapKey, Key<Map<K, Provider<V>>>
providerMapKey,
+ private static <K, V> MapBinder<K, V> newMapBinder(Binder binder,
TypeLiteral<V> valueType,
+ Key<Map<K, V>> mapKey, Key<Map<K, Provider<V>>> providerMapKey,
Multibinder<Entry<K, Provider<V>>> entrySetBinder) {
- RealMapBinder<K, V> mapBinder = new RealMapBinder<K, V>(binder,
- valueType, mapKey, providerMapKey, entrySetBinder);
+ RealMapBinder<K, V> mapBinder = new RealMapBinder<K, V>(
+ binder, valueType, mapKey, providerMapKey, entrySetBinder);
binder.install(mapBinder);
return mapBinder;
}
@@ -199,7 +229,7 @@
* <p>We use a subclass to hide 'implements Module' from the public API.
*/
private static final class RealMapBinder<K, V> extends MapBinder<K, V>
implements Module {
- private final Type valueType;
+ private final TypeLiteral<V> valueType;
private final Key<Map<K, V>> mapKey;
private final Key<Map<K, Provider<V>>> providerMapKey;
private final RealMultibinder<Map.Entry<K, Provider<V>>>
entrySetBinder;
@@ -207,7 +237,7 @@
/* the target injector's binder. non-null until initialization, null
afterwards */
private Binder binder;
- private RealMapBinder(Binder binder, Type valueType,
+ private RealMapBinder(Binder binder, TypeLiteral<V> valueType,
Key<Map<K, V>> mapKey, Key<Map<K, Provider<V>>> providerMapKey,
Multibinder<Map.Entry<K, Provider<V>>> entrySetBinder) {
this.valueType = valueType;
@@ -225,8 +255,7 @@
checkNotNull(key, "key");
checkConfiguration(!isInitialized(), "MapBinder was already
initialized");
- @SuppressWarnings("unchecked")
- Key<V> valueKey = (Key<V>) Key.get(valueType, new
RealElement(entrySetBinder.getSetName()));
+ Key<V> valueKey = Key.get(valueType, new
RealElement(entrySetBinder.getSetName()));
entrySetBinder.addBinding().toInstance(new MapEntry<K,
Provider<V>>(key,
binder.getProvider(valueKey)));
return binder.bind(valueKey);
Modified:
trunk/extensions/multibindings/src/com/google/inject/multibindings/Multibinder.java
==============================================================================
---
trunk/extensions/multibindings/src/com/google/inject/multibindings/Multibinder.java
(original)
+++
trunk/extensions/multibindings/src/com/google/inject/multibindings/Multibinder.java
Sun Nov 30 11:52:02 2008
@@ -88,10 +88,10 @@
private Multibinder() {}
/**
- * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in a
- * [EMAIL PROTECTED] Set} that is itself bound with no binding annotation.
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with no binding annotation.
*/
- public static <T> Multibinder<T> newSetBinder(Binder binder, Type type) {
+ public static <T> Multibinder<T> newSetBinder(Binder binder,
TypeLiteral<T> type) {
binder = binder.skipSources(RealMultibinder.class, Multibinder.class);
RealMultibinder<T> result = new RealMultibinder<T>(binder, type, "",
Key.get(Multibinder.<T>setOf(type)));
@@ -100,10 +100,19 @@
}
/**
- * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in a
- * [EMAIL PROTECTED] Set} that is itself bound with [EMAIL PROTECTED]
annotation}.
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with no binding annotation.
*/
- public static <T> Multibinder<T> newSetBinder(Binder binder, Type type,
Annotation annotation) {
+ public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T>
type) {
+ return newSetBinder(binder, TypeLiteral.get(type));
+ }
+
+ /**
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with [EMAIL PROTECTED] annotation}.
+ */
+ public static <T> Multibinder<T> newSetBinder(
+ Binder binder, TypeLiteral<T> type, Annotation annotation) {
binder = binder.skipSources(RealMultibinder.class, Multibinder.class);
RealMultibinder<T> result = new RealMultibinder<T>(binder, type,
annotation.toString(),
Key.get(Multibinder.<T>setOf(type), annotation));
@@ -112,10 +121,19 @@
}
/**
- * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in a
- * [EMAIL PROTECTED] Set} that is itself bound with [EMAIL PROTECTED]
annotationType}.
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with [EMAIL PROTECTED] annotation}.
+ */
+ public static <T> Multibinder<T> newSetBinder(
+ Binder binder, Class<T> type, Annotation annotation) {
+ return newSetBinder(binder, TypeLiteral.get(type), annotation);
+ }
+
+ /**
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with [EMAIL PROTECTED] annotationType}.
*/
- public static <T> Multibinder<T> newSetBinder(Binder binder, Type type,
+ public static <T> Multibinder<T> newSetBinder(Binder binder,
TypeLiteral<T> type,
Class<? extends Annotation> annotationType) {
binder = binder.skipSources(RealMultibinder.class, Multibinder.class);
RealMultibinder<T> result = new RealMultibinder<T>(binder, type, "@" +
annotationType.getName(),
@@ -124,9 +142,18 @@
return result;
}
- @SuppressWarnings("unchecked")
- private static <T> TypeLiteral<Set<T>> setOf(Type elementType) {
- Type type = Types.setOf(elementType);
+ /**
+ * Returns a new multibinder that collects instances of [EMAIL PROTECTED]
type} in
a [EMAIL PROTECTED] Set} that is
+ * itself bound with [EMAIL PROTECTED] annotationType}.
+ */
+ public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T>
type,
+ Class<? extends Annotation> annotationType) {
+ return newSetBinder(binder, TypeLiteral.get(type), annotationType);
+ }
+
+ @SuppressWarnings("unchecked") // wrapping a T in a Set safely returns a
Set<T>
+ private static <T> TypeLiteral<Set<T>> setOf(TypeLiteral<T> elementType)
{
+ Type type = Types.setOf(elementType.getType());
return (TypeLiteral<Set<T>>) TypeLiteral.get(type);
}
@@ -163,10 +190,9 @@
* <p>We use a subclass to hide 'implements Module, Provider' from the
public
* API.
*/
- static final class RealMultibinder<T>
- extends Multibinder<T> implements Module, Provider<Set<T>> {
+ static final class RealMultibinder<T> extends Multibinder<T> implements
Module, Provider<Set<T>> {
- private final Type elementType;
+ private final TypeLiteral<T> elementType;
private final String setName;
private final Key<Set<T>> setKey;
@@ -176,7 +202,7 @@
/* a provider for each element in the set. null until initialization,
non-null afterwards */
private List<Provider<T>> providers;
- private RealMultibinder(Binder binder, Type elementType,
+ private RealMultibinder(Binder binder, TypeLiteral<T> elementType,
String setName, Key<Set<T>> setKey) {
this.binder = checkNotNull(binder, "binder");
this.elementType = checkNotNull(elementType, "elementType");
@@ -191,11 +217,10 @@
binder.bind(setKey).toProvider(this);
}
- @SuppressWarnings("unchecked")
@Override public LinkedBindingBuilder<T> addBinding() {
checkConfiguration(!isInitialized(), "Multibinder was already
initialized");
- return binder.bind((Key<T>) Key.get(elementType, new
RealElement(setName)));
+ return binder.bind(Key.get(elementType, new RealElement(setName)));
}
/**
@@ -217,7 +242,7 @@
}
private boolean keyMatches(Key<?> key) {
- return key.getTypeLiteral().getType().equals(elementType)
+ return key.getTypeLiteral().equals(elementType)
&& key.getAnnotation() instanceof Element
&& ((Element) key.getAnnotation()).setName().equals(setName);
}
@@ -264,10 +289,6 @@
.append(elementType)
.append(">")
.toString();
- }
-
- public Type getElementType() {
- return elementType;
}
}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---