Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gissuebot/c3e1b2155bacddacb00f to your computer and use it in GitHub Desktop.
Save gissuebot/c3e1b2155bacddacb00f to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 83, comment 6
Index: com/google/inject/ProviderMethods.java
===================================================================
--- com/google/inject/ProviderMethods.java (revision 332)
+++ com/google/inject/ProviderMethods.java (working copy)
@@ -112,7 +112,29 @@
};
// TODO: Fix type warnings.
- Class type = method.getReturnType();
+ TypeLiteral literalType = null;
+
+ Type genericType = method.getGenericReturnType();
+ if (genericType != null) {
+ Type[] params = method.getDeclaringClass().getTypeParameters();
+ for (int i = 0; i < params.length; i++)
+ {
+ if ( genericType.equals( params[i] ))
+ {
+ Type superclass = providers.getClass().getGenericSuperclass();
+ Type actualType = ((java.lang.reflect.ParameterizedType) superclass).getActualTypeArguments()[i];
+ literalType = TypeLiteral.get( actualType );
+ break;
+ }
+ }
+ }
+
+ if ( null == literalType ) {
+ literalType = TypeLiteral.get(method.getReturnType());
+ }
+
+ TypeLiteral<Object> type = literalType;
+
if (scopeAnnotation == null && bindingAnnotation == null) {
bind(type).toProvider(provider);
} else if (scopeAnnotation == null) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment