Skip to content

Commit ac4596f

Browse files
Fix Java 21 parser dropping var in record pattern binding variables (#6781)
* Fix Java 21 parser dropping `var` in record pattern binding variables * Replicate the same fix to the Java 25 parser --------- Co-authored-by: Oliver Wolff <23139298+cuioss@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 636926d commit ac4596f

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,8 +1743,9 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
17431743
typeExpr = convert(vartype);
17441744
}
17451745

1746-
if (typeExpr == null && node.declaredUsingVar()) {
1747-
typeExpr = new J.Identifier(randomId(), sourceBefore("var"), Markers.build(singletonList(JavaVarKeyword.build())), emptyList(), "var", typeMapping.type(vartype), null);
1746+
if (typeExpr == null && (node.declaredUsingVar() ||
1747+
((node.sym.flags() & Flags.MATCH_BINDING) != 0 && source.startsWith("var", indexOfNextNonWhitespace(cursor, source))))) {
1748+
typeExpr = new J.Identifier(randomId(), sourceBefore("var"), Markers.build(singletonList(JavaVarKeyword.build())), emptyList(), "var", vartype != null ? typeMapping.type(vartype) : typeMapping.type(node.sym.type), null);
17481749
}
17491750

17501751
if (typeExpr != null && !typeExprAnnotations.isEmpty()) {

rewrite-java-25/src/main/java/org/openrewrite/java/isolated/ReloadableJava25ParserVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,8 +1775,9 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
17751775
typeExpr = convert(vartype);
17761776
}
17771777

1778-
if (typeExpr == null && node.declaredUsingVar()) {
1779-
typeExpr = new J.Identifier(randomId(), sourceBefore("var"), Markers.build(singletonList(JavaVarKeyword.build())), emptyList(), "var", typeMapping.type(vartype), null);
1778+
if (typeExpr == null && (node.declaredUsingVar() ||
1779+
((node.sym.flags() & Flags.MATCH_BINDING) != 0 && source.startsWith("var", indexOfNextNonWhitespace(cursor, source))))) {
1780+
typeExpr = new J.Identifier(randomId(), sourceBefore("var"), Markers.build(singletonList(JavaVarKeyword.build())), emptyList(), "var", vartype != null ? typeMapping.type(vartype) : typeMapping.type(node.sym.type), null);
17801781
}
17811782

17821783
if (typeExpr != null && !typeExprAnnotations.isEmpty()) {

rewrite-java-tck/src/main/java/org/openrewrite/java/tree/RecordPatternMatchingTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ void printSum(Object obj) {
4343
);
4444
}
4545

46+
@Test
47+
void shouldParseRecordPatternWithVarKeyword() {
48+
rewriteRun(
49+
java(
50+
//language=java
51+
"""
52+
record Point(int x, int y) {}
53+
class Test {
54+
void printSum(Object obj) {
55+
if (obj instanceof Point(var x, var y)) {
56+
System.out.println(x + y);
57+
}
58+
}
59+
}
60+
"""
61+
)
62+
);
63+
}
64+
4665
@Test
4766
void shouldParseJava21NestedPatternMatchForRecords() {
4867
rewriteRun(

0 commit comments

Comments
 (0)