Skip to content

Commit

Permalink
Lazily load plugin checks when running auto-patching
Browse files Browse the repository at this point in the history
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

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4467 from msridhar:lazy-load-plugin-checks-when-autopatching 728e3f9
PiperOrigin-RevId: 652625022
  • Loading branch information
msridhar authored and Error Prone Team committed Jul 15, 2024
1 parent 8f9e7da commit 0d71758
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,24 @@ public static ErrorProneAnalyzer createAnalyzer(
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context);

// Refaster refactorer or using builtin checks
CodeTransformer codeTransformer =
Supplier<CodeTransformer> codeTransformer =
epOptions
.patchingOptions()
.customRefactorer()
.or(
() -> {
ScannerSupplier toUse = ErrorPronePlugins.loadPlugins(scannerSupplier, context);
ImmutableSet<String> namedCheckers = epOptions.patchingOptions().namedCheckers();
if (!namedCheckers.isEmpty()) {
toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName()));
} else {
toUse = toUse.applyOverrides(epOptions);
}
return ErrorProneScannerTransformer.create(toUse.get());
})
.get();
Suppliers.memoize(
() -> {
ScannerSupplier toUse =
ErrorPronePlugins.loadPlugins(scannerSupplier, context);
ImmutableSet<String> namedCheckers =
epOptions.patchingOptions().namedCheckers();
if (!namedCheckers.isEmpty()) {
toUse = toUse.filter(bci -> namedCheckers.contains(bci.canonicalName()));
} else {
toUse = toUse.applyOverrides(epOptions);
}
return ErrorProneScannerTransformer.create(toUse.get());
}));

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

static ErrorProneAnalyzer createWithCustomDescriptionListener(
CodeTransformer codeTransformer,
Supplier<CodeTransformer> codeTransformer,
ErrorProneOptions errorProneOptions,
Context context,
DescriptionListener.Factory descriptionListenerFactory) {
return new ErrorProneAnalyzer(
Suppliers.ofInstance(codeTransformer),
errorProneOptions,
context,
descriptionListenerFactory);
codeTransformer, errorProneOptions, context, descriptionListenerFactory);
}

private ErrorProneAnalyzer(
Expand Down

0 comments on commit 0d71758

Please sign in to comment.