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

Fix errors when suggesting replacements to Lombok generated code #1195

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 @@ -20,6 +20,7 @@

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 @@ -68,7 +69,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 @@ -34,12 +34,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 +64,10 @@ public int endPosition() {

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

public static class InvalidReplacementPositionException extends IllegalArgumentException {
public InvalidReplacementPositionException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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 Down Expand Up @@ -79,7 +80,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