Skip to content

Commit

Permalink
Add back the deprecated ApplicationComponent since hilt-work latest r…
Browse files Browse the repository at this point in the history
…elease still uses it.

RELNOTES=Add back the deprecated ApplicationComponent.
PiperOrigin-RevId: 352671220
  • Loading branch information
Chang-Eric authored and Dagger Team committed Jan 19, 2021
1 parent 94a55bc commit b9325a4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
27 changes: 27 additions & 0 deletions java/dagger/hilt/android/components/ApplicationComponent.java
Original file line number Diff line number Diff line change
@@ -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 {}
1 change: 1 addition & 0 deletions java/dagger/hilt/android/components/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android_library(
srcs = [
"ActivityComponent.java",
"ActivityRetainedComponent.java",
"ApplicationComponent.java",
"FragmentComponent.java",
"ServiceComponent.java",
"ViewComponent.java",
Expand Down
2 changes: 2 additions & 0 deletions java/dagger/hilt/processor/internal/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
20 changes: 17 additions & 3 deletions java/dagger/hilt/processor/internal/Components.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ private static ImmutableSet<ClassName> getHiltInstallInComponents(
|| Processors.hasAnnotation(element, ClassNames.TEST_INSTALL_IN));

ImmutableSet<TypeElement> 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<TypeElement> undefinedComponents =
components.stream()
Expand All @@ -118,5 +121,16 @@ private static ImmutableSet<ClassName> 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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static ImmutableMap<String, ComponentDescriptor> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ DefineComponentMetadata get(Element element) {
}

private DefineComponentMetadata get(Element element, LinkedHashSet<Element> 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));
}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit b9325a4

Please sign in to comment.