Skip to content

Commit 2a6a0b4

Browse files
wanyingd1996Dagger Team
authored andcommitted
Reset processingEnv for BindingGraphPlugins for each round.
Previously, plugin initialization was performed in processingSteps(), which is only called once at the time when the Processor is created. RELNOTES=Added `BindingGraphPlugin#onProcessingRoundBegin` for pre processing initialization. PiperOrigin-RevId: 618910291
1 parent fc2363d commit 2a6a0b4

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

java/dagger/hilt/android/processor/internal/viewmodel/ViewModelValidationPlugin.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ import javax.tools.Diagnostic.Kind
4848
class ViewModelValidationPlugin : BindingGraphPlugin {
4949

5050
private lateinit var env: XProcessingEnv
51+
private lateinit var daggerProcessingEnv: DaggerProcessingEnv
5152

5253
override fun init(processingEnv: DaggerProcessingEnv, options: MutableMap<String, String>) {
53-
env = processingEnv.toXProcessingEnv()
54+
daggerProcessingEnv = processingEnv
55+
}
56+
57+
override fun onProcessingRoundBegin() {
58+
env = daggerProcessingEnv.toXProcessingEnv()
5459
}
5560

5661
override fun visitGraph(bindingGraph: BindingGraph, diagnosticReporter: DiagnosticReporter) {
@@ -75,7 +80,7 @@ class ViewModelValidationPlugin : BindingGraphPlugin {
7580
"\nInjection of an @HiltViewModel class is prohibited since it does not create a " +
7681
"ViewModel instance correctly.\nAccess the ViewModel via the Android APIs " +
7782
"(e.g. ViewModelProvider) instead." +
78-
"\nInjected ViewModel: ${target.key().type()}\n"
83+
"\nInjected ViewModel: ${target.key().type()}\n",
7984
)
8085
} else if (
8186
isViewModelAssistedFactory(target) && !isInternalViewModelAssistedFactoryUsage(source)
@@ -86,7 +91,7 @@ class ViewModelValidationPlugin : BindingGraphPlugin {
8691
"\nInjection of an assisted factory for Hilt ViewModel is prohibited since it " +
8792
"can not be used to create a ViewModel instance correctly.\nAccess the ViewModel via " +
8893
"the Android APIs (e.g. ViewModelProvider) instead." +
89-
"\nInjected factory: ${target.key().type()}\n"
94+
"\nInjected factory: ${target.key().type()}\n",
9095
)
9196
}
9297
}

java/dagger/internal/codegen/ComponentProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public Iterable<XProcessingStep> processingSteps() {
119119
return delegate.processingSteps();
120120
}
121121

122+
@Override
123+
public void preRound(XProcessingEnv env, XRoundEnv roundEnv) {
124+
delegate.onProcessingRoundBegin();
125+
}
126+
122127
@Override
123128
public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {
124129
delegate.postRound(env, roundEnv);

java/dagger/internal/codegen/DelegateComponentProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ public void initialize(
108108
DaggerDelegateComponentProcessor_Injector.factory()
109109
.create(env, plugins, legacyPlugins)
110110
.inject(this);
111+
validationBindingGraphPlugins.initializePlugins();
112+
externalBindingGraphPlugins.initializePlugins();
111113
}
112114

113115
public Iterable<XProcessingStep> processingSteps() {
114-
validationBindingGraphPlugins.initializePlugins();
115-
externalBindingGraphPlugins.initializePlugins();
116116

117117
return processingSteps;
118118
}
119119

120+
public void onProcessingRoundBegin() {
121+
externalBindingGraphPlugins.onProcessingRoundBegin();
122+
}
123+
120124
public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {
121125
if (!roundEnv.isProcessingOver()) {
122126
try {

java/dagger/internal/codegen/KspComponentProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public Iterable<XProcessingStep> processingSteps() {
5858
return delegate.processingSteps();
5959
}
6060

61+
@Override
62+
public void preRound(XProcessingEnv env, XRoundEnv roundEnv) {
63+
delegate.onProcessingRoundBegin();
64+
}
65+
6166
@Override
6267
public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {
6368
delegate.postRound(env, roundEnv);

java/dagger/internal/codegen/validation/ExternalBindingGraphPlugins.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ private void initializePlugin(BindingGraphPlugin plugin) {
8484
plugin.init(SpiModelBindingGraphConverter.toSpiModel(processingEnv), filteredOptions);
8585
}
8686

87+
public void onProcessingRoundBegin() {
88+
plugins.forEach(BindingGraphPlugin::onProcessingRoundBegin);
89+
}
90+
8791
private void initializeLegacyPlugin(dagger.spi.BindingGraphPlugin plugin) {
8892
plugin.initFiler(toJavac(filer));
8993
plugin.initTypes(toJavac(processingEnv).getTypeUtils()); // ALLOW_TYPES_ELEMENTS

java/dagger/spi/model/BindingGraphPlugin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ default String pluginName() {
5959
return getClass().getCanonicalName();
6060
}
6161

62+
/**
63+
* Runs before each round of Dagger annotation processing.
64+
*
65+
* <p>If using the plugin to process elements that need resetting at the beginning of each
66+
* processing round, use this function to perform the setup.
67+
*/
68+
default void onProcessingRoundBegin() {}
69+
6270
/**
6371
* Perform any extra work after the plugin finished all its visiting. This will be called once per
6472
* instance of this plugin, after all graphs were {@linkplain #visitGraph(BindingGraph,

0 commit comments

Comments
 (0)