Skip to content

Commit 6596f74

Browse files
committed
Java 17 instanceof pattern matching (fixes #2158)
1 parent 894c511 commit 6596f74

File tree

8 files changed

+83
-2
lines changed

8 files changed

+83
-2
lines changed

rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ public J visitInstanceOf(InstanceOfTree node, Space fmt) {
712712
return new J.InstanceOf(randomId(), fmt, Markers.EMPTY,
713713
convert(node.getExpression(), t -> sourceBefore("instanceof")),
714714
convert(node.getType()),
715+
null,
715716
typeMapping.type(node));
716717
}
717718

rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,14 @@ public J visitImport(ImportTree node, Space fmt) {
708708

709709
@Override
710710
public J visitInstanceOf(InstanceOfTree node, Space fmt) {
711+
JavaType type = typeMapping.type(node);
711712
return new J.InstanceOf(randomId(), fmt, Markers.EMPTY,
712713
convert(node.getExpression(), t -> sourceBefore("instanceof")),
713714
convert(node.getType()),
714-
typeMapping.type(node));
715+
node.getPattern() instanceof JCBindingPattern b ?
716+
new J.Identifier(randomId(), sourceBefore(b.getVariable().getName().toString()), Markers.EMPTY, b.getVariable().getName().toString(),
717+
type, null) : null,
718+
type);
715719
}
716720

717721
@Override

rewrite-java-17/src/test/java/org/openrewrite/java/cleanup/UseDiamondOperatorWithVarTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.openrewrite.java.cleanup;
217

318
import org.junit.jupiter.api.Test;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.tree;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.java.JavaVisitor;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
public class InstanceOfTest implements RewriteTest {
26+
27+
@Test
28+
void patternMatch() {
29+
rewriteRun(
30+
java(
31+
"""
32+
import java.util.*;
33+
class Test {
34+
public void match(Collection<?> c) {
35+
if (c instanceof List l) {
36+
System.out.println("List");
37+
} else if (c instanceof Set s) {
38+
System.out.println("Set");
39+
}
40+
}
41+
}
42+
""",
43+
spec -> spec.afterRecipe(cu -> new JavaVisitor<Integer>() {
44+
@Override
45+
public J visitInstanceOf(J.InstanceOf instanceOf, Integer integer) {
46+
assertThat(instanceOf.getPattern()).isNotNull();
47+
return instanceOf;
48+
}
49+
}.visit(cu, 0))
50+
)
51+
);
52+
}
53+
}

rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ public J visitInstanceOf(InstanceOfTree node, Space fmt) {
710710
return new J.InstanceOf(randomId(), fmt, Markers.EMPTY,
711711
convert(node.getExpression(), t -> sourceBefore("instanceof")),
712712
convert(node.getType()),
713+
null,
713714
typeMapping.type(node));
714715
}
715716

rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ public J visitInstanceOf(InstanceOf instanceOf, PrintOutputCapture<P> p) {
604604
visitMarkers(instanceOf.getMarkers(), p);
605605
visitRightPadded(instanceOf.getPadding().getExpr(), JRightPadded.Location.INSTANCEOF, "instanceof", p);
606606
visit(instanceOf.getClazz(), p);
607+
visit(instanceOf.getPattern(), p);
607608
return instanceOf;
608609
}
609610

rewrite-java/src/main/java/org/openrewrite/java/JavaVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ public J visitInstanceOf(J.InstanceOf instanceOf, P p) {
682682
}
683683
i = i.getPadding().withExpr(visitRightPadded(i.getPadding().getExpr(), JRightPadded.Location.INSTANCEOF, p));
684684
i = i.withClazz(visitAndCast(i.getClazz(), p));
685+
i = i.withPattern(visitAndCast(i.getPattern(), p));
685686
i = i.withType(visitType(i.getType(), p));
686687
return i;
687688
}

rewrite-java/src/main/java/org/openrewrite/java/tree/J.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,11 @@ public InstanceOf withExpression(Expression expression) {
25272527
@Getter
25282528
J clazz;
25292529

2530+
@Nullable
2531+
@With
2532+
@Getter
2533+
J pattern;
2534+
25302535
@With
25312536
@Nullable
25322537
@Getter
@@ -2571,7 +2576,7 @@ public JRightPadded<Expression> getExpr() {
25712576
}
25722577

25732578
public InstanceOf withExpr(JRightPadded<Expression> expression) {
2574-
return t.expression == expression ? t : new InstanceOf(t.id, t.prefix, t.markers, expression, t.clazz, t.type);
2579+
return t.expression == expression ? t : new InstanceOf(t.id, t.prefix, t.markers, expression, t.clazz, t.pattern, t.type);
25752580
}
25762581
}
25772582

0 commit comments

Comments
 (0)