Skip to content

Commit

Permalink
create diagnostic using JCDiagnostic.Factory::create
Browse files Browse the repository at this point in the history
Fixes #1083

MOE_MIGRATED_REVID=207798249
  • Loading branch information
iignatev authored and cushon committed Aug 7, 2018
1 parent b2463e0 commit fc62cbe
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
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
24 changes: 24 additions & 0 deletions check_api/src/main/java/com/google/errorprone/ErrorProneError.java
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.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
import com.sun.tools.javac.util.JCDiagnostic.Factory;
import com.sun.tools.javac.util.Log;
import javax.tools.JavaFileObject;

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

/** @deprecated prefer {@link #logFatalError(Log, Context)} */
@Deprecated
public void logFatalError(Log log) {
String version = ErrorProneVersion.loadVersionFromPom().or("unknown version");
JavaFileObject prev = log.currentSourceFile();
Expand All @@ -59,6 +64,25 @@ public void logFatalError(Log log) {
}
}

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

private static String formatMessage(
String checkName, JavaFileObject file, DiagnosticPosition pos, Throwable cause) {
DiagnosticSource source = new DiagnosticSource(file, /*log=*/ null);
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,14 @@ 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 +89,30 @@ 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;
}
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 +149,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,20 @@ 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

0 comments on commit fc62cbe

Please sign in to comment.