/* * Copyright (c) 2017, 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.
*/
int typeTestPatternSwitchTest(Object o) { switch (o) { case String s: return Integer.parseInt(s.toString()); case CharSequence s: return 2 * Integer.parseInt(s.toString()); case Integer i: return i; case Object x: return -1;
}
}
int typeTestPatternSwitchExpressionTest(Object o) { returnswitch (o) { case String s -> Integer.parseInt(s.toString()); case @Deprecated CharSequence s -> { yield 2 * Integer.parseInt(s.toString()); } casefinal Integer i -> i; case Object x -> -1;
};
}
int testBooleanSwitchExpression(Object o) {
Object x; if (switch (o) { default -> false;
}) { return -3;
} elseif (switch (o) { case String s -> (x = s) != null; default -> false;
}) { return Integer.parseInt(x.toString());
} elseif (switch (o) { case CharSequence s -> {
x = s;
yield true;
} default -> false;
}) { return 2 * Integer.parseInt(x.toString());
} return typeTestPatternSwitchTest(o);
}
int testStringWithConstant(String str) { switch (str) { case"A": return 1; casenull: return -1; case String s: return s.length();
}
}
int testStringWithConstantExpression(String str) { returnswitch (str) { case"A" -> 1; casenull -> -1; case String s -> s.length();
};
}
String testEnumExpression1(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case E x -> String.valueOf(x); casenull -> "null";
};
}
String testEnumExpression2(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case E x -> String.valueOf(x); casenull -> "null";
};
}
String testEnumWithGuards1(E e) { switch (e) { case A: return"a"; case B: return"b"; case C: return String.valueOf(e); case E x when "A".equals(x.name()): return"broken"; case E x: return String.valueOf(x); casenull: return"null";
}
}
String testEnumWithGuardsExpression1(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case C -> String.valueOf(e); case E x when "A".equals(x.name()) -> "broken"; case E x -> String.valueOf(x); casenull -> "null";
};
}
String testEnumWithGuards2(E e) { switch (e) { case A: return"a"; case B: return"b"; case E x when "C".equals(x.name()): return"C"; case E x: return e == E.C ? "broken" : String.valueOf(x); casenull: return"null";
}
}
String testEnumWithGuardsExpression2(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case E x when "C".equals(x.name()) -> "C"; case E x -> e == E.C ? "broken" : String.valueOf(x); casenull -> "null";
};
}
String testEnumWithGuards3(E e) { switch (e) { case A: return"a"; case B: return"b"; case Object x when "C".equals(x.toString()): return"C"; case E x: return e == E.C ? "broken" : String.valueOf(x); casenull: return"null";
}
}
String testEnumWithGuardsExpression3(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case Object x when "C".equals(x.toString()) -> "C"; case E x -> e == E.C ? "broken" : String.valueOf(x); casenull -> "null";
};
}
String testEnumWithGuards4(E e) { switch (e) { case A: return"a"; case B: return"b"; case Runnable x when "C".equals(x.toString()): return"C"; case E x: return e == E.C ? "broken" : String.valueOf(x); casenull: return"null";
}
}
String testEnumWithGuardsExpression4(E e) { returnswitch (e) { case A -> "a"; case B -> "b"; case Runnable x when "C".equals(x.toString()) -> "C"; case E x -> e == E.C ? "broken" : String.valueOf(x); casenull -> "null";
};
}
String testStringWithGuards1(E e) { switch (e != null ? e.name() : null) { case"A": return"a"; case Switches.ConstantClassClash: return"b"; case String x when "C".equals(x): return"C"; case String x: return"C".equals(x) ? "broken" : String.valueOf(x); casenull: return"null";
}
}
String testStringWithGuardsExpression1(E e) { returnswitch (e != null ? e.name() : null) { case"A" -> "a"; case ConstantClassClash -> "b"; case String x when "C".equals(x) -> "C"; case String x -> e == E.C ? "broken" : String.valueOf(x); casenull -> "null";
};
}
String testIntegerWithGuards1(E e) { switch (e != null ? e.ordinal() : null) { case 0: return"a"; case 1: return"b"; case Integer x when x.equals(2): return"C"; case Integer x: return Objects.equals(x, 2) ? "broken" : String.valueOf(x); casenull: return"null";
}
}
String testIntegerWithGuardsExpression1(E e) { returnswitch (e != null ? e.ordinal() : null) { case 0 -> "a"; case 1 -> "b"; case Integer x when x.equals(2) -> "C"; case Integer x -> Objects.equals(x, 2) ? "broken" : String.valueOf(x); casenull -> "null";
};
}
Integer testFallThroughStatement(Integer i) { int r = 0;
switch (i) { case Integer o when o != null:
r = 1; default:
r = 2;
}
return r;
}
Integer testFallThroughExpression(Integer i) { int r = switch (i) { case Integer o when o != null:
r = 1; default:
r = 2;
yield r;
};
return r;
}
Integer testFallThrough2Statement(Integer i) { int r = 0;
switch (i) { case Integer o when o != null:
r = 1; casenull, default:
r = 2;
}
return r;
}
Integer testFallThrough2Expression(Integer i) { int r = switch (i) { case Integer o when o != null:
r = 1; casenull, default:
r = 2;
yield r;
};
return r;
}
void npeTestStatement(I i) { switch (i) { case A a -> {} case B b -> {}
}
}
void npeTestExpression(I i) { int j = switch (i) { case A a -> 0; case B b -> 1;
};
}
void exhaustiveStatementSane(Object o) { try { switch (o) { case Object obj:; //no break intentionally - should not fall through to any possible default
} if (o == null) { thrownew AssertionError();
}
} catch (NullPointerException ex) { if (o != null) { thrownew AssertionError();
}
} switch (o) { case Object obj: int i; casenull:; //no break intentionally - should not fall through to any possible default
}
}
void exhaustiveStatementSane2(I i) { switch (i) { case A a: break; case B b:; //no break intentionally - should not fall through to any possible default casenull:;
} switch (i) { case A a -> {} case B b -> {} casenull -> {}
}
}
void emptyFallThrough(Object o) { switch (o) { case Integer i: case String s: case Object obj:
}
}
void testSimpleSwitch() {
Object o = ""; int res; switch (o) { default -> res = 1;
};
assertEquals(1, res);
}
void testSimpleSwitchExpression() {
Object o = ""; int res = switch (o) { default -> 1;
};
assertEquals(1, res);
}
//verify that for cases like: //case ConstantClassClash -> //ConstantClassClash is interpreted as a field, not as a class privatestaticfinal String ConstantClassClash = "B"; privatestaticclass ConstantClassClash {}
sealed interface I {} finalclass A implements I {} finalclass B implements I {}
void assertEquals(int expected, int actual) { if (expected != actual) { thrownew AssertionError("Expected: " + expected + ", but got: " + actual);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.