Skip to content

Commit

Permalink
AnnotationPosition: don't fail on record compact constructors.
Browse files Browse the repository at this point in the history
Fixes external google#3494

PiperOrigin-RevId: 482752453

(cherry picked from commit 7db3f74)
  • Loading branch information
graememorgan authored and Stephan202 committed Oct 21, 2022
1 parent 6659d91 commit 067063b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private static List<ErrorProneToken> annotationTokens(
endPos = getStartPosition(methodTree.getReturnType());
} else if (!methodTree.getParameters().isEmpty()) {
endPos = getStartPosition(methodTree.getParameters().get(0));
if (endPos < annotationEnd) {
endPos = state.getEndPosition(methodTree);
}
} else if (methodTree.getBody() != null && !methodTree.getBody().getStatements().isEmpty()) {
endPos = getStartPosition(methodTree.getBody().getStatements().get(0));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,18 @@ public void varKeyword() {
.expectUnchanged()
.doTest(TEXT_MATCH);
}

@Test
public void recordAnnotation() {
assumeTrue(RuntimeVersion.isAtLeast16());
refactoringHelper
.addInputLines(
"Test.java",
"public record Test(String bar) {",
" @SuppressWarnings(\"unused\")",
" public Test {}",
"}")
.expectUnchanged()
.doTest(TEXT_MATCH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource)
private String maybeFormat(String input) {
try {
return new Formatter().formatSource(input);
} catch (FormatterException e) {
// TODO(b/254713810): Remove the AIOOBE catch below.
} catch (FormatterException | ArrayIndexOutOfBoundsException e) {
return input;
}
}
Expand Down

0 comments on commit 067063b

Please sign in to comment.