Skip to content

Commit

Permalink
Add an onPluginEnd method to BindingGraphPlugin.
Browse files Browse the repository at this point in the history
Fixes #3483.

RELNOTES=Add onPluginEnd to BindingGraphPlugin. Fixes #3483
PiperOrigin-RevId: 463211283
  • Loading branch information
Chang-Eric authored and Dagger Team committed Jul 26, 2022
1 parent c78df18 commit 467dce1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions java/dagger/internal/codegen/DelegateComponentProcessor.java
Expand Up @@ -80,6 +80,9 @@ public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {
} catch (SourceFileGenerationException e) {
e.printMessageTo(env.getMessager());
}
} else {
validationBindingGraphPlugins.endPlugins();
externalBindingGraphPlugins.endPlugins();
}
clearableCaches.forEach(ClearableCache::clearCache);
}
Expand Down
Expand Up @@ -80,6 +80,11 @@ public void init(DaggerProcessingEnv processingEnv, Map<String, String> options)
plugins.forEach(plugin -> plugin.init(processingEnv, options));
}

@Override
public void onPluginEnd() {
plugins.forEach(BindingGraphPlugin::onPluginEnd);
}

@Override
public Set<String> supportedOptions() {
return plugins.stream()
Expand Down
Expand Up @@ -99,4 +99,8 @@ boolean visit(dagger.spi.model.BindingGraph spiGraph) {
}
return isClean;
}

public void endPlugins() {
plugins.forEach(BindingGraphPlugin::onPluginEnd);
}
}
Expand Up @@ -95,4 +95,8 @@ boolean visit(BindingGraph graph) {
}
return isClean;
}

public void endPlugins() {
plugins.forEach(BindingGraphPlugin::onPluginEnd);
}
}
7 changes: 7 additions & 0 deletions java/dagger/spi/BindingGraphPlugin.java
Expand Up @@ -91,4 +91,11 @@ default Set<String> supportedOptions() {
default String pluginName() {
return getClass().getCanonicalName();
}

/**
* Perform any extra work after the plugin finished all its visiting. This will be called once per
* instance of this plugin, after all graphs were {@linkplain #visitGraph(BindingGraph,
* DiagnosticReporter) visited}
*/
default void onPluginEnd() {}
}
7 changes: 7 additions & 0 deletions java/dagger/spi/model/BindingGraphPlugin.java
Expand Up @@ -58,4 +58,11 @@ default Set<String> supportedOptions() {
default String pluginName() {
return getClass().getCanonicalName();
}

/**
* Perform any extra work after the plugin finished all its visiting. This will be called once per
* instance of this plugin, after all graphs were {@linkplain #visitGraph(BindingGraph,
* DiagnosticReporter) visited}
*/
default void onPluginEnd() {}
}
19 changes: 19 additions & 0 deletions javatests/dagger/spi/FailingPlugin.java
Expand Up @@ -21,12 +21,17 @@
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import dagger.model.BindingGraph;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.tools.StandardLocation;

@AutoService(BindingGraphPlugin.class)
public final class FailingPlugin implements BindingGraphPlugin {
private Map<String, String> options;
private Filer filer;

@Override
public Set<String> supportedOptions() {
Expand All @@ -37,6 +42,11 @@ public Set<String> supportedOptions() {
"error_on_subcomponents");
}

@Override
public void initFiler(Filer filer) {
this.filer = filer;
}

@Override
public void initOptions(Map<String, String> options) {
this.options = options;
Expand Down Expand Up @@ -89,4 +99,13 @@ public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticR
public String pluginName() {
return "FailingPlugin";
}

@Override
public void onPluginEnd() {
try {
filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "onPluginEndTest.txt");
} catch (IOException e) {
throw new UncheckedIOException("Failed to create txt file", e);
}
}
}
17 changes: 17 additions & 0 deletions javatests/dagger/spi/SpiPluginTest.java
Expand Up @@ -25,6 +25,7 @@
import com.google.testing.compile.JavaFileObjects;
import dagger.internal.codegen.ComponentProcessor;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -483,6 +484,22 @@ public void shortestPathToBindingExistsThroughSubcomponentBuilder() {
.onLineContaining("interface TestComponent");
}

@Test
public void onPluginEnd() {
JavaFileObject component =
JavaFileObjects.forSourceLines(
"test.TestComponent",
"package test;",
"",
"import dagger.Component;",
"",
"@Component",
"interface TestComponent {}");
Compilation compilation = javac().withProcessors(new ComponentProcessor()).compile(component);
assertThat(compilation)
.generatedFile(StandardLocation.SOURCE_OUTPUT, "", "onPluginEndTest.txt");
}

// This works around an issue in the opensource compile testing where only one diagnostic is
// recorded per line. When multiple validation items resolve to the same entry point, we can
// only see the first. This helper class makes it easier to compile all of the files in the test
Expand Down

0 comments on commit 467dce1

Please sign in to comment.