@@ -440,4 +440,102 @@ protected String getString(Number number) {
440440 assertEquals ("SingleVariableDeclaration type" , typePattern .getPatternVariable2 ().getNodeType (), ASTNode .SINGLE_VARIABLE_DECLARATION );
441441 assertEquals ("pattern variable name" , ((SingleVariableDeclaration ) typePattern .getPatternVariable2 ()).getType ().toString (), "Long" );
442442 }
443+
444+ //SingleVariableDeclaration with name and type
445+ public void testNaiveASTFlattnerSync_a () throws JavaModelException {
446+ String contents = """
447+ public class X {
448+ public static void main(Object object) {
449+ if (object instanceof Pos(int x1, int y1)) {
450+ System.out.println("object is a path starting at x = "+x1 + " , y = "+ y1 +"%n");
451+ }
452+ }
453+ }
454+ record Pos(int x, int y) {}
455+ """ ;
456+ this .workingCopy = getWorkingCopy ("/Converter_21/src/X.java" , true /*resolve*/ );
457+ ASTNode node = buildAST (contents , this .workingCopy );
458+ assertEquals ("Wrong type of statement" , ASTNode .COMPILATION_UNIT , node .getNodeType ());
459+ CompilationUnit cu = (CompilationUnit ) node ;
460+
461+ TypeDeclaration typedeclaration = (TypeDeclaration ) cu .types ().get (0 );
462+ MethodDeclaration methodDeclaration = (MethodDeclaration ) typedeclaration .bodyDeclarations ().get (0 );
463+ Block block = methodDeclaration .getBody ();
464+ List <ASTNode > statements = block .statements ();
465+ IfStatement ifStatement = (IfStatement ) statements .get (0 );
466+ PatternInstanceofExpression expression = (PatternInstanceofExpression ) ifStatement .getExpression ();
467+ RecordPattern rp = (RecordPattern ) expression .getPattern ();
468+ List <Pattern > patterns = rp .patterns ();
469+ TypePattern secondPattern = (TypePattern ) patterns .get (1 );
470+ SingleVariableDeclaration svd = secondPattern .getPatternVariable ();
471+ assertEquals ("SingleVariableDeclaration type" , svd .getNodeType (), ASTNode .SINGLE_VARIABLE_DECLARATION );
472+ assertEquals ("SimpleType" , svd .getType ().getNodeType (), ASTNode .PRIMITIVE_TYPE );
473+ assertEquals ("SimpleName" , svd .getName ().getNodeType (), ASTNode .SIMPLE_NAME );
474+ }
475+
476+ //SingleVariableDeclaration with Type and name as _
477+ public void testNaiveASTFlattnerSync_b () throws JavaModelException {
478+ String contents = """
479+ public class X {
480+ public static void main(Object object) {
481+ if (object instanceof Pos(int x1, int _)) {
482+ System.out.println("object is a path starting at x = "+x1);
483+ }
484+ }
485+ }
486+ record Pos(int x, int y) {}
487+ """ ;
488+ this .workingCopy = getWorkingCopy ("/Converter_23/src/X.java" , true /*resolve*/ );
489+ ASTNode node = buildAST (contents , this .workingCopy );
490+ assertEquals ("Wrong type of statement" , ASTNode .COMPILATION_UNIT , node .getNodeType ());
491+ CompilationUnit cu = (CompilationUnit ) node ;
492+
493+ TypeDeclaration typedeclaration = (TypeDeclaration ) cu .types ().get (0 );
494+ MethodDeclaration methodDeclaration = (MethodDeclaration ) typedeclaration .bodyDeclarations ().get (0 );
495+ Block block = methodDeclaration .getBody ();
496+ List <ASTNode > statements = block .statements ();
497+ IfStatement ifStatement = (IfStatement ) statements .get (0 );
498+ PatternInstanceofExpression expression = (PatternInstanceofExpression ) ifStatement .getExpression ();
499+ RecordPattern rp = (RecordPattern ) expression .getPattern ();
500+ List <Pattern > patterns = rp .patterns ();
501+ TypePattern secondPattern = (TypePattern ) patterns .get (1 );
502+ SingleVariableDeclaration svd = (SingleVariableDeclaration ) secondPattern .getPatternVariable2 ();
503+ assertEquals ("SingleVariableDeclaration type" , svd .getNodeType (), ASTNode .SINGLE_VARIABLE_DECLARATION );
504+ assertEquals ("SimpleType" , svd .getType ().getNodeType (), ASTNode .PRIMITIVE_TYPE );
505+ assertEquals ("SimpleName" , svd .getName ().getNodeType (), ASTNode .SIMPLE_NAME );
506+ }
507+
508+ //VariableDeclarationFragment
509+ public void testNaiveASTFlattnerSync_c () throws JavaModelException {
510+ String contents = """
511+ public class X {
512+ public static void main(Object object) {
513+ if (object instanceof Pos(int x1, _)) {
514+ System.out.println("object is a path starting at x = "+x1);
515+ }
516+ }
517+ }
518+ record Pos(int x, int y) {}
519+ """ ;
520+ this .workingCopy = getWorkingCopy ("/Converter_23/src/X.java" , true /*resolve*/ );
521+ ASTNode node = buildAST (contents , this .workingCopy );
522+ assertEquals ("Wrong type of statement" , ASTNode .COMPILATION_UNIT , node .getNodeType ());
523+ CompilationUnit cu = (CompilationUnit ) node ;
524+
525+ TypeDeclaration typedeclaration = (TypeDeclaration ) cu .types ().get (0 );
526+ MethodDeclaration methodDeclaration = (MethodDeclaration ) typedeclaration .bodyDeclarations ().get (0 );
527+ Block block = methodDeclaration .getBody ();
528+ List <ASTNode > statements = block .statements ();
529+ IfStatement ifStatement = (IfStatement ) statements .get (0 );
530+ PatternInstanceofExpression expression = (PatternInstanceofExpression ) ifStatement .getExpression ();
531+ RecordPattern rp = (RecordPattern ) expression .getPattern ();
532+ List <Pattern > patterns = rp .patterns ();
533+ TypePattern secondPattern = (TypePattern ) patterns .get (1 );
534+ VariableDeclarationFragment vdf = (VariableDeclarationFragment ) secondPattern .getPatternVariable2 ();
535+
536+ assertEquals ("SingleVariableDeclaration type" , vdf .getNodeType (), ASTNode .VARIABLE_DECLARATION_FRAGMENT );
537+ assertEquals ("SimpleName" , vdf .getName ().getNodeType (), ASTNode .SIMPLE_NAME );
538+ }
539+
540+
443541}
0 commit comments