Skip to content

Commit 96b4dff

Browse files
msridharError Prone Team
authored andcommitted
Lazily load plugin checks when running auto-patching
When running in normal checking mode, Error Prone loads plugin checks lazily to delay the loading until after the javac file manager is initialized; see [here](https://github.com/google/error-prone/blob/8f9e7da16b81b9fd65cca5fef9a79a4e6019eabd/check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java#L145-L159). This change introduces the same lazy loading when running Error Prone in auto-patching mode. In most scenarios I've seen, applying suggested fixes from plugin checks works fine, but when trying to generate patches on Bazel (see bazelbuild/bazel#21640), things do not work without this change. The previous code was "forcing" a `Supplier` and then re-wrapping the result in a `Supplier` again; this change mostly just gets rid of that. Fixes #4467 COPYBARA_INTEGRATE_REVIEW=#4467 from msridhar:lazy-load-plugin-checks-when-autopatching 728e3f9 PiperOrigin-RevId: 652638190
1 parent 8f9e7da commit 96b4dff

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,24 @@ public static ErrorProneAnalyzer createAnalyzer(
7272
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context);
7373

7474
// Refaster refactorer or using builtin checks
75-
CodeTransformer codeTransformer =
75+
Supplier<CodeTransformer> codeTransformer =
7676
epOptions
7777
.patchingOptions()
7878
.customRefactorer()
7979
.or(
80-
() -> {
81-
ScannerSupplier toUse = ErrorPronePlugins.loadPlugins(scannerSupplier, context);
82-
ImmutableSet<String> namedCheckers = epOptions.patchingOptions().namedCheckers();
83-
if (!namedCheckers.isEmpty()) {
84-
toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName()));
85-
} else {
86-
toUse = toUse.applyOverrides(epOptions);
87-
}
88-
return ErrorProneScannerTransformer.create(toUse.get());
89-
})
90-
.get();
80+
Suppliers.memoize(
81+
() -> {
82+
ScannerSupplier toUse =
83+
ErrorPronePlugins.loadPlugins(scannerSupplier, context);
84+
ImmutableSet<String> namedCheckers =
85+
epOptions.patchingOptions().namedCheckers();
86+
if (!namedCheckers.isEmpty()) {
87+
toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName()));
88+
} else {
89+
toUse = toUse.applyOverrides(epOptions);
90+
}
91+
return ErrorProneScannerTransformer.create(toUse.get());
92+
}));
9193

9294
return createWithCustomDescriptionListener(
9395
codeTransformer, epOptions, context, refactoringCollection[0]);
@@ -160,15 +162,12 @@ private static Supplier<CodeTransformer> scansPlugins(
160162
}
161163

162164
static ErrorProneAnalyzer createWithCustomDescriptionListener(
163-
CodeTransformer codeTransformer,
165+
Supplier<CodeTransformer> codeTransformer,
164166
ErrorProneOptions errorProneOptions,
165167
Context context,
166168
DescriptionListener.Factory descriptionListenerFactory) {
167169
return new ErrorProneAnalyzer(
168-
Suppliers.ofInstance(codeTransformer),
169-
errorProneOptions,
170-
context,
171-
descriptionListenerFactory);
170+
codeTransformer, errorProneOptions, context, descriptionListenerFactory);
172171
}
173172

174173
private ErrorProneAnalyzer(

0 commit comments

Comments
 (0)