Bug 573949 - [17][switch pattern][formatter] JEP 406 changes

Change-Id: Iaf36f9fdd6ae20d5c8257cf580524810f87eb90b
Signed-off-by: Mateusz Matela <mateusz.matela@gmail.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184750
Tested-by: JDT Bot <jdt-bot@eclipse.org>
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
index ba82e20..0973f7d 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2020 IBM Corporation and others.
+ * Copyright (c) 2000, 2021 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -15941,4 +15941,48 @@
 		"}\n"
 	);
 }
+
+/**
+ * https://bugs.eclipse.org/573949 - [17][switch pattern][formatter] JEP 406 changes
+ */
+public void testBug573949() {
+	setComplianceLevel(CompilerOptions.VERSION_17);
+	String source =
+		"public class X {\n" +
+		" private static void foo(Object o) {\n" +
+		"   switch (o) {\n" +
+		"     case Integer t, String : System.out.println(\"Error should be flagged for Integer and String\");\n" +
+		"     default : System.out.println(\"Object\");\n" +
+		"   }\n" +
+		" }\n" +
+		"\n" +
+		"static void testTriangle(Shape s) {\n" +
+		"    switch (s) {\n" +
+		"        case Triangle t&&(t.calculateArea() > 100) ->\n" +
+		"            System.out.println(\"Large triangle\");\n" +
+		"        default ->\n" +
+		"            System.out.println(\"A shape, possibly a small triangle\");\n" +
+		"    }\n" +
+		"}\n" +
+		"}";
+	formatSource(source,
+		"public class X {\n" +
+		"	private static void foo(Object o) {\n" +
+		"		switch (o) {\n" +
+		"		case Integer t, String:\n" +
+		"			System.out.println(\"Error should be flagged for Integer and String\");\n" +
+		"		default:\n" +
+		"			System.out.println(\"Object\");\n" +
+		"		}\n" +
+		"	}\n" +
+		"\n" +
+		"	static void testTriangle(Shape s) {\n" +
+		"		switch (s) {\n" +
+		"		case Triangle t && (t.calculateArea() > 100) -> System.out.println(\"Large triangle\");\n" +
+		"		default -> System.out.println(\"A shape, possibly a small triangle\");\n" +
+		"		}\n" +
+		"	}\n" +
+		"}"
+	);
+}
 }
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
index 3dc05c1..f1fa89f 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2020 IBM Corporation and others.
+ * Copyright (c) 2000, 2021 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -334,9 +334,8 @@
 		return null;
 	}
 
-	@SuppressWarnings("deprecation")
 	private ASTParser createParser(int kind) {
-		ASTParser parser = ASTParser.newParser(AST.JLS16);
+		ASTParser parser = ASTParser.newParser(AST.JLS17);
 
 		if (kind == K_MODULE_INFO) {
 			parser.setSource(createDummyModuleInfoCompilationUnit());
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java
index 32b7bf7..0816536 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/SpacePreparator.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014, 2019 Mateusz Matela and others.
+ * Copyright (c) 2014, 2021 Mateusz Matela and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -57,6 +57,7 @@
 import org.eclipse.jdt.core.dom.ExpressionStatement;
 import org.eclipse.jdt.core.dom.FieldDeclaration;
 import org.eclipse.jdt.core.dom.ForStatement;
+import org.eclipse.jdt.core.dom.GuardedPattern;
 import org.eclipse.jdt.core.dom.IfStatement;
 import org.eclipse.jdt.core.dom.ImportDeclaration;
 import org.eclipse.jdt.core.dom.InfixExpression;
@@ -461,6 +462,13 @@
 	}
 
 	@Override
+	public boolean visit(GuardedPattern node) {
+		handleTokenAfter(node.getPattern(), TokenNameAND_AND, this.options.insert_space_before_logical_operator,
+				this.options.insert_space_after_logical_operator);
+		return true;
+	}
+
+	@Override
 	public boolean visit(YieldStatement node) {
 		if (node.getExpression() != null && !node.isImplicit()) {
 			this.tm.firstTokenIn(node, -1).spaceAfter();