From b9325a45c4c73a5c5518d50f8ccf91a320e4661b Mon Sep 17 00:00:00 2001 From: Eric Chang Date: Tue, 19 Jan 2021 15:43:47 -0800 Subject: [PATCH] Add back the deprecated ApplicationComponent since hilt-work latest release still uses it. RELNOTES=Add back the deprecated ApplicationComponent. PiperOrigin-RevId: 352671220 --- .../components/ApplicationComponent.java | 27 +++++++++++++++++++ java/dagger/hilt/android/components/BUILD | 1 + .../hilt/processor/internal/ClassNames.java | 2 ++ .../hilt/processor/internal/Components.java | 20 +++++++++++--- .../aggregateddeps/ComponentDependencies.java | 2 +- .../DefineComponentMetadatas.java | 8 ++++++ 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 java/dagger/hilt/android/components/ApplicationComponent.java diff --git a/java/dagger/hilt/android/components/ApplicationComponent.java b/java/dagger/hilt/android/components/ApplicationComponent.java new file mode 100644 index 00000000000..6c09eb9da82 --- /dev/null +++ b/java/dagger/hilt/android/components/ApplicationComponent.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2019 The Dagger Authors. + * + * 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 dagger.hilt.android.components; + +import dagger.hilt.components.SingletonComponent; + +/** + * A Hilt component that has the lifetime of the application. + * + * @deprecated use {@link SingletonComponent} instead + */ +@Deprecated +public interface ApplicationComponent extends SingletonComponent {} diff --git a/java/dagger/hilt/android/components/BUILD b/java/dagger/hilt/android/components/BUILD index 3a307d145bd..7f5decbee00 100644 --- a/java/dagger/hilt/android/components/BUILD +++ b/java/dagger/hilt/android/components/BUILD @@ -22,6 +22,7 @@ android_library( srcs = [ "ActivityComponent.java", "ActivityRetainedComponent.java", + "ApplicationComponent.java", "FragmentComponent.java", "ServiceComponent.java", "ViewComponent.java", diff --git a/java/dagger/hilt/processor/internal/ClassNames.java b/java/dagger/hilt/processor/internal/ClassNames.java index 234ea7b4588..776eb59f490 100644 --- a/java/dagger/hilt/processor/internal/ClassNames.java +++ b/java/dagger/hilt/processor/internal/ClassNames.java @@ -167,6 +167,8 @@ public final class ClassNames { public static final ClassName ARRAYS = get("java.util", "Arrays"); // Standard components + public static final ClassName LEGACY_APPLICATION_COMPONENT = + get("dagger.hilt.android.components", "ApplicationComponent"); public static final ClassName SINGLETON_COMPONENT = get("dagger.hilt.components", "SingletonComponent"); public static final ClassName ACTIVITY_COMPONENT = diff --git a/java/dagger/hilt/processor/internal/Components.java b/java/dagger/hilt/processor/internal/Components.java index 661dd8a9bc9..9ea7e64e7c4 100644 --- a/java/dagger/hilt/processor/internal/Components.java +++ b/java/dagger/hilt/processor/internal/Components.java @@ -93,16 +93,19 @@ private static ImmutableSet getHiltInstallInComponents( || Processors.hasAnnotation(element, ClassNames.TEST_INSTALL_IN)); ImmutableSet components = - ImmutableSet.copyOf( Processors.hasAnnotation(element, ClassNames.INSTALL_IN) ? Processors.getAnnotationClassValues( elements, Processors.getAnnotationMirror(element, ClassNames.INSTALL_IN), - "value") + "value").stream() + .map(component -> mapComponents(elements, component)) + .collect(toImmutableSet()) : Processors.getAnnotationClassValues( elements, Processors.getAnnotationMirror(element, ClassNames.TEST_INSTALL_IN), - "components")); + "components").stream() + .map(component -> mapComponents(elements, component)) + .collect(toImmutableSet()); ImmutableSet undefinedComponents = components.stream() @@ -118,5 +121,16 @@ private static ImmutableSet getHiltInstallInComponents( return components.stream().map(ClassName::get).collect(toImmutableSet()); } + // Temporary hack while ApplicationComponent is renamed to SingletonComponent + private static TypeElement mapComponents(Elements elements, TypeElement element) { + if (ClassNames.LEGACY_APPLICATION_COMPONENT.equals(ClassName.get(element))) { + TypeElement singletonComponent = + elements.getTypeElement(ClassNames.SINGLETON_COMPONENT.canonicalName()); + Preconditions.checkState(singletonComponent != null); + return singletonComponent; + } + return element; + } + private Components() {} } diff --git a/java/dagger/hilt/processor/internal/aggregateddeps/ComponentDependencies.java b/java/dagger/hilt/processor/internal/aggregateddeps/ComponentDependencies.java index 23ed148c0ec..d943faf7204 100644 --- a/java/dagger/hilt/processor/internal/aggregateddeps/ComponentDependencies.java +++ b/java/dagger/hilt/processor/internal/aggregateddeps/ComponentDependencies.java @@ -243,7 +243,7 @@ private static ImmutableMap descriptorLookupMap( // processor since new processors should convert to the new SingletonComponent when generating // the metadata class. if (descriptor.component().equals(ClassNames.SINGLETON_COMPONENT)) { - builder.put("dagger.hilt.android.components.ApplicationComponent", descriptor); + builder.put(ClassNames.LEGACY_APPLICATION_COMPONENT.toString(), descriptor); } builder.put(descriptor.component().toString(), descriptor); } diff --git a/java/dagger/hilt/processor/internal/definecomponent/DefineComponentMetadatas.java b/java/dagger/hilt/processor/internal/definecomponent/DefineComponentMetadatas.java index aa69e40e119..fde80265b46 100644 --- a/java/dagger/hilt/processor/internal/definecomponent/DefineComponentMetadatas.java +++ b/java/dagger/hilt/processor/internal/definecomponent/DefineComponentMetadatas.java @@ -61,6 +61,13 @@ DefineComponentMetadata get(Element element) { } private DefineComponentMetadata get(Element element, LinkedHashSet childPath) { + // This is a temporary hack to map the old ApplicationComponent to the new SingletonComponent + if (element.getKind().equals(ElementKind.INTERFACE) + && asType(element).getQualifiedName() + .contentEquals(ClassNames.LEGACY_APPLICATION_COMPONENT.toString())) { + element = asTypeElement(asType(element).getInterfaces().get(0)); + } + if (!metadatas.containsKey(element)) { metadatas.put(element, getUncached(element, childPath)); } @@ -152,6 +159,7 @@ private DefineComponentMetadata getUncached( ProcessorErrors.checkState( ClassName.get(parent).equals(ClassNames.DEFINE_COMPONENT_NO_PARENT) + || ClassName.get(parent).equals(ClassNames.LEGACY_APPLICATION_COMPONENT) || Processors.hasAnnotation(parent, ClassNames.DEFINE_COMPONENT), component, "@DefineComponent %s, references a type not annotated with @DefineComponent: %s",