Skip to content

Commit

Permalink
Fix ASTMatcher for PatternInstanceofExpression when ast api level >= …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jjohnstn authored Oct 16, 2023
1 parent 398ba0b commit 6e0a691
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static Test suite() {
suite.addTest(new ASTMatcherTest(methods[i].getName(), JLS3_INTERNAL));
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=391898
suite.addTest(new ASTMatcherTest(methods[i].getName(), getJLS8()));
suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS17));
suite.addTest(new ASTMatcherTest(methods[i].getName(), AST.JLS21));
}
}
return suite;
Expand Down Expand Up @@ -273,7 +275,16 @@ public String getName() {
case JLS3_INTERNAL:
name = "JLS3 - " + name;
break;
}
case AST.JLS8:
name = "JLS8 - " + name;
break;
case AST.JLS17:
name = "JLS17 - " + name;
break;
case AST.JLS21:
name = "JLS21 - " + name;
break;
}
return name;
}

Expand Down Expand Up @@ -586,6 +597,12 @@ public boolean match(TypeMethodReference node, Object other) {
public boolean match(IntersectionType node, Object other) {
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
}
public boolean match(TypePattern node, Object other) {
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
}
public boolean match(PatternInstanceofExpression node, Object other) {
return standardBody(node, other, this.superMatch ? super.match(node, other) : false);
}
}

/**
Expand Down Expand Up @@ -1137,6 +1154,25 @@ public void testParenthesizedExpression() {
ParenthesizedExpression x1 = this.ast.newParenthesizedExpression();
basicMatch(x1);
}
public void testPatternInstanceofExpression() {
if (this.ast.apiLevel() < AST.JLS17) {
return;
}
PatternInstanceofExpression x1 = this.ast.newPatternInstanceofExpression();
x1.setLeftOperand(this.N1);
SingleVariableDeclaration svd = this.ast.newSingleVariableDeclaration();
svd.setType(this.T1);
svd.setName(this.N2);

if (this.ast.apiLevel() <= AST.JLS20) {
x1.setRightOperand(svd);
} else {
TypePattern tp = this.ast.newTypePattern();
tp.setPatternVariable(svd);
x1.setPattern(tp);
}
basicMatch(x1);
}
public void testPostfixExpression() {
PostfixExpression x1 = this.ast.newPostfixExpression();
x1.setOperand(this.E1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1976,15 +1976,19 @@ public boolean match(ParenthesizedExpression node, Object other) {
* different node type or is <code>null</code>
* @since 3.26
*/
@SuppressWarnings("deprecation")
public boolean match(PatternInstanceofExpression node, Object other) {
if (!(other instanceof PatternInstanceofExpression)) {
return false;
}
PatternInstanceofExpression o = (PatternInstanceofExpression) other;
return
safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
&& safeSubtreeMatch(node.getRightOperand(), o.getRightOperand());
AST ast= node.getAST();
if (ast.apiLevel() <= 20) {
return
safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
&& safeSubtreeMatch(node.getRightOperand(), o.getRightOperand());
}
return safeSubtreeMatch(node.getLeftOperand(), o.getLeftOperand())
&& safeSubtreeMatch(node.getPattern(), o.getPattern());
}

/**
Expand Down

0 comments on commit 6e0a691

Please sign in to comment.