From db5310f248cb71197b6420acc883850ce8593793 Mon Sep 17 00:00:00 2001 From: weijiaxu Date: Thu, 5 Apr 2018 16:55:11 -0700 Subject: Change @DialerComponent to @IncludeInDialerRoot and turn on @IncludeInDialerRoot in root component generator. Test: local test. PiperOrigin-RevId: 191820677 Change-Id: I9df15cccf65e8c51c8fadf57eabec4fd41a51007 --- .../annotation/DialerComponent.java | 45 --------------------- .../annotation/IncludeInDialerRoot.java | 47 ++++++++++++++++++++++ .../processor/ComponentGeneratingStep.java | 11 ++--- .../processor/MetadataGeneratingStep.java | 20 +++++---- .../processor/RootComponentGeneratingStep.java | 7 ++-- 5 files changed, 68 insertions(+), 62 deletions(-) delete mode 100644 java/com/android/dialer/rootcomponentgenerator/annotation/DialerComponent.java create mode 100644 java/com/android/dialer/rootcomponentgenerator/annotation/IncludeInDialerRoot.java diff --git a/java/com/android/dialer/rootcomponentgenerator/annotation/DialerComponent.java b/java/com/android/dialer/rootcomponentgenerator/annotation/DialerComponent.java deleted file mode 100644 index 573abae7f..000000000 --- a/java/com/android/dialer/rootcomponentgenerator/annotation/DialerComponent.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.dialer.rootcomponentgenerator.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Target; - -/** - * Annotates a type equivalent to {@link dagger.Subcomponent}. - * - *

The annotation processor will generate a new type file with some prefix, which contains public - * static XXX get(Context context) method and HasComponent interface like: - * - *

- * - *

- * 
- *  public static SimulatorComponent get(Context context) {
- *      HasRootComponent hasRootComponent = (HasRootComponent) context.getApplicationContext();
- *      return ((HasComponent)(hasRootComponent.component()).simulatorComponent();
- *  }
- *  public interface HasComponent {
- *      SimulatorComponent simulatorComponent();
- *  }
- * 
- * 
- */ -@Target(ElementType.TYPE) -public @interface DialerComponent { - Class[] modules() default {}; -} diff --git a/java/com/android/dialer/rootcomponentgenerator/annotation/IncludeInDialerRoot.java b/java/com/android/dialer/rootcomponentgenerator/annotation/IncludeInDialerRoot.java new file mode 100644 index 000000000..4ce9ec3a3 --- /dev/null +++ b/java/com/android/dialer/rootcomponentgenerator/annotation/IncludeInDialerRoot.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.rootcomponentgenerator.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Annotates a type that should be included in Dialer Root Component. Typically, annotated types are + * HasComponent interfaces. + * + *

An example: + * + *

