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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/**
* @test
* @compile/fail/ref=SimpleDeconstructionPatternOld.out --release 20 -XDrawDiagnostics SimpleDeconstructionPattern.java
* @compile SimpleDeconstructionPattern.java
* @run main SimpleDeconstructionPattern
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class SimpleDeconstructionPattern {
public static void main(String... args) throws Throwable {
if (!test2(new P(42))) {
throw new IllegalStateException();
}
if (test2(new P(41))) {
throw new IllegalStateException();
}
if (!test2a(new P(42))) {
throw new IllegalStateException();
}
if (test2a(new P(41))) {
throw new IllegalStateException();
}
if (!test4(new P2(new P(42), ""))) {
throw new IllegalStateException();
}
if (test4(new P2(new P(41), ""))) {
throw new IllegalStateException();
}
if (test4(new P2(new P(42), "a"))) {
throw new IllegalStateException();
}
if (!test5(new P(42))) {
throw new IllegalStateException();
}
if (test5(new P(41))) {
throw new IllegalStateException();
}
if (!test7(new P3(""))) {
throw new IllegalStateException();
}
if (test7(new P3("a"))) {
throw new IllegalStateException();
}
if (!test7a(new P3(""))) {
throw new IllegalStateException();
}
if (test7a(new P3("a"))) {
throw new IllegalStateException();
}
if (test8(new P4(""))) {
throw new IllegalStateException();
}
if (!test8(new P4(new P3("")))) {
throw new IllegalStateException();
}
if (!test8a(new P4(new P3("")))) {
throw new IllegalStateException();
}
if (test8(new P4(new P3("a")))) {
throw new IllegalStateException();
}
if (test8a(new P4(new P3("a")))) {
throw new IllegalStateException();
}
if (!test9(new P5(new ArrayList<String>(Arrays.asList(""))))) {
throw new IllegalStateException();
}
if (test9(new P5(new LinkedList<String>(Arrays.asList(""))))) {
throw new IllegalStateException();
}
if (testA(new P6(null))) {
throw new IllegalStateException();
}
if (!testA(new P6(new P3(null)))) {
throw new IllegalStateException();
}
if (testB(new P6(null))) {
throw new IllegalStateException();
}
if (!testB(new P6(new P3(null)))) {
throw new IllegalStateException();
}
if (testC(new P6(null))) {
throw new IllegalStateException();
}
if (!testC(new P6(new P3("")))) {
throw new IllegalStateException();
}
if (!testE(new P6(new P3(null)))) {
throw new IllegalStateException();
}
if (!testF(new P7(0, (short) 0))) {
throw new IllegalStateException();
}
if (testF(new P7(0, (short) 1))) {
throw new IllegalStateException();
}
if (testGen3(new GenRecord1<>(3L, ""))) {
throw new IllegalStateException();
}
if (!testGen3(new GenRecord1<>(3, ""))) {
throw new IllegalStateException();
}
if (!testGen3(new GenRecord1<>(3, ""))) {
throw new IllegalStateException();
}
if (!I.testInInterface(new P2(new P(0), ""))) {
throw new IllegalStateException();
}
}
private static void exp(Object o) throws Throwable {
if (o instanceof P(var i)) {
System.err.println("i=" + i);
}
}
private static boolean test2(Object o) throws Throwable {
return o instanceof P(var i) && i == 42;
}
private static boolean test2a(Object o) throws Throwable {
return o instanceof P(int i) && i == 42;
}
private static boolean test4(Object o) throws Throwable {
return o instanceof P2(P(var i), var s) && i == 42 && "".equals(s);
}
private static boolean test5(Object o) throws Throwable {
return o instanceof P(var i) && i == 42;
}
private static boolean test7(Object o) throws Throwable {
return o instanceof P3(var s) && "".equals(s);
}
private static boolean test7a(Object o) throws Throwable {
return o instanceof P3(String s) && "".equals(s);
}
private static boolean test8(Object o) throws Throwable {
return o instanceof P4(P3(var s)) && "".equals(s);
}
private static boolean test8a(Object o) throws Throwable {
return o instanceof P4(P3(String s)) && "".equals(s);
}
private static boolean test9(Object o) throws Throwable {
return o instanceof P5(ArrayList<String> l) && !l.isEmpty();
}
private static boolean testA(Object o) throws Throwable {
return o instanceof P6(P3(var s));
}
private static boolean testB(Object o) throws Throwable {
return o instanceof P6(P3(String s));
}
private static boolean testC(Object o) throws Throwable {
return o instanceof P6(P3(String s)) && s.isEmpty();
}
private static boolean testE(Object o) throws Throwable {
return o instanceof P6(P3(String s)) && s == null;
}
private static boolean testF(Object o) throws Throwable {
return o instanceof P7(int i, short s) && i == s;
}
private static boolean testGen3(Object o) throws Throwable {
return o instanceof GenRecord1<?, ?>(Integer i, var s) && i.intValue() == 3 && s.length() == 0;
}
private static boolean testGen4(GenBase<Integer, String> o) throws Throwable {
return o instanceof GenRecord1<Integer, String>(var i, var s) && i.intValue() == 3 && s.length() == 0;
}
interface I {
public static boolean testInInterface(Object o) {
return o instanceof P2(P(int i), var s) && i == 0;
}
}
public record P(int i) {
}
public record P2(P p, String s) {
}
public record P3(String s) {
}
public record P4(Object o) {}
public record P5(List<String> l) {}
public record P6(P3 p) {}
public record P7(int i, short s) {}
public interface Base {}
public record BaseUse(Base b) {}
public record BaseSubclass(int i) implements Base {}
public interface GenBase<T1, T2 extends CharSequence> {}
public record GenRecord1<T1, T2 extends CharSequence> (T1 i, T2 s) implements GenBase<T1, T2> {}
}
|