Skip to content

Commit

Permalink
Merge pull request google#280 from cgruber/handlenoprovidesmodulesona…
Browse files Browse the repository at this point in the history
…lljvms

Support all annotations, since some JVMs don't consistently fire the processor.
  • Loading branch information
swankjesse committed Jun 14, 2013
2 parents b892765 + f3b5d76 commit ddaf6ac
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 @@ -68,7 +69,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 @@ -117,11 +118,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 @@ -157,20 +153,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 ddaf6ac

Please sign in to comment.