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

error-prone doesn't work w/ latest javac version #1083

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 @@ -211,7 +211,7 @@ static ErrorProneAnalyzer createAnalyzer(
if (!epOptions.patchingOptions().doRefactor()) {
return ErrorProneAnalyzer.createByScanningForPlugins(scannerSupplier, epOptions, context);
}
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions());
refactoringCollection[0] = RefactoringCollection.refactor(epOptions.patchingOptions(), context);

// Refaster refactorer or using builtin checks
CodeTransformer codeTransformer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static ErrorProneAnalyzer createByScanningForPlugins(
scansPlugins(scannerSupplier, errorProneOptions, context),
errorProneOptions,
context,
JavacErrorDescriptionListener.provider());
JavacErrorDescriptionListener.provider(context));
}

private static Supplier<CodeTransformer> scansPlugins(
Expand Down Expand Up @@ -102,7 +102,7 @@ public ErrorProneAnalyzer(
scansPlugins(scannerSupplier, errorProneOptions, context),
errorProneOptions,
context,
JavacErrorDescriptionListener.provider());
JavacErrorDescriptionListener.provider(context));
}

private ErrorProneAnalyzer(
Expand Down Expand Up @@ -152,7 +152,7 @@ public void finished(TaskEvent taskEvent) {
transformer.get().apply(new TreePath(compilation), subContext, descriptionListener);
}
} catch (ErrorProneError e) {
e.logFatalError(log);
e.logFatalError(log, context);
// let the exception propagate to javac's main, where it will cause the compilation to
// terminate with Result.ABNORMAL
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.DiagnosticSource;
import com.sun.tools.javac.util.JCDiagnostic.Factory;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
import com.sun.tools.javac.util.Log;
import javax.tools.JavaFileObject;

Expand Down Expand Up @@ -47,15 +50,14 @@ public ErrorProneError(
this.source = source;
}

public void logFatalError(Log log) {
public void logFatalError(Log log, Context context) {
String version = ErrorProneVersion.loadVersionFromPom().or("unknown version");
JavaFileObject prev = log.currentSourceFile();
JavaFileObject originalSource = log.useSource(source);
Factory factory = Factory.instance(context);
try {
log.useSource(source);
log.error(
pos, "error.prone.crash", Throwables.getStackTraceAsString(cause), version, checkName);
log.report(factory.create(DiagnosticType.ERROR, log.currentSource(), pos, "error.prone.crash", Throwables.getStackTraceAsString(cause), version, checkName));
} finally {
log.useSource(prev);
log.useSource(originalSource);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.Log;
import java.io.IOError;
Expand All @@ -44,6 +46,7 @@ public class JavacErrorDescriptionListener implements DescriptionListener {
private final Log log;
private final JavaFileObject sourceFile;
private final Function<Fix, AppliedFix> fixToAppliedFix;
private final Context context;

// When we're trying to refactor using error prone fixes, any error halts compilation of other
// files. We set this to true when refactoring so we can log every hit without breaking the
Expand All @@ -54,9 +57,10 @@ public class JavacErrorDescriptionListener implements DescriptionListener {
private static final String MESSAGE_BUNDLE_KEY = "error.prone";

private JavacErrorDescriptionListener(
Log log, EndPosTable endPositions, JavaFileObject sourceFile, boolean dontUseErrors) {
Log log, EndPosTable endPositions, JavaFileObject sourceFile, Context context, boolean dontUseErrors) {
this.log = log;
this.sourceFile = sourceFile;
this.context = context;
this.dontUseErrors = dontUseErrors;
checkNotNull(endPositions);
try {
Expand All @@ -81,26 +85,32 @@ public void onDescribed(Description description) {
String message = messageForFixes(description, appliedFixes);
// Swap the log's source and the current file's source; then be sure to swap them back later.
JavaFileObject originalSource = log.useSource(sourceFile);
switch (description.severity) {
case ERROR:
if (dontUseErrors) {
log.warning((DiagnosticPosition) description.node, MESSAGE_BUNDLE_KEY, message);
} else {
log.error((DiagnosticPosition) description.node, MESSAGE_BUNDLE_KEY, message);
}
break;
case WARNING:
log.warning((DiagnosticPosition) description.node, MESSAGE_BUNDLE_KEY, message);
break;
case SUGGESTION:
log.note((DiagnosticPosition) description.node, MESSAGE_BUNDLE_KEY, message);
break;
default:
break;
}

if (originalSource != null) {
log.useSource(originalSource);
try {
JCDiagnostic.Factory factory = JCDiagnostic.Factory.instance(context);
JCDiagnostic.DiagnosticType type = JCDiagnostic.DiagnosticType.ERROR;
DiagnosticPosition pos = (DiagnosticPosition) description.node;
switch (description.severity) {
case ERROR:
if (dontUseErrors) {
type = JCDiagnostic.DiagnosticType.WARNING;
} else {
type = JCDiagnostic.DiagnosticType.ERROR;
}
break;
case WARNING:
type = JCDiagnostic.DiagnosticType.WARNING;
break;
case SUGGESTION:
type = JCDiagnostic.DiagnosticType.NOTE;
break;
default:
break;
}
log.report(factory.create(type, log.currentSource(), pos, MESSAGE_BUNDLE_KEY, message));
} finally {
if (originalSource != null) {
log.useSource(originalSource);
}
}
}

Expand Down Expand Up @@ -137,15 +147,15 @@ private static String messageForFixes(Description description, List<AppliedFix>
return messageBuilder.toString();
}

static Factory provider() {
static Factory provider(Context context) {
return (log, compilation) ->
new JavacErrorDescriptionListener(
log, compilation.endPositions, compilation.getSourceFile(), false);
log, compilation.endPositions, compilation.getSourceFile(), context, false);
}

static Factory providerForRefactoring() {
static Factory providerForRefactoring(Context context) {
return (log, compilation) ->
new JavacErrorDescriptionListener(
log, compilation.endPositions, compilation.getSourceFile(), true);
log, compilation.endPositions, compilation.getSourceFile(), context, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.errorprone.apply.SourceFile;
import com.google.errorprone.matchers.Description;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import java.io.IOError;
import java.io.IOException;
Expand Down Expand Up @@ -76,7 +77,7 @@ enum RefactoringResultType {
CHANGED,
}

static RefactoringCollection refactor(PatchingOptions patchingOptions) {
static RefactoringCollection refactor(PatchingOptions patchingOptions, Context context) {
Path rootPath = buildRootPath();
FileDestination fileDestination;
Function<URI, RefactoringResult> postProcess;
Expand Down Expand Up @@ -119,18 +120,19 @@ public RefactoringResult apply(URI uri) {
}

ImportOrganizer importOrganizer = patchingOptions.importOrganizer();
return new RefactoringCollection(rootPath, fileDestination, postProcess, importOrganizer);
return new RefactoringCollection(rootPath, fileDestination, postProcess, importOrganizer, context);
}

private RefactoringCollection(
Path rootPath,
FileDestination fileDestination,
Function<URI, RefactoringResult> postProcess,
ImportOrganizer importOrganizer) {
ImportOrganizer importOrganizer,
Context context) {
this.rootPath = rootPath;
this.fileDestination = fileDestination;
this.postProcess = postProcess;
this.descriptionsFactory = JavacErrorDescriptionListener.providerForRefactoring();
this.descriptionsFactory = JavacErrorDescriptionListener.providerForRefactoring(context);
this.importOrganizer = importOrganizer;
}

Expand Down