Skip to content

Commit

Permalink
Support all annotations, since some JVMs will only cause the module p…
Browse files Browse the repository at this point in the history
…rocessor to run if that compilation job includes classes that have both @dagger.Provides and @dagger.Module.
  • Loading branch information
cgruber committed Jun 14, 2013
1 parent a8c471f commit f3b5d76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
8 changes: 4 additions & 4 deletions compiler/src/it/provide-provider-or-lazy/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import java.io.File;

File buildLog = new File(basedir, "build.log");
new BuildLogValidator().assertHasText(buildLog, new String[]{
"@Provides method must not return Provider directly: test.TestModule.provideProvider"});
"@Provides method must not return javax.inject.Provider directly: test.TestModule.provideProvider"});
new BuildLogValidator().assertHasText(buildLog, new String[]{
"@Provides method must not return Provider directly: test.TestModule.provideRawProvider"});
"@Provides method must not return javax.inject.Provider directly: test.TestModule.provideRawProvider"});
new BuildLogValidator().assertHasText(buildLog, new String[]{
"@Provides method must not return Lazy directly: test.TestModule.provideLazy"});
"@Provides method must not return dagger.Lazy directly: test.TestModule.provideLazy"});
new BuildLogValidator().assertHasText(buildLog, new String[]{
"@Provides method must not return Lazy directly: test.TestModule.provideRawLazy"});
"@Provides method must not return dagger.Lazy directly: test.TestModule.provideRawLazy"});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import dagger.internal.SetBinding;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -69,7 +70,7 @@
* Generates an implementation of {@link ModuleAdapter} that includes a binding
* for each {@code @Provides} method of a target class.
*/
@SupportedAnnotationTypes({ "dagger.Provides", "dagger.Module" })
@SupportedAnnotationTypes({ "*" })
public final class ModuleAdapterProcessor extends AbstractProcessor {
private final LinkedHashMap<String, List<ExecutableElement>> remainingTypes =
new LinkedHashMap<String, List<ExecutableElement>>();
Expand Down Expand Up @@ -118,11 +119,6 @@ private Map<String, List<ExecutableElement>> providerMethodsByClass(RoundEnviron
Elements elementUtils = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();

TypeElement providerElement = elementUtils.getTypeElement("javax.inject.Provider");
TypeMirror providerType = types.erasure(providerElement.asType());
TypeElement lazyElement = elementUtils.getTypeElement("dagger.Lazy");
TypeMirror lazyType = types.erasure(lazyElement.asType());

Map<String, List<ExecutableElement>> result = new HashMap<String, List<ExecutableElement>>();
for (Element providerMethod : providesMethods(env)) {
switch (providerMethod.getEnclosingElement().getKind()) {
Expand Down Expand Up @@ -158,20 +154,17 @@ private Map<String, List<ExecutableElement>> providerMethodsByClass(RoundEnviron
continue;
}

// Invalidate return types.
TypeMirror returnType = types.erasure(providerMethodAsExecutable.getReturnType());
if (types.isSameType(returnType, providerType)) {
error("@Provides method must not return Provider directly: "
+ type.getQualifiedName()
+ "."
+ providerMethod, providerMethod);
continue;
}
if (types.isSameType(returnType, lazyType)) {
error("@Provides method must not return Lazy directly: "
+ type.getQualifiedName()
+ "."
+ providerMethod, providerMethod);
continue;
for (String invalidTypeName : Arrays.asList("javax.inject.Provider", "dagger.Lazy")) {
TypeElement invalidTypeElement = elementUtils.getTypeElement(invalidTypeName);
if (invalidTypeElement != null) {
if (types.isSameType(returnType, types.erasure(invalidTypeElement.asType()))) {
error(String.format("@Provides method must not return %s directly: %s.%s",
invalidTypeElement, type.getQualifiedName(), providerMethod), providerMethod);
continue; // skip to next provides method.
}
}
}

List<ExecutableElement> methods = result.get(type.getQualifiedName().toString());
Expand Down

0 comments on commit f3b5d76

Please sign in to comment.