Skip to content
Merged
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 @@ -327,18 +327,16 @@ public static class ReplaceVarTypes extends BugChecker implements VariableTreeMa
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Tree type = tree.getType();
if (ASTHelpers.hasExplicitSource(type, state)) {
return Description.NO_MATCH;
}
return describeMatch(type, SuggestedFix.replace(type, "Object"));
}
}

@SuppressWarnings("MissingTestCall") // used in a method reference in assertThrows
@Test
public void replaceVarTypes() {
// after JDK-8358604, var types have source positions
assume().that(Runtime.version().feature()).isLessThan(27);
// after JDK-8358604 in JDK 27, var types have source positions
// after JDK-8359383 in JDK 26, var type start and end positions are the same
assume().that(Runtime.version().feature()).isLessThan(26);
BugCheckerRefactoringTestHelper helper =
BugCheckerRefactoringTestHelper.newInstance(ReplaceVarTypes.class, getClass())
.addInputLines(
Expand All @@ -362,4 +360,32 @@ public void foo() {
""");
assertThat(e).hasMessageThat().contains("BugPattern: ReplaceVarTypes");
}

@Test
public void varTypeSourcePositions() {
// after JDK-8358604 in JDK 27, var types have source positions
assume().that(Runtime.version().feature()).isAtLeast(27);
BugCheckerRefactoringTestHelper.newInstance(ReplaceVarTypes.class, getClass())
.addInputLines(
"Test.java",
"""
public class Test {
public void foo() {
var x = 1 + 2;
System.out.println(x);
}
}
""")
.addOutputLines(
"out/Test.java",
"""
public class Test {
public void foo() {
Object x = 1 + 2;
System.out.println(x);
}
}
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,9 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
@SuppressWarnings("MissingTestCall") // used in a method reference in assertThrows
@Test
public void replaceVarTypes() {
// after JDK-8358604, var types have source positions
assume().that(Runtime.version().feature()).isLessThan(27);
// after JDK-8358604 in JDK 27, var types have source positions
// after JDK-8359383 in JDK 26, var type start and end positions are the same
assume().that(Runtime.version().feature()).isLessThan(26);
var compilationTestHelper =
CompilationTestHelper.newInstance(ReplaceVarTypes.class, getClass())
.addSourceLines(
Expand Down
Loading