Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make patching API public #4466

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 @@ 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 +199,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,74 @@ 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]);
}

/** A {@link TaskListener} that performs refactorings. */
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
Loading