From 0dfbcaaac4187fab7808274567c14d5031b51ed0 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Wed, 27 Apr 2022 15:26:26 -0700 Subject: [PATCH] [Refactor] Move CompositeBindingGraphValidator into bindinggraphvalidation package. This class is a BindingGraphPlugin, so it should go into the bindinggraphvalidation package with the rest of our BindingGraphPlugins. Additionally, this allows us to make this class package-private. This CL also converts a hand-written factory to use Dagger's new AssistedFactory. RELNOTES=N/A PiperOrigin-RevId: 444983117 --- .../codegen/bindinggraphvalidation/BUILD | 1 + .../BindingGraphValidationModule.java | 3 +- .../CompositeBindingGraphPlugin.java | 37 +++++++------------ .../PackageNameCompressor.java | 2 +- .../PackageNameCompressorTest.java | 6 +-- .../dagger/internal/codegen/validation/BUILD | 33 ----------------- 6 files changed, 20 insertions(+), 62 deletions(-) rename java/dagger/internal/codegen/{validation => bindinggraphvalidation}/CompositeBindingGraphPlugin.java (91%) rename java/dagger/internal/codegen/{validation => bindinggraphvalidation}/PackageNameCompressor.java (99%) rename javatests/dagger/internal/codegen/{validation => bindinggraphvalidation}/PackageNameCompressorTest.java (95%) delete mode 100644 javatests/dagger/internal/codegen/validation/BUILD diff --git a/java/dagger/internal/codegen/bindinggraphvalidation/BUILD b/java/dagger/internal/codegen/bindinggraphvalidation/BUILD index cb7db877c56..85c64515696 100644 --- a/java/dagger/internal/codegen/bindinggraphvalidation/BUILD +++ b/java/dagger/internal/codegen/bindinggraphvalidation/BUILD @@ -38,6 +38,7 @@ java_library( "//java/dagger/spi", "//third_party/java/auto:common", "//third_party/java/auto:value", + "//third_party/java/error_prone:annotations", "//third_party/java/guava/base", "//third_party/java/guava/collect", "//third_party/java/guava/graph", diff --git a/java/dagger/internal/codegen/bindinggraphvalidation/BindingGraphValidationModule.java b/java/dagger/internal/codegen/bindinggraphvalidation/BindingGraphValidationModule.java index f689427acd0..2df35e8a7c5 100644 --- a/java/dagger/internal/codegen/bindinggraphvalidation/BindingGraphValidationModule.java +++ b/java/dagger/internal/codegen/bindinggraphvalidation/BindingGraphValidationModule.java @@ -20,7 +20,6 @@ import dagger.Module; import dagger.Provides; import dagger.internal.codegen.compileroption.CompilerOptions; -import dagger.internal.codegen.validation.CompositeBindingGraphPlugin; import dagger.internal.codegen.validation.Validation; import dagger.spi.model.BindingGraphPlugin; @@ -57,7 +56,7 @@ static ImmutableSet providePlugins( validation10, validation11); if (compilerOptions.experimentalDaggerErrorMessages()) { - return ImmutableSet.of(factory.create(plugins, "Dagger/Validation")); + return ImmutableSet.of(factory.create(plugins)); } else { return plugins; } diff --git a/java/dagger/internal/codegen/validation/CompositeBindingGraphPlugin.java b/java/dagger/internal/codegen/bindinggraphvalidation/CompositeBindingGraphPlugin.java similarity index 91% rename from java/dagger/internal/codegen/validation/CompositeBindingGraphPlugin.java rename to java/dagger/internal/codegen/bindinggraphvalidation/CompositeBindingGraphPlugin.java index d1b84cf97ac..d7b1b82840d 100644 --- a/java/dagger/internal/codegen/validation/CompositeBindingGraphPlugin.java +++ b/java/dagger/internal/codegen/bindinggraphvalidation/CompositeBindingGraphPlugin.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package dagger.internal.codegen.validation; +package dagger.internal.codegen.bindinggraphvalidation; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -26,6 +26,10 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.FormatMethod; +import dagger.assisted.Assisted; +import dagger.assisted.AssistedFactory; +import dagger.assisted.AssistedInject; +import dagger.internal.codegen.validation.DiagnosticMessageGenerator; import dagger.spi.model.BindingGraph; import dagger.spi.model.BindingGraph.ChildFactoryMethodEdge; import dagger.spi.model.BindingGraph.ComponentNode; @@ -37,7 +41,6 @@ import java.util.Optional; import java.util.Set; import javax.annotation.processing.Filer; -import javax.inject.Inject; import javax.lang.model.util.Elements; // ALLOW_TYPES_ELEMENTS because of interface dependencies import javax.lang.model.util.Types; // ALLOW_TYPES_ELEMENTS because of interface dependencies import javax.tools.Diagnostic; @@ -46,32 +49,20 @@ * Combines many {@link BindingGraphPlugin} implementations. This helps reduce spam by combining * all of the messages that are reported on the root component. */ -public final class CompositeBindingGraphPlugin implements BindingGraphPlugin { +final class CompositeBindingGraphPlugin implements BindingGraphPlugin { + @AssistedFactory + interface Factory { + CompositeBindingGraphPlugin create(ImmutableSet plugins); + } private final ImmutableSet plugins; - private final String pluginName; private final DiagnosticMessageGenerator.Factory messageGeneratorFactory; - /** Factory class for {@link CompositeBindingGraphPlugin}. */ - public static final class Factory { - private final DiagnosticMessageGenerator.Factory messageGeneratorFactory; - - @Inject Factory(DiagnosticMessageGenerator.Factory messageGeneratorFactory) { - this.messageGeneratorFactory = messageGeneratorFactory; - } - - public CompositeBindingGraphPlugin create( - ImmutableSet plugins, String pluginName) { - return new CompositeBindingGraphPlugin(plugins, pluginName, messageGeneratorFactory); - } - } - - private CompositeBindingGraphPlugin( - ImmutableSet plugins, - String pluginName, + @AssistedInject + CompositeBindingGraphPlugin( + @Assisted ImmutableSet plugins, DiagnosticMessageGenerator.Factory messageGeneratorFactory) { this.plugins = plugins; - this.pluginName = pluginName; this.messageGeneratorFactory = messageGeneratorFactory; } @@ -114,7 +105,7 @@ public Set supportedOptions() { @Override public String pluginName() { - return pluginName; + return "Dagger/Validation"; } // TODO(erichang): This kind of breaks some of the encapsulation by relying on or repeating diff --git a/java/dagger/internal/codegen/validation/PackageNameCompressor.java b/java/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressor.java similarity index 99% rename from java/dagger/internal/codegen/validation/PackageNameCompressor.java rename to java/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressor.java index 6b7c720b2b3..9759faaaf89 100644 --- a/java/dagger/internal/codegen/validation/PackageNameCompressor.java +++ b/java/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package dagger.internal.codegen.validation; +package dagger.internal.codegen.bindinggraphvalidation; import static java.util.Comparator.comparing; diff --git a/javatests/dagger/internal/codegen/validation/PackageNameCompressorTest.java b/javatests/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressorTest.java similarity index 95% rename from javatests/dagger/internal/codegen/validation/PackageNameCompressorTest.java rename to javatests/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressorTest.java index f6a1b79aae8..eeacb1aabe4 100644 --- a/javatests/dagger/internal/codegen/validation/PackageNameCompressorTest.java +++ b/javatests/dagger/internal/codegen/bindinggraphvalidation/PackageNameCompressorTest.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package dagger.internal.codegen.validation; +package dagger.internal.codegen.bindinggraphvalidation; import static com.google.common.truth.Truth.assertThat; -import static dagger.internal.codegen.validation.PackageNameCompressor.LEGEND_FOOTER; -import static dagger.internal.codegen.validation.PackageNameCompressor.LEGEND_HEADER; +import static dagger.internal.codegen.bindinggraphvalidation.PackageNameCompressor.LEGEND_FOOTER; +import static dagger.internal.codegen.bindinggraphvalidation.PackageNameCompressor.LEGEND_HEADER; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/javatests/dagger/internal/codegen/validation/BUILD b/javatests/dagger/internal/codegen/validation/BUILD deleted file mode 100644 index 6271768cf4e..00000000000 --- a/javatests/dagger/internal/codegen/validation/BUILD +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (C) 2020 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. - -# Description: -# Tests for dagger.internal.codegen.validation - -load("//:build_defs.bzl", "DOCLINT_HTML_AND_SYNTAX") -load("//:test_defs.bzl", "GenJavaTests") - -package(default_visibility = ["//:src"]) - -GenJavaTests( - name = "validation_tests", - srcs = glob(["*.java"]), - functional = False, - javacopts = DOCLINT_HTML_AND_SYNTAX, - deps = [ - "//java/dagger/internal/codegen/validation", - "//third_party/java/junit", - "//third_party/java/truth", - ], -)