Skip to content

Commit

Permalink
Follow-up changes to the EarlyEntryPoint API
Browse files Browse the repository at this point in the history
This CL changes the usage of @EarlyEntryPoint from being an addition to @entrypoint to being a replacement for @entrypoint.

It also adds the @beta annotation to the API.

RELNOTES=N/A
PiperOrigin-RevId: 359399064
  • Loading branch information
bcorso authored and Dagger Team committed Feb 25, 2021
1 parent 289f59f commit 1ad74b9
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 229 deletions.
3 changes: 2 additions & 1 deletion java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ android_library(
"EarlyEntryPoints.java",
],
exported_plugins = [
"//java/dagger/hilt/android/processor/internal/earlyentrypoint:processor",
"//java/dagger/hilt/processor/internal/aggregateddeps:plugin",
],
deps = [
":package_info",
"//:dagger_with_compiler",
"//java/dagger/hilt:entry_point",
"//java/dagger/hilt/internal:component_manager",
"//java/dagger/hilt/internal:preconditions",
Expand Down
13 changes: 9 additions & 4 deletions java/dagger/hilt/android/EarlyEntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import dagger.internal.Beta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand All @@ -31,25 +32,29 @@
*
* <h1>Usage:
*
* <p>To use, annotate an existing entry point with {@link EarlyEntryPoint} as shown below:
* <p>To enable an existing entry point to be called early in a Hilt test, replace its
* {@link dagger.hilt.EntryPoint} annotation with {@link EarlyEntryPoint}. (Note that,
* {@link EarlyEntryPoint} is only allowed on entry points installed in the
* {@link dagger.hilt.components.SingletonComponent}).
*
* <pre>{@code
* @EarlyEntryPoint
* @EntryPoint
* @EarlyEntryPoint // <- This replaces @EntryPoint
* @InstallIn(SingletonComponent.class)
* interface FooEntryPoint {
* Foo getFoo();
* }
* }</pre>
*
* <p>Then, replace any usages of {@link EntryPoints} with {@link EarlyEntryPoints}, as shown below:
* <p>Then, replace any of the corresponding usages of {@link dagger.hilt.EntryPoints} with
* {@link EarlyEntryPoints}, as shown below:
*
* <pre>{@code
* // EarlyEntryPoints.get() must be used with entry points annotated with @EarlyEntryPoint
* // This entry point can now be called at any point during a test, e.g. in Application.onCreate().
* Foo foo = EarlyEntryPoints.get(appContext, FooEntryPoint.class).getFoo();
* }</pre>
*/
@Beta
@Retention(RUNTIME) // Needs to be runtime for checks in EntryPoints and EarlyEntryPoints.
@Target(ElementType.TYPE)
public @interface EarlyEntryPoint {}
2 changes: 2 additions & 0 deletions java/dagger/hilt/android/EarlyEntryPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import dagger.hilt.internal.GeneratedComponentManagerHolder;
import dagger.hilt.internal.Preconditions;
import dagger.hilt.internal.TestSingletonComponentManager;
import dagger.internal.Beta;
import java.lang.annotation.Annotation;
import javax.annotation.Nonnull;

/** Static utility methods for accessing entry points annotated with {@link EarlyEntryPoint}. */
@Beta
public final class EarlyEntryPoints {

/**
Expand Down
1 change: 0 additions & 1 deletion java/dagger/hilt/android/processor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ gen_maven_artifact(
"//java/dagger/hilt/android/processor/internal/androidentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/bindvalue:bind_value_processor_lib",
"//java/dagger/hilt/android/processor/internal/customtestapplication:processor_lib",
"//java/dagger/hilt/android/processor/internal/earlyentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:validation_plugin_lib",
"//java/dagger/hilt/android/processor/internal/uninstallmodules:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:processor_lib",
Expand Down
49 changes: 0 additions & 49 deletions java/dagger/hilt/android/processor/internal/earlyentrypoint/BUILD

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions java/dagger/hilt/processor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ java_library(
"//java/dagger/hilt/android/processor/internal/androidentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/bindvalue:bind_value_processor_lib",
"//java/dagger/hilt/android/processor/internal/customtestapplication:processor_lib",
"//java/dagger/hilt/android/processor/internal/earlyentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/uninstallmodules:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:validation_plugin_lib",
Expand Down Expand Up @@ -64,7 +63,6 @@ gen_maven_artifact(
"//java/dagger/hilt/android/processor/internal/androidentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/bindvalue:bind_value_processor_lib",
"//java/dagger/hilt/android/processor/internal/customtestapplication:processor_lib",
"//java/dagger/hilt/android/processor/internal/earlyentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:validation_plugin_lib",
"//java/dagger/hilt/android/processor/internal/uninstallmodules:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:processor_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class AggregatedDepsProcessor extends BaseProcessor {
private static final ImmutableSet<ClassName> ENTRY_POINT_ANNOTATIONS =
ImmutableSet.of(
ClassNames.ENTRY_POINT,
ClassNames.EARLY_ENTRY_POINT,
ClassNames.GENERATED_ENTRY_POINT,
ClassNames.COMPONENT_ENTRY_POINT);

Expand Down Expand Up @@ -308,6 +309,28 @@ private void processEntryPoint(
element);
TypeElement entryPoint = asType(element);

if (entryPointAnnotation.equals(ClassNames.EARLY_ENTRY_POINT)) {
ImmutableSet<ClassName> components = Components.getComponents(getElementUtils(), element);
ProcessorErrors.checkState(
components.equals(ImmutableSet.of(ClassNames.SINGLETON_COMPONENT)),
element,
"@EarlyEntryPoint can only be installed into the SingletonComponent. Found: %s",
components);

Optional<TypeElement> optionalTestElement =
Processors.getOriginatingTestElement(element, getElementUtils());
ProcessorErrors.checkState(
!optionalTestElement.isPresent(),
element,
"@EarlyEntryPoint-annotated entry point, %s, cannot be nested in (or originate from) "
+ "a @HiltAndroidTest-annotated class, %s. This requirement is to avoid confusion "
+ "with other, test-specific entry points.",
asType(element).getQualifiedName().toString(),
optionalTestElement
.map(testElement -> testElement.getQualifiedName().toString())
.orElse(""));
}

generateAggregatedDeps(
entryPointAnnotation.equals(ClassNames.COMPONENT_ENTRY_POINT)
? "componentEntryPoints"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
Expand Down Expand Up @@ -70,10 +69,6 @@ void generate() throws IOException {
AnnotationSpec.get(metadata.getOptionalInstallInAnnotationMirror().get()));
}

if (Processors.hasAnnotation(metadata.getTypeElement(), ClassNames.EARLY_ENTRY_POINT)) {
entryPointInterfaceBuilder.addAnnotation(ClassNames.EARLY_ENTRY_POINT);
}

JavaFile.builder(
metadata.generatedClassName().packageName(), entryPointInterfaceBuilder.build())
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
/** A custom application used to test @EarlyEntryPoint */
public class BaseTestApplication extends Application {
@EarlyEntryPoint
@EntryPoint
@InstallIn(SingletonComponent.class)
interface EarlyFooEntryPoint {
Foo earlyFoo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dagger.hilt.android;

import dagger.hilt.EntryPoint;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import javax.inject.Inject;
Expand All @@ -26,9 +25,8 @@
public final class EarlyEntryPointCustomApplicationClasses {
private EarlyEntryPointCustomApplicationClasses() {}

// @EarlyEntryPoints cannot be nested in tests, so we have to put everything here.
// @EarlyEntryPoint cannot be nested in tests, so we've separated it out into this class.
@EarlyEntryPoint
@EntryPoint
@InstallIn(SingletonComponent.class)
interface EarlyFooEntryPoint {
Foo foo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ interface FooEntryPoint {
Foo foo();
}

// @EarlyEntryPoint cannot be nested in tests, so we've separated it out into this class.
@EarlyEntryPoint
@EntryPoint
@InstallIn(SingletonComponent.class)
interface EarlyFooEntryPoint {
Foo foo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@
public final class EarlyEntryPointHiltAndroidTestRuntimeClasses {
private EarlyEntryPointHiltAndroidTestRuntimeClasses() {}

// @EarlyEntryPoints cannot be nested in tests, so we have to put everything here.
// @EarlyEntryPoint cannot be nested in tests, so we've separated it out into this class.
@EarlyEntryPoint
@EntryPoint
@InstallIn(SingletonComponent.class)
interface EarlyFooEntryPoint {
Foo foo();
}

// @EarlyEntryPoints cannot be nested in tests, so we have to put everything here.
// @EarlyEntryPoint cannot be nested in tests, so we've separated it out into this class.
@EarlyEntryPoint
@EntryPoint
@InstallIn(SingletonComponent.class)
interface EarlyMySubcomponentBuilderEntryPoint {
MySubcomponent.Builder mySubcomponentBuilder();
Expand Down
2 changes: 0 additions & 2 deletions javatests/dagger/hilt/android/processor/AndroidCompilers.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.testing.compile.Compiler;
import dagger.hilt.android.processor.internal.androidentrypoint.AndroidEntryPointProcessor;
import dagger.hilt.android.processor.internal.customtestapplication.CustomTestApplicationProcessor;
import dagger.hilt.android.processor.internal.earlyentrypoint.EarlyEntryPointProcessor;
import dagger.hilt.android.processor.internal.uninstallmodules.UninstallModulesProcessor;
import dagger.hilt.processor.internal.aggregateddeps.AggregatedDepsProcessor;
import dagger.hilt.processor.internal.definecomponent.DefineComponentProcessor;
Expand Down Expand Up @@ -69,7 +68,6 @@ private static ImmutableList<Processor> defaultProcessors() {
new AndroidEntryPointProcessor(),
new ComponentProcessor(),
new DefineComponentProcessor(),
new EarlyEntryPointProcessor(),
new GeneratesRootInputProcessor(),
new OriginatingElementProcessor(),
new CustomTestApplicationProcessor(),
Expand Down
1 change: 0 additions & 1 deletion javatests/dagger/hilt/android/processor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ java_library(
deps = [
"//java/dagger/hilt/android/processor/internal/androidentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/customtestapplication:processor_lib",
"//java/dagger/hilt/android/processor/internal/earlyentrypoint:processor_lib",
"//java/dagger/hilt/android/processor/internal/uninstallmodules:processor_lib",
"//java/dagger/hilt/android/processor/internal/viewmodel:processor_lib",
"//java/dagger/hilt/processor/internal/aggregateddeps:processor_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ compiler_test(
],
)

compiler_test(
name = "EarlyEntryPointProcessorTest",
srcs = ["EarlyEntryPointProcessorTest.java"],
compiler_deps = [
"//java/dagger/hilt:entry_point",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android:early_test_entry_point",
"//java/dagger/hilt/android/components",
"//java/dagger/hilt/android/testing:hilt_android_test",
"@androidsdk//:platforms/android-30/android.jar",
],
deps = [
"@google_bazel_common//third_party/java/compile_testing",
"@google_bazel_common//third_party/java/junit",
"@google_bazel_common//third_party/java/truth",
"//javatests/dagger/hilt/android/processor:android_compilers",
],
)

java_library(
name = "InstallInModule",
srcs = ["InstallInModule.java"],
Expand Down
Loading

0 comments on commit 1ad74b9

Please sign in to comment.