Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding module generic support. (WIP) #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions integration-tests/upstream/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ test.filter {
excludeTest 'dagger.functional.BasicTest', null

// TODO reflect bug! Generics don't work well.
excludeTest 'dagger.functional.GenericTest', 'complexGenerics'
excludeTest 'dagger.functional.GenericTest', 'genericModules'
excludeTest 'dagger.functional.GenericTest', 'membersInjections'
excludeTest 'dagger.functional.GenericTest', 'noDepsGenerics'
excludeTest 'dagger.functional.GenericTest', 'packagePrivateTypeParameterDependencies'
excludeTest 'dagger.functional.GenericTest', 'publicSubclassWithPackagePrivateTypeParameterOfSuperclass'
excludeTest 'dagger.functional.GenericTest', 'testGenericComponentCreate'
excludeTest 'dagger.functional.GenericTest', 'testGenericDoubleReferences'
excludeTest 'dagger.functional.GenericTest', 'testGenericSimpleReferences'
// excludeTest 'dagger.functional.GenericTest', 'complexGenerics'
// excludeTest 'dagger.functional.GenericTest', 'genericModules'
// excludeTest 'dagger.functional.GenericTest', 'membersInjections'
// excludeTest 'dagger.functional.GenericTest', 'noDepsGenerics'
// excludeTest 'dagger.functional.GenericTest', 'packagePrivateTypeParameterDependencies'
// excludeTest 'dagger.functional.GenericTest', 'publicSubclassWithPackagePrivateTypeParameterOfSuperclass'
// excludeTest 'dagger.functional.GenericTest', 'testGenericComponentCreate'
// excludeTest 'dagger.functional.GenericTest', 'testGenericDoubleReferences'
// excludeTest 'dagger.functional.GenericTest', 'testGenericSimpleReferences'
excludeTest 'dagger.functional.binds.BindsTest', 'bindDelegates'
excludeTest 'dagger.functional.binds.BindsTest', 'bindWithScope'

Expand Down
50 changes: 48 additions & 2 deletions reflect/src/main/java/dagger/reflect/ReflectiveModuleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.jetbrains.annotations.Nullable;

final class ReflectiveModuleParser {
static void parse(Class<?> moduleClass, @Nullable Object instance, Scope.Builder scopeBuilder) {
for (Class<?> target : Reflection.getDistinctTypeHierarchy(moduleClass)) {
Set<Class<?>> hierarchy = Reflection.getDistinctTypeHierarchy(moduleClass);
for (Class<?> target : hierarchy) {
for (Method method : target.getDeclaredMethods()) {
Type returnType = method.getGenericReturnType();

Annotation[] annotations = method.getAnnotations();
Annotation qualifier = findQualifier(annotations);

Expand Down Expand Up @@ -85,7 +90,8 @@ static void parse(Class<?> moduleClass, @Nullable Object instance, Scope.Builder

if (method.getAnnotation(Provides.class) != null) {
ensureNotPrivate(method);
Key key = Key.of(qualifier, returnType);

Key key = Key.of(qualifier, resolveType(hierarchy, target, returnType));
Binding binding = new UnlinkedProvidesBinding(instance, method);
addBinding(scopeBuilder, key, binding, annotations);
}
Expand All @@ -94,6 +100,46 @@ static void parse(Class<?> moduleClass, @Nullable Object instance, Scope.Builder
}
}

private static Type resolveType(Set<Class<?>> moduleClass, Class<?> target, Type type) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;

Type[] typeArguments = new Type[parameterizedType.getActualTypeArguments().length];

int index = 0;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (typeArgument instanceof TypeVariable) {
for (Class<?> possibility : moduleClass) {
if (target != possibility && target.isAssignableFrom(possibility)) {
ParameterizedType genericSuperclass = ((ParameterizedType) possibility.getGenericSuperclass());
if (genericSuperclass.getRawType() == target) {
int typeIndex = 0;
TypeVariable<? extends Class<?>>[] typeParameters = target.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
if (typeArgument == typeParameters[i]) {
typeIndex = i;
}
}

typeArguments[index] = genericSuperclass.getActualTypeArguments()[typeIndex];

}
}
}
} else {
typeArguments[index] = typeArgument;
}


index += 1;
}

return new ParameterizedTypeImpl(parameterizedType.getOwnerType(), parameterizedType.getRawType(), typeArguments);
}

return type;
}

private static void addBinding(
Scope.Builder scopeBuilder, Key key, Binding binding, Annotation[] annotations) {
Annotation scope = findScope(annotations);
Expand Down