diff --git a/compiler/src/it/provide-provider-or-lazy/verify.bsh b/compiler/src/it/provide-provider-or-lazy/verify.bsh index 08487dd31c2..7a5cfd0885d 100644 --- a/compiler/src/it/provide-provider-or-lazy/verify.bsh +++ b/compiler/src/it/provide-provider-or-lazy/verify.bsh @@ -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"}); diff --git a/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java b/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java index 519309e76d8..d149d167e94 100644 --- a/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java +++ b/compiler/src/main/java/dagger/internal/codegen/GraphVizWriter.java @@ -15,7 +15,7 @@ */ package dagger.internal.codegen; -import com.squareup.java.JavaWriter; +import com.squareup.javawriter.JavaWriter; import java.io.Closeable; import java.io.IOException; import java.io.Writer; diff --git a/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java b/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java index de890dc0be5..cb47831d58f 100644 --- a/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java +++ b/compiler/src/main/java/dagger/internal/codegen/InjectAdapterProcessor.java @@ -15,7 +15,7 @@ */ package dagger.internal.codegen; -import com.squareup.java.JavaWriter; +import com.squareup.javawriter.JavaWriter; import dagger.MembersInjector; import dagger.internal.Binding; import dagger.internal.Linker; @@ -239,7 +239,7 @@ private void writeInjectAdapter(TypeElement type, ExecutableElement constructor, boolean dependent = injectMembers || ((constructor != null) && !constructor.getParameters().isEmpty()); - writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); + writer.emitSingleLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); writer.emitPackage(packageName); writer.emitEmptyLine(); writer.emitImports(getImports(dependent, injectMembers, constructor != null)); @@ -411,7 +411,7 @@ private void writeStaticInjection(TypeElement type, List fields) throws .createSourceFile(adapterName, type); JavaWriter writer = new JavaWriter(sourceFile.openWriter()); - writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); + writer.emitSingleLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); writer.emitPackage(getPackage(type).getQualifiedName().toString()); writer.emitEmptyLine(); diff --git a/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java b/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java index 66e18c49356..1a814630f15 100644 --- a/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java +++ b/compiler/src/main/java/dagger/internal/codegen/ModuleAdapterProcessor.java @@ -15,7 +15,7 @@ */ package dagger.internal.codegen; -import com.squareup.java.JavaWriter; +import com.squareup.javawriter.JavaWriter; import dagger.Module; import dagger.Provides; import dagger.internal.Binding; @@ -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; @@ -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> remainingTypes = new LinkedHashMap>(); @@ -117,11 +118,6 @@ private Map> 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> result = new HashMap>(); for (Element providerMethod : providesMethods(env)) { switch (providerMethod.getEnclosingElement().getKind()) { @@ -157,20 +153,17 @@ private Map> 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 methods = result.get(type.getQualifiedName().toString()); @@ -238,7 +231,7 @@ private void writeModuleAdapter(TypeElement type, Map module, boolean multibindings = checkForMultibindings(providerMethods); boolean providerMethodDependencies = checkForDependencies(providerMethods); - writer.emitEndOfLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); + writer.emitSingleLineComment(AdapterJavadocs.GENERATED_BY_DAGGER); writer.emitPackage(getPackage(type).getQualifiedName().toString()); writer.emitEmptyLine(); writer.emitImports( diff --git a/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java index 8bc75bf7917..e082c0f6f6f 100644 --- a/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java +++ b/examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java @@ -30,8 +30,9 @@ public class DemoApplication extends Application { } protected List getModules() { - return Arrays.asList( - new AndroidModule(this) + return Arrays.asList( + new AndroidModule(this), + new DemoModule() ); } diff --git a/pom.xml b/pom.xml index 7338751f76c..dc7ac9e84a2 100644 --- a/pom.xml +++ b/pom.xml @@ -43,7 +43,7 @@ 1.6 1 - 1.0.5 + 2.0.1 4.10