Skip to content

Commit

Permalink
Fix replacement exceptions in auto-generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasser Ganjisaffar committed Jan 12, 2021
1 parent d7f6466 commit 1153e9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.fixes.AppliedFix;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.fixes.Replacement;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.Tree.Kind;
import com.sun.tools.javac.tree.EndPosTable;
Expand Down Expand Up @@ -83,7 +84,15 @@ private JavacErrorDescriptionListener(
checkNotNull(endPositions);
try {
CharSequence sourceFileContent = sourceFile.getCharContent(true);
fixToAppliedFix = fix -> AppliedFix.fromSource(sourceFileContent, endPositions).apply(fix);
fixToAppliedFix = fix -> {
try {
return AppliedFix.fromSource(sourceFileContent, endPositions).apply(fix);
} catch (Replacement.InvalidReplacementPositionException e) {
// Not possible to auto-suggest a fix, for example inside
// Lombok auto-generated code
return null;
}
};
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.errorprone.fixes;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.auto.value.AutoValue;
import com.google.common.collect.Range;

Expand All @@ -34,12 +32,13 @@ public abstract class Replacement {
* @param replaceWith the replacement text
*/
public static Replacement create(int startPosition, int endPosition, String replaceWith) {
checkArgument(
startPosition >= 0 && startPosition <= endPosition,
"invalid replacement: [%s, %s) (%s)",
startPosition,
endPosition,
replaceWith);
if (startPosition < 0 || startPosition > endPosition) {
throw new InvalidReplacementPositionException(String.format(
"invalid replacement: [%s, %s) (%s)",
startPosition,
endPosition,
replaceWith));
}
return new AutoValue_Replacement(Range.closedOpen(startPosition, endPosition), replaceWith);
}

Expand All @@ -63,4 +62,11 @@ public int endPosition() {

/** The source text to appear in the output. */
public abstract String replaceWith();

public static class InvalidReplacementPositionException extends IllegalArgumentException {
InvalidReplacementPositionException(String message) {
super(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import static com.google.errorprone.matchers.Matchers.instanceEqualsInvocation;
import static com.google.errorprone.matchers.Matchers.staticEqualsInvocation;

import java.util.List;
import java.util.Optional;

import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher;
import com.google.errorprone.dataflow.nullnesspropagation.Nullness;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.fixes.Replacement;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
Expand All @@ -35,8 +39,6 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.TreePath;
import java.util.List;
import java.util.Optional;

/**
* Abstract implementation of a BugPattern that detects the use of reference equality to compare
Expand Down Expand Up @@ -75,7 +77,12 @@ public final Description matchBinary(BinaryTree tree, VisitorState state) {
}

Description.Builder builder = buildDescription(tree);
addFixes(builder, tree, state);
try {
addFixes(builder, tree, state);
} catch (Replacement.InvalidReplacementPositionException e) {
// Not possible to auto-suggest a fix, for example inside
// Lombok auto-generated setters which use "=="
}
return builder.build();
}

Expand Down

0 comments on commit 1153e9e

Please sign in to comment.