Skip to content

Commit

Permalink
Fix handling of variables without modifiers in AnnotationPosition
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 525856729
  • Loading branch information
cushon authored and Error Prone Team committed Apr 20, 2023
1 parent 789f361 commit f425b7e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ private static List<ErrorProneToken> annotationTokens(
endPos = state.getEndPosition(methodTree);
}
} else if (tree instanceof JCVariableDecl) {
endPos = state.getEndPosition(((JCVariableDecl) tree).getModifiers());
JCVariableDecl variableTree = (JCVariableDecl) tree;
endPos = getStartPosition(variableTree.getType());
if (endPos == -1) {
// handle 'var'
endPos = state.getEndPosition(variableTree.getModifiers());
}
} else if (tree instanceof JCClassDecl) {
JCClassDecl classTree = (JCClassDecl) tree;
endPos =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,66 @@ public void recordAnnotation() {
.expectUnchanged()
.doTest(TEXT_MATCH);
}

@Test
public void interspersedJavadoc_enum() {
refactoringHelper
.addInputLines(
"Test.java", //
"enum Test {",
" @NonTypeUse",
" /** Javadoc! */",
" ONE;",
"}")
.addOutputLines(
"Test.java", //
"enum Test {",
" /** Javadoc! */",
" @NonTypeUse",
" ONE;",
"}")
.doTest(TEXT_MATCH);
}

@Test
public void interspersedJavadoc_variableNoModifiers() {
refactoringHelper
.addInputLines(
"Test.java", //
"class Test {",
" @NonTypeUse",
" /** Javadoc! */",
" int x;",
"}")
.addOutputLines(
"Test.java", //
"class Test {",
" /** Javadoc! */",
" @NonTypeUse",
" int x;",
"}")
.doTest(TEXT_MATCH);
}

@Test
public void variable_genericType_modifiers() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.List;",
"class Test {",
" @TypeUse private List<?> x;",
" @EitherUse private List<?> y;",
" @NonTypeUse private List<?> z;",
"}")
.addOutputLines(
"Test.java",
"import java.util.List;",
"class Test {",
" private @TypeUse List<?> x;",
" private @EitherUse List<?> y;",
" @NonTypeUse private List<?> z;",
"}")
.doTest(TEXT_MATCH);
}
}

0 comments on commit f425b7e

Please sign in to comment.