From 96b4dfff376defed3fdf1eb6a9fe7ecd0122d007 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Mon, 15 Jul 2024 16:55:50 -0700 Subject: [PATCH] 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 https://github.com/bazelbuild/bazel/pull/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=https://github.com/google/error-prone/pull/4467 from msridhar:lazy-load-plugin-checks-when-autopatching 728e3f957d134f42766d3726d1e0a793f809744c PiperOrigin-RevId: 652638190 --- .../google/errorprone/ErrorProneAnalyzer.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java b/check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java index c59458442d0..589542587e8 100644 --- a/check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java +++ b/check_api/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java @@ -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 = epOptions .patchingOptions() .customRefactorer() .or( - () -> { - ScannerSupplier toUse = ErrorPronePlugins.loadPlugins(scannerSupplier, context); - ImmutableSet 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 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]); @@ -160,15 +162,12 @@ private static Supplier scansPlugins( } static ErrorProneAnalyzer createWithCustomDescriptionListener( - CodeTransformer codeTransformer, + Supplier codeTransformer, ErrorProneOptions errorProneOptions, Context context, DescriptionListener.Factory descriptionListenerFactory) { return new ErrorProneAnalyzer( - Suppliers.ofInstance(codeTransformer), - errorProneOptions, - context, - descriptionListenerFactory); + codeTransformer, errorProneOptions, context, descriptionListenerFactory); } private ErrorProneAnalyzer(