1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8298184
* @compile GenericRecordDeconstructionPattern.java
* @run main GenericRecordDeconstructionPattern
*/
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
public class GenericRecordDeconstructionPattern {
public static void main(String... args) throws Throwable {
new GenericRecordDeconstructionPattern().run();
}
void run() {
runTest(this::runIf);
runTest(this::runSwitch);
runTest(this::runSwitchExpression);
runTest(this::runSwitchInference1);
runTest(this::runSwitchInference2);
runTest(this::runSwitchInference3);
runTest(this::runSwitchInference4);
testInference3();
assertEquals(1, runIfSuperBound(new Box<>(new StringBuilder())));
assertEquals(1, runIfSuperBound(new Box<>(0)));
}
void runTest(Function<Box<String>, Integer> test) {
Box<String> b = new Box<>(null);
assertEquals(1, test.apply(b));
}
int runIf(Box<String> b) {
if (b instanceof Box<String>(String s)) return 1;
return -1;
}
int runSwitch(Box<String> b) {
switch (b) {
case Box<String>(String s): return 1;
default: return -1;
}
}
int runSwitchExpression(Box<String> b) {
return switch (b) {
case Box<String>(String s) -> 1;
default -> -1;
};
}
int runSwitchInference1(I<String> b) {
switch (b) {
case Box(String s): return s == null ? 1 : s.length();
default: return -1;
}
}
int runSwitchInference2(I<String> b) {
switch (b) {
case Box(var s): return s == null ? 1 : s.length();
default: return -1;
}
}
int runSwitchInference3(I<String> b) {
return b instanceof Box(var s) ? s == null ? 1 : s.length()
: -1;
}
<Z extends I<String>> int runSwitchInference4(Z b) {
return b instanceof Box(var s) ? s == null ? 1 : s.length()
: -1;
}
<B extends CharSequence & Runnable, Z extends I<B>> int runSwitchInference5(Z b) {
return b instanceof Box(var s) ? s == null ? 1 : s.length()
: -1;
}
void testInference3() {
I<I<String>> b = new Box<>(new Box<>(null));
assertEquals(1, runSwitchInferenceNested(b));
}
int runSwitchInferenceNested(I<I<String>> b) {
switch (b) {
case Box(Box(var s)): return s == null ? 1 : s.length();
default: return -1;
}
}
int runIfSuperBound(I<? super String> b) {
if (b instanceof Box(var v)) return 1;
return -1;
}
sealed interface I<T> {}
record Box<V>(V v) implements I<V> {
}
void assertEquals(Object expected, Object actual) {
if (!Objects.equals(expected, actual)) {
throw new AssertionError("Expected: " + expected + "," +
"got: " + actual);
}
}
void fail(String message) {
throw new AssertionError(message);
}
public static class TestPatternFailed extends AssertionError {
public TestPatternFailed(String message) {
super(message);
}
}
}
|