Skip to content

Commit

Permalink
Fix visit(PatternInstanceofExpression node) in NaiveASTFlattener (#2285)
Browse files Browse the repository at this point in the history
#2285

Author: twasik <[email protected]>
Also-by: Jay Arthanareeswaran <[email protected]>
  • Loading branch information
twasik81 authored Jul 1, 2024
1 parent 975e7c9 commit 104a635
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation and others.
* Copyright (c) 2020, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -21,6 +21,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand Down Expand Up @@ -560,4 +561,30 @@ public void testRecordPattern004() throws CoreException {
GuardedPattern guardedPattern = (GuardedPattern)caseStmt.expressions().get(0);
assertEquals("There should be 1 Record Pattern", ASTNode.RECORD_PATTERN , guardedPattern.getPattern().getNodeType());
}
public void testVisitPattern() {
class NodeFinder extends ASTVisitor {
PatternInstanceofExpression patternInstanceofExpression;
public boolean visit(PatternInstanceofExpression node) {
this.patternInstanceofExpression = node;
return super.visit(node);
}
}
String content = """
public class Foo {
static boolean checkCollisionIfs(Object p1, Object p2) {
if (p1 instanceof Point(int x1, int y1)) {
return x1 == x2 && y1 == y2;
}
}
record Point(int x, int y) {}
record ColoredPoint(Point T, String color) {}
record Decorator(Object obj) {}}""";

ASTParser parser = ASTParser.newParser(AST.JLS21);
parser.setSource(content.toCharArray());
CompilationUnit cunit = (CompilationUnit) parser.createAST(null);
NodeFinder astVisitor = new NodeFinder();
cunit.accept(astVisitor);
assertEquals("Wrong node", "p1 instanceof Point(int x1, int y1)", astVisitor.patternInstanceofExpression.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 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 @@ -88,6 +88,13 @@ public class NaiveASTFlattener extends ASTVisitor {
*/
private static final int JLS14 = AST.JLS14;

/**
* Internal synonym for {@link AST#JLS21}. Use to alleviate
* deprecation warnings.
* @deprecated
*/
private static final int JLS21 = AST.JLS21;

/**
* The string buffer into which the serialized representation of the AST is
* written.
Expand Down Expand Up @@ -899,7 +906,11 @@ public boolean visit(InstanceofExpression node) {
public boolean visit(PatternInstanceofExpression node) {
node.getLeftOperand().accept(this);
this.buffer.append(" instanceof ");//$NON-NLS-1$
node.getRightOperand().accept(this);
if (node.getAST().apiLevel() >= JLS21) {
node.getPattern().accept(this);
} else {
node.getRightOperand().accept(this);
}
return false;
}

Expand Down

0 comments on commit 104a635

Please sign in to comment.