+ * 
+ * {@literal @}dagger.Subcomponent
+ * public abstract class SimulatorComponent {
+ *   public static SimulatorComponent get(Context context) {
+ *      return ((HasComponent)((HasRootComponent) context.getApplicationContext()).component())
+ *         .simulatorComponent();
+ *   }
+ *   {@literal @}IncludeInDialerRoot
+ *   public interface HasComponent {
+ *      SimulatorComponent simulatorComponent();
+ *  }
+ * }
+ * 
+ * 
+ */ +@Target(ElementType.TYPE) +public @interface IncludeInDialerRoot { + Class[] modules() default {}; +} diff --git a/java/com/android/dialer/rootcomponentgenerator/processor/ComponentGeneratingStep.java b/java/com/android/dialer/rootcomponentgenerator/processor/ComponentGeneratingStep.java index 8605499c7..04d42ac59 100644 --- a/java/com/android/dialer/rootcomponentgenerator/processor/ComponentGeneratingStep.java +++ b/java/com/android/dialer/rootcomponentgenerator/processor/ComponentGeneratingStep.java @@ -23,7 +23,7 @@ import static javax.lang.model.element.Modifier.PUBLIC; import static javax.lang.model.element.Modifier.STATIC; import static javax.lang.model.util.ElementFilter.typesIn; -import com.android.dialer.rootcomponentgenerator.annotation.DialerComponent; +import com.android.dialer.rootcomponentgenerator.annotation.IncludeInDialerRoot; import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep; import com.google.auto.common.MoreElements; import com.google.common.base.Optional; @@ -52,7 +52,7 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; /** - * Generates component for a type annotated with {@link DialerComponent}. + * Generates component for a type annotated with {@link IncludeInDialerRoot}. * *

Our components have boilerplate code like: * @@ -67,6 +67,7 @@ import javax.lang.model.type.TypeMirror; * return ((HasComponent)((HasRootComponent) context.getApplicationContext()).component()) * .simulatorComponent(); * } + * {@literal @}IncludeInDialerRoot * public interface HasComponent { * SimulatorComponent simulatorComponent(); * } @@ -88,13 +89,13 @@ final class ComponentGeneratingStep implements ProcessingStep { @Override public Set> annotations() { - return ImmutableSet.of(DialerComponent.class); + return ImmutableSet.of(IncludeInDialerRoot.class); } @Override public Set process( SetMultimap, Element> elementsByAnnotation) { - for (TypeElement type : typesIn(elementsByAnnotation.get(DialerComponent.class))) { + for (TypeElement type : typesIn(elementsByAnnotation.get(IncludeInDialerRoot.class))) { generateComponent(type); } return Collections.emptySet(); @@ -124,7 +125,7 @@ final class ComponentGeneratingStep implements ProcessingStep { private AnnotationSpec makeDaggerSubcomponentAnnotation(TypeElement dialerComponentElement) { Optional componentMirror = - getAnnotationMirror(dialerComponentElement, DialerComponent.class); + getAnnotationMirror(dialerComponentElement, IncludeInDialerRoot.class); AnnotationSpec.Builder subcomponentBuilder = AnnotationSpec.builder(Subcomponent.class); for (AnnotationValue annotationValue : diff --git a/java/com/android/dialer/rootcomponentgenerator/processor/MetadataGeneratingStep.java b/java/com/android/dialer/rootcomponentgenerator/processor/MetadataGeneratingStep.java index b7d31c0e9..3b46ed0d0 100644 --- a/java/com/android/dialer/rootcomponentgenerator/processor/MetadataGeneratingStep.java +++ b/java/com/android/dialer/rootcomponentgenerator/processor/MetadataGeneratingStep.java @@ -18,22 +18,25 @@ package com.android.dialer.rootcomponentgenerator.processor; import static javax.tools.Diagnostic.Kind.ERROR; +import com.android.dialer.rootcomponentgenerator.annotation.IncludeInDialerRoot; import com.android.dialer.rootcomponentgenerator.annotation.InstallIn; import com.android.dialer.rootcomponentgenerator.annotation.RootComponentGeneratorMetadata; import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep; +import com.google.auto.common.MoreElements; import com.google.common.collect.ImmutableSet; import com.google.common.collect.SetMultimap; import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.TypeSpec; -import dagger.Subcomponent; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.Set; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; /** - * Genereates metadata for every type annotated by {@link InstallIn} and {@link Subcomponent}. + * Genereates metadata for every type annotated by {@link InstallIn} and {@link + * IncludeInDialerRoot}. * *

The metadata has the information where the annotated types are and it is used by annotation * processor when the processor tries to generate root component. @@ -48,15 +51,15 @@ final class MetadataGeneratingStep implements ProcessingStep { @Override public Set> annotations() { - return ImmutableSet.of(Subcomponent.class, InstallIn.class); + return ImmutableSet.of(IncludeInDialerRoot.class, InstallIn.class); } @Override public Set process( SetMultimap, Element> elementsByAnnotation) { - for (Element element : elementsByAnnotation.get(Subcomponent.class)) { - generateMetadataFor(Subcomponent.class, element); + for (Element element : elementsByAnnotation.get(IncludeInDialerRoot.class)) { + generateMetadataFor(IncludeInDialerRoot.class, MoreElements.asType(element)); } for (Element element : elementsByAnnotation.get(InstallIn.class)) { if (element.getAnnotation(InstallIn.class).variants().length == 0) { @@ -66,16 +69,17 @@ final class MetadataGeneratingStep implements ProcessingStep { ERROR, String.format("@InstallIn %s must have at least one variant", element)); continue; } - generateMetadataFor(InstallIn.class, element); + generateMetadataFor(InstallIn.class, MoreElements.asType(element)); } return Collections.emptySet(); } private void generateMetadataFor( - Class annotation, Element annotatedElement) { + Class annotation, TypeElement annotatedElement) { TypeSpec.Builder metadataClassBuilder = - TypeSpec.classBuilder(annotatedElement.getSimpleName() + "Metadata"); + TypeSpec.classBuilder( + annotatedElement.getQualifiedName().toString().replace('.', '_') + "Metadata"); metadataClassBuilder.addAnnotation( AnnotationSpec.builder(RootComponentGeneratorMetadata.class) .addMember("tag", "$S", annotation.getSimpleName()) diff --git a/java/com/android/dialer/rootcomponentgenerator/processor/RootComponentGeneratingStep.java b/java/com/android/dialer/rootcomponentgenerator/processor/RootComponentGeneratingStep.java index 9b97adafd..ad91a3cbe 100644 --- a/java/com/android/dialer/rootcomponentgenerator/processor/RootComponentGeneratingStep.java +++ b/java/com/android/dialer/rootcomponentgenerator/processor/RootComponentGeneratingStep.java @@ -22,6 +22,7 @@ import static com.google.auto.common.MoreElements.isAnnotationPresent; import com.android.dialer.rootcomponentgenerator.annotation.DialerRootComponent; import com.android.dialer.rootcomponentgenerator.annotation.DialerVariant; +import com.android.dialer.rootcomponentgenerator.annotation.IncludeInDialerRoot; import com.android.dialer.rootcomponentgenerator.annotation.InstallIn; import com.android.dialer.rootcomponentgenerator.annotation.RootComponentGeneratorMetadata; import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep; @@ -35,7 +36,6 @@ import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.TypeSpec; import dagger.Component; -import dagger.Subcomponent; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; @@ -89,8 +89,7 @@ final class RootComponentGeneratingStep implements ProcessingStep { .addModifiers(Modifier.PUBLIC) .addAnnotation(Singleton.class); for (TypeElement componentWithSuperInterface : componentList) { - rootComponentClassBuilder.addSuperinterface( - ClassName.get(componentWithSuperInterface).nestedClass("HasComponent")); + rootComponentClassBuilder.addSuperinterface(ClassName.get(componentWithSuperInterface)); } AnnotationSpec.Builder componentAnnotation = AnnotationSpec.builder(Component.class); for (TypeElement annotatedElement : componentModuleMap.get(dialerVariant)) { @@ -104,7 +103,7 @@ final class RootComponentGeneratingStep implements ProcessingStep { private List generateComponentList() { List list = new ArrayList<>(); - extractInfoFromMetadata(Subcomponent.class, list::add); + extractInfoFromMetadata(IncludeInDialerRoot.class, list::add); return list; } -- cgit v1.2.3