Skip to content

Commit

Permalink
Make patching API public
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalvador committed May 4, 2024
1 parent 3d065de commit 905d626
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@
import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.RefactoringCollection.RefactoringResult;
import com.google.errorprone.scanner.ErrorProneScannerTransformer;
import com.google.errorprone.scanner.ScannerSupplier;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskEvent.Kind;
import com.sun.source.util.TaskListener;
import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Log.WriterKind;
import com.sun.tools.javac.util.Options;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.EnumSet;
Expand Down Expand Up @@ -94,9 +85,9 @@ static void addTaskListener(
setupMessageBundle(context);
RefactoringCollection[] refactoringCollection = {null};
javacTask.addTaskListener(
createAnalyzer(scannerSupplier, errorProneOptions, context, refactoringCollection));
ErrorProneAnalyzer.createAnalyzer(scannerSupplier, errorProneOptions, context, refactoringCollection));
if (refactoringCollection[0] != null) {
javacTask.addTaskListener(new RefactoringTask(context, refactoringCollection[0]));
javacTask.addTaskListener(new ErrorProneAnalyzer.RefactoringTask(context, refactoringCollection[0]));
}
}

Expand Down Expand Up @@ -206,71 +197,4 @@ public static void setupMessageBundle(Context context) {
ResourceBundle bundle = ResourceBundle.getBundle("com.google.errorprone.errors");
JavacMessages.instance(context).add(l -> bundle);
}

static ErrorProneAnalyzer createAnalyzer(
ScannerSupplier scannerSupplier,
ErrorProneOptions epOptions,
Context context,
RefactoringCollection[] refactoringCollection) {
if (!epOptions.patchingOptions().doRefactor()) {
return ErrorProneAnalyzer.createByScanningForPlugins(scannerSupplier, epOptions, context);
}
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context);

// Refaster refactorer or using builtin checks
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();

return ErrorProneAnalyzer.createWithCustomDescriptionListener(
codeTransformer, epOptions, context, refactoringCollection[0]);
}

static class RefactoringTask implements TaskListener {

private final Context context;
private final RefactoringCollection refactoringCollection;

public RefactoringTask(Context context, RefactoringCollection refactoringCollection) {
this.context = context;
this.refactoringCollection = refactoringCollection;
}

@Override
public void started(TaskEvent event) {}

@Override
public void finished(TaskEvent event) {
if (event.getKind() != Kind.GENERATE) {
return;
}
RefactoringResult refactoringResult;
try {
refactoringResult = refactoringCollection.applyChanges(event.getSourceFile().toUri());
} catch (Exception e) {
PrintWriter out = Log.instance(context).getWriter(WriterKind.ERROR);
out.println(e.getMessage());
out.flush();
return;
}
if (refactoringResult.type() == RefactoringCollection.RefactoringResultType.CHANGED) {
PrintWriter out = Log.instance(context).getWriter(WriterKind.NOTICE);
out.println(refactoringResult.message());
out.flush();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.RefactoringCollection.RefactoringResult;
import com.google.errorprone.scanner.ErrorProneScannerTransformer;
import com.google.errorprone.scanner.ScannerSupplier;
import com.google.errorprone.util.ASTHelpers;
Expand All @@ -39,7 +41,9 @@
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Log.WriterKind;
import com.sun.tools.javac.util.PropagatedException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
Expand All @@ -57,6 +61,73 @@ public class ErrorProneAnalyzer implements TaskListener {
private final Context context;
private final DescriptionListener.Factory descriptionListenerFactory;

public static ErrorProneAnalyzer createAnalyzer(
ScannerSupplier scannerSupplier,
ErrorProneOptions epOptions,
Context context,
RefactoringCollection[] refactoringCollection) {
if (!epOptions.patchingOptions().doRefactor()) {
return createByScanningForPlugins(scannerSupplier, epOptions, context);
}
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context);

// Refaster refactorer or using builtin checks
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();

return createWithCustomDescriptionListener(
codeTransformer, epOptions, context, refactoringCollection[0]);
}

public static class RefactoringTask implements TaskListener {

private final Context context;
private final RefactoringCollection refactoringCollection;

public RefactoringTask(Context context, RefactoringCollection refactoringCollection) {
this.context = context;
this.refactoringCollection = refactoringCollection;
}

@Override
public void started(TaskEvent event) {}

@Override
public void finished(TaskEvent event) {
if (event.getKind() != Kind.GENERATE) {
return;
}
RefactoringResult refactoringResult;
try {
refactoringResult = refactoringCollection.applyChanges(event.getSourceFile().toUri());
} catch (Exception e) {
PrintWriter out = Log.instance(context).getWriter(WriterKind.ERROR);
out.println(e.getMessage());
out.flush();
return;
}
if (refactoringResult.type() == RefactoringCollection.RefactoringResultType.CHANGED) {
PrintWriter out = Log.instance(context).getWriter(WriterKind.NOTICE);
out.println(refactoringResult.message());
out.flush();
}
}
}

public static ErrorProneAnalyzer createByScanningForPlugins(
ScannerSupplier scannerSupplier, ErrorProneOptions errorProneOptions, Context context) {
return new ErrorProneAnalyzer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import java.util.logging.Logger;

/** A container of fixes that have been collected during a single compilation phase. */
class RefactoringCollection implements DescriptionListener.Factory {
public class RefactoringCollection implements DescriptionListener.Factory {

private static final Logger logger = Logger.getLogger(RefactoringCollection.class.getName());

Expand Down

0 comments on commit 905d626

Please sign in to comment.