| /* | |
| * Copyright 2025 Google Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * https://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package switchpatterns; | |
| import jsinterop.annotations.JsEnum; | |
| public class SwitchPatterns { | |
| void testPatternsWithGuards() { | |
| Object o = "Hello"; | |
| switch (o) { | |
| case String s when s.length() == 1 -> {} | |
| case String s -> {} | |
| default -> {} | |
| } | |
| // Integer switch with expressions that does not allow nulls. | |
| Integer i = null; | |
| switch (i) { | |
| case 23, 32 -> {} | |
| case Integer j when j > 2 -> {} | |
| case Number n -> {} | |
| } | |
| // Integer switch with expressions that does allow nulls. | |
| switch (i) { | |
| case 23, 32 -> {} | |
| case Integer j when j > 2 -> {} | |
| case null -> {} | |
| default -> {} | |
| } | |
| // Guard of boxed Boolean type. | |
| Boolean condition = true; | |
| switch (o) { | |
| case String s when condition -> {} | |
| default -> {} | |
| } | |
| } | |
| void testRecordPatterns() { | |
| // Switch with record patterns. | |
| record R(Object o, String s) {} | |
| Object r = null; | |
| switch (r) { | |
| case R(R(String s1, var s2), var s3) when s1.equals("Hello") -> s2.length(); | |
| case R(var c1, var c2) -> {} | |
| default -> {} | |
| } | |
| } | |
| static void testGenericRecordPatterns() { | |
| // Switch with record patterns on generic records, where the components need erasure casts. | |
| record GenericRecord<T>(T value) {} | |
| GenericRecord<String> genericRecord = new GenericRecord<>("Hello"); | |
| switch (genericRecord) { | |
| case GenericRecord<String>(String s) when s.isEmpty() -> {} | |
| case GenericRecord<String>(var s) -> s.length(); | |
| } | |
| } | |
| void testReassignedBindings() { | |
| Object o = "Hello"; | |
| switch (o) { | |
| case String s -> s = s + "!"; | |
| default -> {} | |
| } | |
| String result = | |
| switch (o) { | |
| case String s -> { | |
| s = s + "?"; | |
| yield s; | |
| } | |
| default -> ""; | |
| }; | |
| } | |
| sealed interface Sealed { | |
| final class A implements Sealed {} | |
| final class B implements Sealed {} | |
| default void testSwitchOnThis() { | |
| int i = | |
| switch (this) { | |
| case Sealed.A a -> 1; | |
| case Sealed.B b -> 2; | |
| }; | |
| } | |
| } | |
| Sealed getSealed() { | |
| return null; | |
| } | |
| void testExhaustivePatternsWithNoDefault() { | |
| int i = | |
| switch (getSealed()) { | |
| case Sealed.A a -> 1; | |
| case Sealed.B b -> 2; | |
| }; | |
| } | |
| enum Color { | |
| RED, | |
| BLUE, | |
| GREEN | |
| } | |
| @JsEnum | |
| enum Digit { | |
| ZERO, | |
| ONE, | |
| TWO, | |
| THREE | |
| } | |
| void testQualifiedEnumConstants() { | |
| Object o = Color.RED; | |
| switch (o) { | |
| case Color.RED -> {} | |
| case String s -> {} | |
| default -> {} | |
| } | |
| // Switch with only enum constants and no patterns. | |
| switch (o) { | |
| case Color.RED, Color.BLUE -> {} | |
| default -> {} | |
| } | |
| } | |
| void testPatternsWithNull() { | |
| Color color = null; | |
| // Enum switch. Case null that is not a default case. | |
| switch (color) { | |
| case RED: | |
| fail(); | |
| break; | |
| case null: | |
| break; | |
| default: | |
| fail(); | |
| } | |
| // Enum switch. Case null, default case. | |
| switch (color) { | |
| case RED: | |
| fail(); | |
| break; | |
| case null, default: | |
| { | |
| int variable = 0; | |
| break; | |
| } | |
| } | |
| // Enum switch. Expression with potential side effects. | |
| switch (Color.valueOf("BLUE")) { | |
| case RED: | |
| fail(); | |
| break; | |
| case null, default: | |
| break; | |
| } | |
| // JsEnum switch. Case null that is not a default case. | |
| Digit digit = Digit.ZERO; | |
| switch (digit) { | |
| case ZERO: | |
| fail(); | |
| break; | |
| case null: | |
| break; | |
| default: | |
| fail(); | |
| } | |
| // String switch. Case null that is not a default case. | |
| String s = "Hi"; | |
| switch (s) { | |
| case "Hi": | |
| fail(); | |
| break; | |
| case null: | |
| break; | |
| default: | |
| fail(); | |
| } | |
| // String switch. Case null, default case. | |
| switch (s) { | |
| case "Hi": | |
| fail(); | |
| break; | |
| case null, default: | |
| break; | |
| } | |
| } | |
| void testDefaultNotLast() { | |
| Integer i = 2; | |
| // Default case that is not the last case; the cases after it still need to be tested. | |
| switch (i) { | |
| case null: | |
| fail(); | |
| break; | |
| default: | |
| fail(); | |
| case 2: | |
| break; | |
| } | |
| Color color = Color.BLUE; | |
| int unused = | |
| switch (color) { | |
| case null -> 0; | |
| default -> 1; | |
| case BLUE -> 2; | |
| }; | |
| } | |
| void testCaseNullWithFallthrough() { | |
| Integer i = 2; | |
| switch (i) { | |
| case null: | |
| fail(); | |
| case 1: | |
| fail(); | |
| break; | |
| default: | |
| } | |
| } | |
| static class Holder<T> { | |
| T value; | |
| Holder(T value) { | |
| this.value = value; | |
| } | |
| } | |
| void testSwitchOnGenericField() { | |
| // The selector needs an erasure cast and must be evaluated only once, before any guard runs. | |
| Holder<Integer> integerHolder = new Holder<>(1); | |
| switch (integerHolder.value) { | |
| case null -> {} | |
| case Integer i when guard() -> {} | |
| default -> {} | |
| } | |
| Holder<Color> colorHolder = new Holder<>(Color.RED); | |
| switch (colorHolder.value) { | |
| case null -> {} | |
| case Color.RED -> {} | |
| default -> {} | |
| } | |
| } | |
| static boolean guard() { | |
| return false; | |
| } | |
| static void fail() {} | |
| } |