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 215 216 217 218 219 220 221 222 223 224
|
/*
* @test /nodynamiccopyright/
* @bug 8262891 8272776
* @summary Check null handling for non-pattern switches.
*/
public class NullSwitch {
public static void main(String[] args) {
new NullSwitch().switchTest();
}
void switchTest() {
assertEquals(0, matchingSwitch1(""));
assertEquals(1, matchingSwitch1("a"));
assertEquals(100, matchingSwitch1(0));
assertEquals(-1, matchingSwitch1(null));
assertEquals(-2, matchingSwitch1(0.0));
assertEquals(0, matchingSwitch2(""));
assertEquals(1, matchingSwitch2(null));
assertEquals(1, matchingSwitch2(0.0));
assertEquals(0, matchingSwitch3(""));
assertEquals(1, matchingSwitch3("a"));
assertEquals(100, matchingSwitch3(0));
assertEquals(-1, matchingSwitch3(null));
assertEquals(-2, matchingSwitch3(0.0));
assertEquals(0, matchingSwitch5(""));
assertEquals(1, matchingSwitch5("a"));
assertEquals(100, matchingSwitch5(0));
assertEquals(-1, matchingSwitch5(null));
assertEquals(-2, matchingSwitch5(0.0));
assertEquals(0, matchingSwitch6(""));
assertEquals(1, matchingSwitch6(null));
assertEquals(1, matchingSwitch6(0.0));
assertEquals(0, matchingSwitch7(""));
assertEquals(1, matchingSwitch7("a"));
assertEquals(100, matchingSwitch7(0));
assertEquals(-1, matchingSwitch7(null));
assertEquals(-2, matchingSwitch7(0.0));
assertEquals(0, matchingSwitch8(""));
assertEquals(1, matchingSwitch8(null));
assertEquals(1, matchingSwitch8(0.0));
assertEquals(0, matchingSwitch9a(""));
assertEquals(1, matchingSwitch9a(null));
assertEquals(1, matchingSwitch9a(0.0));
assertEquals(0, matchingSwitch10a(""));
assertEquals(1, matchingSwitch10a(null));
assertEquals(1, matchingSwitch10a(0.0));
assertEquals(0, matchingSwitch9b(""));
assertEquals(2, matchingSwitch9b(null));
assertEquals(1, matchingSwitch9b(0.0));
assertEquals(0, matchingSwitch10b(""));
assertEquals(2, matchingSwitch10b(null));
assertEquals(1, matchingSwitch10b(0.0));
assertEquals(0, matchingSwitch11(""));
assertEquals(2, matchingSwitch11(null));
assertEquals(1, matchingSwitch11(0.0));
assertEquals(0, matchingSwitch12(""));
assertEquals(2, matchingSwitch12(null));
assertEquals(1, matchingSwitch12(0.0));
assertEquals(0, matchingSwitch13(""));
assertEquals(1, matchingSwitch13(0.0));
assertEquals(2, matchingSwitch13(null));
// record classes and null
assertEquals(1, matchingSwitch14(new R(null)));
assertEquals(2, matchingSwitch15(new R(null)));
}
class Super {}
class Sub extends Super {}
record R(Super s) {}
private int matchingSwitch14(R r) {
return switch(r) {
case R(Super s) -> 1;
default -> 2;
};
}
private int matchingSwitch15(R r) {
return switch(r) {
case R(Sub s) -> 1;
default -> 2;
};
}
private int matchingSwitch1(Object obj) {
return switch (obj) {
case String s -> s.length();
case Integer i -> 100 + i;
case null -> -1;
default -> -2;
};
}
private int matchingSwitch2(Object obj) {
return switch (obj) {
case String s -> 0;
case null, default -> 1;
};
}
private int matchingSwitch3(Object obj) {
return switch (obj) {
case String s -> s.length();
case Integer i -> 100 + i;
case null -> -1;
default -> -2;
};
}
private int matchingSwitch5(Object obj) {
return switch (obj) {
case String s: yield s.length();
case null: yield -1;
case Integer i: yield 100 + i;
default: yield -2;
};
}
private int matchingSwitch6(Object obj) {
return switch (obj) {
case String s: yield 0;
case null: yield 1;
default: yield 1;
};
}
private int matchingSwitch7(Object obj) {
return switch (obj) {
case String s: yield s.length();
case Integer i: yield 100 + i;
case null: yield -1;
default: yield -2;
};
}
private int matchingSwitch8(Object obj) {
return switch (obj) {
case String s: yield 0;
case null:
default: yield 1;
};
}
private int matchingSwitch9a(Object obj) {
return switch (obj) {
case String s: yield 0;
case null: yield 1;
case Object o: yield 1;
};
}
private int matchingSwitch10a(Object obj) {
switch (obj) {
case String s: return 0;
case null: return 1;
case Object o: return 1;
}
}
private int matchingSwitch9b(Object obj) {
try {
return switch (obj) {
case String s: yield 0;
case Object o: yield 1;
};
} catch (NullPointerException ex) {
return 2;
}
}
private int matchingSwitch10b(Object obj) {
try {
switch (obj) {
case String s: return 0;
case Object o: return 1;
}
} catch (NullPointerException ex) {
return 2;
}
}
private int matchingSwitch11(Object obj) {
try {
return switch (obj) {
case String s: yield 0;
default: yield 1;
};
} catch (NullPointerException ex) {
return 2;
}
}
private int matchingSwitch12(Object obj) {
try {
switch (obj) {
case String s: return 0;
default: return 1;
}
} catch (NullPointerException ex) {
return 2;
}
}
private int matchingSwitch13(Object obj) {
try {
switch (obj) {
case String s: return 0;
default: return 1;
}
} catch (NullPointerException ex) {
return 2;
}
}
static void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("Expected: " + expected + ", actual: " + actual);
}
}
}
|