From 560eb125b437fef71e624327a024853dd9bc06ea Mon Sep 17 00:00:00 2001 From: weijiaxu Date: Wed, 18 Apr 2018 16:22:33 -0700 Subject: Add support for adding module in the same class annotated with @DialerRootComponent. Test: local test. PiperOrigin-RevId: 193435828 Change-Id: Ieb2be7c7d3e2861c0d2f32f1461535e439dbcb1a --- .../dialer/inject/demo/DemoDaggerApplication.java | 2 +- .../com/android/dialer/inject/demo/DemoModule.java | 4 +++- .../com/android/dialer/inject/demo/DemoObject.java | 16 +++++++--------- .../dialer/inject/demo/DemoSubcomponent.java | 3 ++- .../RootComponentGeneratingStep.java | 22 ++++++++++++++++++++-- 5 files changed, 33 insertions(+), 14 deletions(-) (limited to 'java') diff --git a/java/com/android/dialer/inject/demo/DemoDaggerApplication.java b/java/com/android/dialer/inject/demo/DemoDaggerApplication.java index 0c13dbb48..ab40eca1b 100644 --- a/java/com/android/dialer/inject/demo/DemoDaggerApplication.java +++ b/java/com/android/dialer/inject/demo/DemoDaggerApplication.java @@ -50,6 +50,6 @@ public final class DemoDaggerApplication extends Application implements HasRootC public void onCreate() { super.onCreate(); - DemoSubcomponent.get(this).demoObject(); + DemoSubcomponent.get(this).demoObjects(); } } diff --git a/java/com/android/dialer/inject/demo/DemoModule.java b/java/com/android/dialer/inject/demo/DemoModule.java index 40cd6fea1..9917c7dbd 100644 --- a/java/com/android/dialer/inject/demo/DemoModule.java +++ b/java/com/android/dialer/inject/demo/DemoModule.java @@ -20,6 +20,7 @@ import com.android.dialer.inject.DialerVariant; import com.android.dialer.inject.InstallIn; import dagger.Module; import dagger.Provides; +import dagger.multibindings.IntoSet; /** Module for demo dagger application. */ @Module @@ -29,7 +30,8 @@ public final class DemoModule { private DemoModule() {} @Provides + @IntoSet static DemoObject provide() { - return new DemoObject("prod"); + return DemoObject.create("prod"); } } diff --git a/java/com/android/dialer/inject/demo/DemoObject.java b/java/com/android/dialer/inject/demo/DemoObject.java index c6d48a14d..3c3862124 100644 --- a/java/com/android/dialer/inject/demo/DemoObject.java +++ b/java/com/android/dialer/inject/demo/DemoObject.java @@ -16,17 +16,15 @@ package com.android.dialer.inject.demo; -/** Object used to demonstrate dagger bindings. */ -class DemoObject { +import com.google.auto.value.AutoValue; - private final String value; +/** Object used to demonstrate dagger bindings. */ +@AutoValue +abstract class DemoObject { - DemoObject(String value) { - this.value = value; - } + abstract String value(); - @Override - public String toString() { - return value; + static DemoObject create(String value) { + return new AutoValue_DemoObject(value); } } diff --git a/java/com/android/dialer/inject/demo/DemoSubcomponent.java b/java/com/android/dialer/inject/demo/DemoSubcomponent.java index ff000aaa9..3e4dd66bb 100644 --- a/java/com/android/dialer/inject/demo/DemoSubcomponent.java +++ b/java/com/android/dialer/inject/demo/DemoSubcomponent.java @@ -20,12 +20,13 @@ import android.content.Context; import com.android.dialer.inject.HasRootComponent; import com.android.dialer.inject.IncludeInDialerRoot; import dagger.Subcomponent; +import java.util.Set; /** Subcomponent for the demo dagger application. */ @Subcomponent public abstract class DemoSubcomponent { - abstract DemoObject demoObject(); + abstract Set demoObjects(); public static DemoSubcomponent get(Context context) { return ((HasComponent) ((HasRootComponent) context.getApplicationContext()).component()) diff --git a/java/com/android/dialer/rootcomponentgenerator/RootComponentGeneratingStep.java b/java/com/android/dialer/rootcomponentgenerator/RootComponentGeneratingStep.java index 61c326a3c..4c4e6ee8d 100644 --- a/java/com/android/dialer/rootcomponentgenerator/RootComponentGeneratingStep.java +++ b/java/com/android/dialer/rootcomponentgenerator/RootComponentGeneratingStep.java @@ -19,6 +19,7 @@ package com.android.dialer.rootcomponentgenerator; import static com.google.auto.common.AnnotationMirrors.getAnnotationValue; import static com.google.auto.common.MoreElements.getAnnotationMirror; import static com.google.auto.common.MoreElements.isAnnotationPresent; +import static javax.tools.Diagnostic.Kind.ERROR; import com.android.dialer.inject.DialerRootComponent; import com.android.dialer.inject.DialerVariant; @@ -61,14 +62,22 @@ final class RootComponentGeneratingStep implements ProcessingStep { @Override public Set> annotations() { - return ImmutableSet.of(DialerRootComponent.class); + return ImmutableSet.of(DialerRootComponent.class, InstallIn.class, IncludeInDialerRoot.class); } @Override public Set process( SetMultimap, Element> elementsByAnnotation) { for (Element element : elementsByAnnotation.get(DialerRootComponent.class)) { - generateRootComponent(MoreElements.asType(element)); + // defer root components to the next round in case where the current build target contains + // elements annotated with @InstallIn. Annotation processor cannot detect metadata files + // generated in the same round and the metadata is accessible in the next round. + if (elementsByAnnotation.containsKey(InstallIn.class) + || elementsByAnnotation.containsKey(IncludeInDialerRoot.class)) { + return elementsByAnnotation.get(DialerRootComponent.class); + } else { + generateRootComponent(MoreElements.asType(element)); + } } return Collections.emptySet(); } @@ -124,6 +133,15 @@ final class RootComponentGeneratingStep implements ProcessingStep { Class annotation, MetadataProcessor metadataProcessor) { PackageElement cachePackage = processingEnv.getElementUtils().getPackageElement(RootComponentUtils.METADATA_PACKAGE_NAME); + if (cachePackage == null) { + processingEnv + .getMessager() + .printMessage( + ERROR, + "Metadata haven't been generated! do you forget to add modules " + + "or components in dependency of dialer root?"); + return; + } for (Element element : cachePackage.getEnclosedElements()) { Optional metadataAnnotation = getAnnotationMirror(element, RootComponentGeneratorMetadata.class); -- cgit v1.2.3