Skip to content

Commit 6e0a691

Browse files
authored
Fix ASTMatcher for PatternInstanceofExpression when ast api level >= 21 (eclipse-jdt#1481)
* Fix ASTMatcher for PatternInstanceofExpression when ast level >= 21 - fix the match method for PatternInstanceofExpression to use the getRightOperand() when ast level <= 20 and the getPattern() method when ast level 21 or higher - fixes: eclipse-jdt#1480 * Add test to ASTMatcherTest
1 parent 398ba0b commit 6e0a691

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTMatcherTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static Test suite() {
5252
suite.addTest(new ASTMatcherTest(methods[i].getName(), JLS3_INTERNAL));
5353
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898
5454
suite.addTest(new ASTMatcherTest(methods[i].getName(), getJLS8()));
55+
suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS17));
56+
suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS21));
5557
}
5658
}
5759
return suite;
@@ -273,7 +275,16 @@ public String getName() {
273275
case JLS3_INTERNAL:
274276
name = "JLS3 - " + name;
275277
break;
276-
}
278+
case AST.JLS8:
279+
name = "JLS8 - " + name;
280+
break;
281+
case AST.JLS17:
282+
name = "JLS17 - " + name;
283+
break;
284+
case AST.JLS21:
285+
name = "JLS21 - " + name;
286+
break;
287+
}
277288
return name;
278289
}
279290

@@ -586,6 +597,12 @@ public boolean match(TypeMethodReference node, Object other) {
586597
public boolean match(IntersectionType node, Object other) {
587598
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
588599
}
600+
public boolean match(TypePattern node, Object other) {
601+
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
602+
}
603+
public boolean match(PatternInstanceofExpression node, Object other) {
604+
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
605+
}
589606
}
590607

591608
/**
@@ -1137,6 +1154,25 @@ public void testParenthesizedExpression() {
11371154
ParenthesizedExpression x1 = this.ast.newParenthesizedExpression();
11381155
basicMatch(x1);
11391156
}
1157+
public void testPatternInstanceofExpression() {
1158+
if (this.ast.apiLevel() < AST.JLS17) {
1159+
return;
1160+
}
1161+
PatternInstanceofExpression x1 = this.ast.newPatternInstanceofExpression();
1162+
x1.setLeftOperand(this.N1);
1163+
SingleVariableDeclaration svd = this.ast.newSingleVariableDeclaration();
1164+
svd.setType(this.T1);
1165+
svd.setName(this.N2);
1166+
1167+
if (this.ast.apiLevel() <= AST.JLS20) {
1168+
x1.setRightOperand(svd);
1169+
} else {
1170+
TypePattern tp = this.ast.newTypePattern();
1171+
tp.setPatternVariable(svd);
1172+
x1.setPattern(tp);
1173+
}
1174+
basicMatch(x1);
1175+
}
11401176
public void testPostfixExpression() {
11411177
PostfixExpression x1 = this.ast.newPostfixExpression();
11421178
x1.setOperand(this.E1);

org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTMatcher.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -1976,15 +1976,19 @@ public boolean match(ParenthesizedExpression node, Object other) {
19761976
* different node type or is <code>null</code>
19771977
* @since 3.26
19781978
*/
1979-
@SuppressWarnings("deprecation")
19801979
public boolean match(PatternInstanceofExpression node, Object other) {
19811980
if (!(other instanceof PatternInstanceofExpression)) {
19821981
return false;
19831982
}
19841983
PatternInstanceofExpression o = (PatternInstanceofExpression) other;
1985-
return
1986-
safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
1987-
&& safeSubtreeMatch(node.getRightOperand(), o.getRightOperand());
1984+
AST ast= node.getAST();
1985+
if (ast.apiLevel() <= 20) {
1986+
return
1987+
safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
1988+
&& safeSubtreeMatch(node.getRightOperand(), o.getRightOperand());
1989+
}
1990+
return safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
1991+
&& safeSubtreeMatch(node.getPattern(), o.getPattern());
19881992
}
19891993

19901994
/**

0 commit comments

Comments
 (0)