From e0f510a679f37e06d272ab919596149a50219975 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Mon, 19 Apr 2021 09:59:20 -0700 Subject: Make aggregated deps public and prepend "_" to the name to obfuscate it from auto suggest tools. RELNOTES=N/A PiperOrigin-RevId: 369243374 --- .../src/test/kotlin/IncrementalProcessorTest.kt | 34 +++++++--------------- .../dagger/hilt/processor/internal/Processors.java | 23 +++++++++++++++ .../aggregateddeps/AggregatedDepsGenerator.java | 18 ++---------- .../internal/aliasof/AliasOfProcessor.java | 6 ++-- .../aliasof/AliasOfPropagatedDataGenerator.java | 32 +++++++++----------- java/dagger/hilt/processor/internal/aliasof/BUILD | 1 + .../definecomponent/DefineComponentProcessor.java | 23 +++++---------- .../AggregatedEarlyEntryPointGenerator.java | 27 +++++------------ .../AggregatedUninstallModulesGenerator.java | 22 ++++---------- .../DefineComponentProcessorTest.java | 8 ++--- 10 files changed, 80 insertions(+), 114 deletions(-) diff --git a/java/dagger/hilt/android/plugin/src/test/kotlin/IncrementalProcessorTest.kt b/java/dagger/hilt/android/plugin/src/test/kotlin/IncrementalProcessorTest.kt index a003ab516..cab50aab0 100644 --- a/java/dagger/hilt/android/plugin/src/test/kotlin/IncrementalProcessorTest.kt +++ b/java/dagger/hilt/android/plugin/src/test/kotlin/IncrementalProcessorTest.kt @@ -157,28 +157,22 @@ class IncrementalProcessorTest { genActivityInjector2 = File(projectRoot, "$GEN_SRC_DIR/simple/Activity2_GeneratedInjector.java") genAppInjectorDeps = File( projectRoot, - "$GEN_SRC_DIR/hilt_aggregated_deps/simple_SimpleApp_GeneratedInjectorModuleDeps.java" + "$GEN_SRC_DIR/hilt_aggregated_deps/_simple_SimpleApp_GeneratedInjector.java" ) genActivityInjectorDeps1 = File( projectRoot, - "$GEN_SRC_DIR/hilt_aggregated_deps/simple_Activity1_GeneratedInjectorModuleDeps.java" + "$GEN_SRC_DIR/hilt_aggregated_deps/_simple_Activity1_GeneratedInjector.java" ) genActivityInjectorDeps2 = File( projectRoot, - "$GEN_SRC_DIR/hilt_aggregated_deps/simple_Activity2_GeneratedInjectorModuleDeps.java" + "$GEN_SRC_DIR/hilt_aggregated_deps/_simple_Activity2_GeneratedInjector.java" ) genModuleDeps1 = File( projectRoot, - "$GEN_SRC_DIR/hilt_aggregated_deps/simple_Module1ModuleDeps.java" - ) - genModuleDeps2 = File( - projectRoot, - "$GEN_SRC_DIR/hilt_aggregated_deps/simple_Module2ModuleDeps.java" - ) - genHiltComponents = File( - projectRoot, - "$GEN_SRC_DIR/simple/SimpleApp_HiltComponents.java" + "$GEN_SRC_DIR/hilt_aggregated_deps/_simple_Module1.java" ) + genModuleDeps2 = File(projectRoot, "$GEN_SRC_DIR/hilt_aggregated_deps/_simple_Module2.java") + genHiltComponents = File(projectRoot, "$GEN_SRC_DIR/simple/SimpleApp_HiltComponents.java") genDaggerHiltApplicationComponent = File( projectRoot, "$GEN_SRC_DIR/simple/DaggerSimpleApp_HiltComponents_SingletonC.java" @@ -203,24 +197,18 @@ class IncrementalProcessorTest { ) classGenAppInjectorDeps = File( projectRoot, - "$CLASS_DIR/hilt_aggregated_deps/simple_SimpleApp_GeneratedInjectorModuleDeps.class" + "$CLASS_DIR/hilt_aggregated_deps/_simple_SimpleApp_GeneratedInjector.class" ) classGenActivityInjectorDeps1 = File( projectRoot, - "$CLASS_DIR/hilt_aggregated_deps/simple_Activity1_GeneratedInjectorModuleDeps.class" + "$CLASS_DIR/hilt_aggregated_deps/_simple_Activity1_GeneratedInjector.class" ) classGenActivityInjectorDeps2 = File( projectRoot, - "$CLASS_DIR/hilt_aggregated_deps/simple_Activity2_GeneratedInjectorModuleDeps.class" - ) - classGenModuleDeps1 = File( - projectRoot, - "$CLASS_DIR/hilt_aggregated_deps/simple_Module1ModuleDeps.class" - ) - classGenModuleDeps2 = File( - projectRoot, - "$CLASS_DIR/hilt_aggregated_deps/simple_Module2ModuleDeps.class" + "$CLASS_DIR/hilt_aggregated_deps/_simple_Activity2_GeneratedInjector.class" ) + classGenModuleDeps1 = File(projectRoot, "$CLASS_DIR/hilt_aggregated_deps/_simple_Module1.class") + classGenModuleDeps2 = File(projectRoot, "$CLASS_DIR/hilt_aggregated_deps/_simple_Module2.class") classGenHiltComponents = File( projectRoot, "$CLASS_DIR/simple/SimpleApp_HiltComponents.class" diff --git a/java/dagger/hilt/processor/internal/Processors.java b/java/dagger/hilt/processor/internal/Processors.java index 6fc1dfa24..f33462dad 100644 --- a/java/dagger/hilt/processor/internal/Processors.java +++ b/java/dagger/hilt/processor/internal/Processors.java @@ -22,6 +22,7 @@ import static com.google.auto.common.MoreElements.asVariable; import static com.google.common.base.Preconditions.checkNotNull; import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; import static javax.lang.model.element.Modifier.ABSTRACT; +import static javax.lang.model.element.Modifier.PUBLIC; import static javax.lang.model.element.Modifier.STATIC; import com.google.auto.common.AnnotationMirrors; @@ -42,6 +43,7 @@ import com.google.common.collect.Multimap; import com.google.common.collect.SetMultimap; import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.ParameterSpec; import com.squareup.javapoet.ParameterizedTypeName; @@ -49,6 +51,7 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import dagger.internal.codegen.extension.DaggerStreams; import dagger.internal.codegen.kotlin.KotlinMetadataUtil; +import java.io.IOException; import java.lang.annotation.Annotation; import java.util.LinkedHashSet; import java.util.List; @@ -89,6 +92,26 @@ public final class Processors { private static final String JAVA_CLASS = "java.lang.Class"; + public static void generateAggregatingClass( + String aggregatingPackage, + AnnotationSpec aggregatingAnnotation, + TypeElement element, + Class generatedAnnotationClass, + ProcessingEnvironment env) throws IOException { + ClassName name = ClassName.get(aggregatingPackage, "_" + getFullEnclosedName(element)); + TypeSpec.Builder builder = + TypeSpec.classBuilder(name) + .addModifiers(PUBLIC) + .addOriginatingElement(element) + .addAnnotation(aggregatingAnnotation) + .addJavadoc("This class should only be referenced by generated code!") + .addJavadoc("This class aggregates information across multiple compilations.\n");; + + addGeneratedAnnotation(builder, env, generatedAnnotationClass); + + JavaFile.builder(name.packageName(), builder.build()).build().writeTo(env.getFiler()); + } + /** Returns a map from {@link AnnotationMirror} attribute name to {@link AnnotationValue}s */ public static ImmutableMap getAnnotationValues(Elements elements, AnnotationMirror annotation) { diff --git a/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsGenerator.java b/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsGenerator.java index bcc2dfe04..84fb5a1ee 100644 --- a/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsGenerator.java +++ b/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsGenerator.java @@ -19,8 +19,6 @@ package dagger.hilt.processor.internal.aggregateddeps; import com.google.common.collect.ImmutableSet; import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.ClassName; -import com.squareup.javapoet.JavaFile; -import com.squareup.javapoet.TypeSpec; import dagger.hilt.processor.internal.Processors; import java.io.IOException; import java.util.Optional; @@ -59,20 +57,8 @@ final class AggregatedDepsGenerator { } void generate() throws IOException { - ClassName name = - ClassName.get( - AGGREGATING_PACKAGE, Processors.getFullEnclosedName(dependency) + "ModuleDeps"); - TypeSpec.Builder generator = - TypeSpec.classBuilder(name.simpleName()) - .addOriginatingElement(dependency) - .addAnnotation(aggregatedDepsAnnotation()) - .addJavadoc("Generated class to pass information through multiple javac runs.\n"); - - Processors.addGeneratedAnnotation(generator, processingEnv, getClass()); - - JavaFile.builder(name.packageName(), generator.build()) - .build() - .writeTo(processingEnv.getFiler()); + Processors.generateAggregatingClass( + AGGREGATING_PACKAGE, aggregatedDepsAnnotation(), dependency, getClass(), processingEnv); } private AnnotationSpec aggregatedDepsAnnotation() { diff --git a/java/dagger/hilt/processor/internal/aliasof/AliasOfProcessor.java b/java/dagger/hilt/processor/internal/aliasof/AliasOfProcessor.java index 6efd6431e..02e7f5d56 100644 --- a/java/dagger/hilt/processor/internal/aliasof/AliasOfProcessor.java +++ b/java/dagger/hilt/processor/internal/aliasof/AliasOfProcessor.java @@ -16,6 +16,7 @@ package dagger.hilt.processor.internal.aliasof; +import static com.google.auto.common.MoreElements.asType; import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; import com.google.auto.service.AutoService; @@ -48,13 +49,14 @@ public final class AliasOfProcessor extends BaseProcessor { "%s should only be used on scopes." + " However, it was found annotating %s", annotation, element); + AnnotationMirror annotationMirror = Processors.getAnnotationMirror(element, ClassNames.ALIAS_OF); - Element defineComponentScope = + TypeElement defineComponentScope = Processors.getAnnotationClassValue(getElementUtils(), annotationMirror, "value"); - new AliasOfPropagatedDataGenerator(getProcessingEnv(), element, defineComponentScope) + new AliasOfPropagatedDataGenerator(getProcessingEnv(), asType(element), defineComponentScope) .generate(); } } diff --git a/java/dagger/hilt/processor/internal/aliasof/AliasOfPropagatedDataGenerator.java b/java/dagger/hilt/processor/internal/aliasof/AliasOfPropagatedDataGenerator.java index 2ccb5cfbd..1d7edf285 100644 --- a/java/dagger/hilt/processor/internal/aliasof/AliasOfPropagatedDataGenerator.java +++ b/java/dagger/hilt/processor/internal/aliasof/AliasOfPropagatedDataGenerator.java @@ -17,43 +17,37 @@ package dagger.hilt.processor.internal.aliasof; 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; -import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; /** Generates resource files for {@link dagger.hilt.migration.AliasOf}. */ final class AliasOfPropagatedDataGenerator { private final ProcessingEnvironment processingEnv; - private final Element aliasScope; - private final Element defineComponentScope; + private final TypeElement aliasScope; + private final TypeElement defineComponentScope; AliasOfPropagatedDataGenerator( - ProcessingEnvironment processingEnv, Element aliasScope, Element defineComponentScope) { + ProcessingEnvironment processingEnv, + TypeElement aliasScope, + TypeElement defineComponentScope) { this.processingEnv = processingEnv; this.aliasScope = aliasScope; this.defineComponentScope = defineComponentScope; } void generate() throws IOException { - TypeSpec.Builder generator = - TypeSpec.classBuilder(Processors.getFullEnclosedName(aliasScope)) - .addOriginatingElement(aliasScope) - .addAnnotation( - AnnotationSpec.builder(ClassNames.ALIAS_OF_PROPAGATED_DATA) + Processors.generateAggregatingClass( + ClassNames.ALIAS_OF_PROPAGATED_DATA_PACKAGE, + AnnotationSpec.builder(ClassNames.ALIAS_OF_PROPAGATED_DATA) .addMember("defineComponentScope", "$T.class", defineComponentScope) .addMember("alias", "$T.class", aliasScope) - .build()) - .addJavadoc("Generated class for aggregating scope aliases. \n"); - - Processors.addGeneratedAnnotation(generator, processingEnv, getClass()); - - JavaFile.builder(ClassNames.ALIAS_OF_PROPAGATED_DATA_PACKAGE, generator.build()) - .build() - .writeTo(processingEnv.getFiler()); + .build(), + aliasScope, + getClass(), + processingEnv); } } diff --git a/java/dagger/hilt/processor/internal/aliasof/BUILD b/java/dagger/hilt/processor/internal/aliasof/BUILD index 9a29a8ed8..3438ad54e 100644 --- a/java/dagger/hilt/processor/internal/aliasof/BUILD +++ b/java/dagger/hilt/processor/internal/aliasof/BUILD @@ -39,6 +39,7 @@ java_library( "@google_bazel_common//third_party/java/auto:service", "@google_bazel_common//third_party/java/incap", "@google_bazel_common//third_party/java/javapoet", + "@maven//:com_google_auto_auto_common", ], ) diff --git a/java/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessor.java b/java/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessor.java index fb5d0f1e2..f7e54a0a7 100644 --- a/java/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessor.java +++ b/java/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessor.java @@ -22,8 +22,6 @@ import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableSet; import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.ClassName; -import com.squareup.javapoet.JavaFile; -import com.squareup.javapoet.TypeSpec; import dagger.hilt.processor.internal.BaseProcessor; import dagger.hilt.processor.internal.ClassNames; import dagger.hilt.processor.internal.Processors; @@ -70,18 +68,13 @@ public final class DefineComponentProcessor extends BaseProcessor { } private void generateFile(String member, TypeElement typeElement) throws IOException { - TypeSpec.Builder builder = - TypeSpec.interfaceBuilder(Processors.getFullEnclosedName(typeElement)) - .addOriginatingElement(typeElement) - .addAnnotation( - AnnotationSpec.builder(ClassNames.DEFINE_COMPONENT_CLASSES) - .addMember(member, "$S", typeElement.getQualifiedName()) - .build()); - - Processors.addGeneratedAnnotation(builder, processingEnv, getClass()); - - JavaFile.builder(ClassNames.DEFINE_COMPONENT_CLASSES_PACKAGE, builder.build()) - .build() - .writeTo(processingEnv.getFiler()); + Processors.generateAggregatingClass( + ClassNames.DEFINE_COMPONENT_CLASSES_PACKAGE, + AnnotationSpec.builder(ClassNames.DEFINE_COMPONENT_CLASSES) + .addMember(member, "$S", typeElement.getQualifiedName()) + .build(), + typeElement, + getClass(), + getProcessingEnv()); } } diff --git a/java/dagger/hilt/processor/internal/earlyentrypoint/AggregatedEarlyEntryPointGenerator.java b/java/dagger/hilt/processor/internal/earlyentrypoint/AggregatedEarlyEntryPointGenerator.java index 84ed6b266..ae341189c 100644 --- a/java/dagger/hilt/processor/internal/earlyentrypoint/AggregatedEarlyEntryPointGenerator.java +++ b/java/dagger/hilt/processor/internal/earlyentrypoint/AggregatedEarlyEntryPointGenerator.java @@ -17,9 +17,6 @@ package dagger.hilt.processor.internal.earlyentrypoint; import com.squareup.javapoet.AnnotationSpec; -import com.squareup.javapoet.ClassName; -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; @@ -41,21 +38,13 @@ final class AggregatedEarlyEntryPointGenerator { } void generate() throws IOException { - ClassName name = - ClassName.get( - ClassNames.AGGREGATED_EARLY_ENTRY_POINT_PACKAGE, - Processors.getFullEnclosedName(earlyEntryPoint) + "_AggregatedEarlyEntryPoint"); - - TypeSpec.Builder builder = - TypeSpec.classBuilder(name) - .addOriginatingElement(earlyEntryPoint) - .addAnnotation( - AnnotationSpec.builder(ClassNames.AGGREGATED_EARLY_ENTRY_POINT) - .addMember("earlyEntryPoint", "$S", earlyEntryPoint.getQualifiedName()) - .build()); - - Processors.addGeneratedAnnotation(builder, env, getClass()); - - JavaFile.builder(name.packageName(), builder.build()).build().writeTo(env.getFiler()); + Processors.generateAggregatingClass( + ClassNames.AGGREGATED_EARLY_ENTRY_POINT_PACKAGE, + AnnotationSpec.builder(ClassNames.AGGREGATED_EARLY_ENTRY_POINT) + .addMember("earlyEntryPoint", "$S", earlyEntryPoint.getQualifiedName()) + .build(), + earlyEntryPoint, + getClass(), + env); } } diff --git a/java/dagger/hilt/processor/internal/uninstallmodules/AggregatedUninstallModulesGenerator.java b/java/dagger/hilt/processor/internal/uninstallmodules/AggregatedUninstallModulesGenerator.java index 9b38dad47..654a690c4 100644 --- a/java/dagger/hilt/processor/internal/uninstallmodules/AggregatedUninstallModulesGenerator.java +++ b/java/dagger/hilt/processor/internal/uninstallmodules/AggregatedUninstallModulesGenerator.java @@ -18,9 +18,6 @@ package dagger.hilt.processor.internal.uninstallmodules; import com.google.common.collect.ImmutableList; import com.squareup.javapoet.AnnotationSpec; -import com.squareup.javapoet.ClassName; -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; @@ -47,19 +44,12 @@ final class AggregatedUninstallModulesGenerator { } void generate() throws IOException { - ClassName name = - ClassName.get( - ClassNames.AGGREGATED_UNINSTALL_MODULES_PACKAGE, - Processors.getFullEnclosedName(testElement) + "_AggregatedUninstallModules"); - - TypeSpec.Builder builder = - TypeSpec.classBuilder(name) - .addOriginatingElement(testElement) - .addAnnotation(aggregatedUninstallModulesAnnotation()); - - Processors.addGeneratedAnnotation(builder, env, getClass()); - - JavaFile.builder(name.packageName(), builder.build()).build().writeTo(env.getFiler()); + Processors.generateAggregatingClass( + ClassNames.AGGREGATED_UNINSTALL_MODULES_PACKAGE, + aggregatedUninstallModulesAnnotation(), + testElement, + getClass(), + env); } private AnnotationSpec aggregatedUninstallModulesAnnotation() { diff --git a/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java b/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java index 901c7a3b0..a8c5fbe9c 100644 --- a/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java +++ b/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java @@ -64,7 +64,7 @@ public final class DefineComponentProcessorTest { JavaFileObject componentOutput = JavaFileObjects.forSourceLines( - "dagger.hilt.processor.internal.definecomponent.codegen.test_FooComponent", + "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponent", "package dagger.hilt.processor.internal.definecomponent.codegen;", "", "import dagger.hilt.internal.definecomponent.DefineComponentClasses;", @@ -72,11 +72,11 @@ public final class DefineComponentProcessorTest { "", "@DefineComponentClasses(component = \"test.FooComponent\")", "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")", - "interface test_FooComponent {}"); + "public class _test_FooComponent {}"); JavaFileObject builderOutput = JavaFileObjects.forSourceLines( - "dagger.hilt.processor.internal.definecomponent.codegen.test_FooComponentBuilder", + "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponentBuilder", "package dagger.hilt.processor.internal.definecomponent.codegen;", "", "import dagger.hilt.internal.definecomponent.DefineComponentClasses;", @@ -84,7 +84,7 @@ public final class DefineComponentProcessorTest { "", "@DefineComponentClasses(builder = \"test.FooComponentBuilder\")", "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")", - "interface test_FooComponentBuilder {}"); + "public class _test_FooComponentBuilder {}"); Compilation compilation = compiler().compile(component, builder); assertThat(compilation).succeeded(); -- cgit v1.2.3