|
| 1 | +package StmtExpr; |
| 2 | + |
| 3 | +import java.util.function.IntConsumer; |
| 4 | +import java.util.function.IntFunction; |
| 5 | +import java.util.function.Supplier; |
| 6 | + |
| 7 | +class StmtExpr { |
| 8 | + void test() { |
| 9 | + toString(); |
| 10 | + |
| 11 | + // Method call with `void` return type is not a ValueDiscardingExpr |
| 12 | + System.out.println("test"); |
| 13 | + |
| 14 | + // LocalVariableDeclarationStatement with init is not a ValueDiscardingExpr |
| 15 | + String s = toString(); |
| 16 | + |
| 17 | + int i; |
| 18 | + i = 0; |
| 19 | + i++; |
| 20 | + ++i; |
| 21 | + i--; |
| 22 | + --i; |
| 23 | + |
| 24 | + new Object(); |
| 25 | + // Language specification does not permit ArrayCreationExpression at locations where its |
| 26 | + // value would be discard, but the value of a method access on it can be discarded |
| 27 | + new int[] {}.clone(); |
| 28 | + |
| 29 | + // for statement init can discard value |
| 30 | + for (System.out.append("init");;) { |
| 31 | + break; |
| 32 | + } |
| 33 | + |
| 34 | + // for statement update discards value |
| 35 | + for (;; System.out.append("update")) { |
| 36 | + break; |
| 37 | + } |
| 38 | + |
| 39 | + // Method call with `void` return type is not a ValueDiscardingExpr |
| 40 | + for (;; System.out.println("update")) { |
| 41 | + break; |
| 42 | + } |
| 43 | + |
| 44 | + // variable declaration and condition are not ValueDiscardingExpr |
| 45 | + for (int i1 = 0; i1 < 10;) { } |
| 46 | + for (int i1, i2 = 0; i2 < 10;) { } |
| 47 | + for (;;) { |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + // Not a ValueDiscardingExpr |
| 52 | + for (int i2 : new int[] {1}) { } |
| 53 | + |
| 54 | + switch(1) { |
| 55 | + default -> toString(); // ValueDiscardingExpr |
| 56 | + } |
| 57 | + |
| 58 | + switch(1) { |
| 59 | + // Method call with `void` return type is not a ValueDiscardingExpr |
| 60 | + default -> System.out.println(); |
| 61 | + } |
| 62 | + |
| 63 | + String s2 = switch(1) { |
| 64 | + // Expression in SwitchExpression case rule is not a ValueDiscardingExpr |
| 65 | + default -> toString(); |
| 66 | + }; |
| 67 | + |
| 68 | + // Expression in lambda with non-void return type is not a ValueDiscardingExpr |
| 69 | + Supplier<Object> supplier1 = () -> toString(); |
| 70 | + Supplier<Object> supplier2 = () -> { |
| 71 | + return toString(); |
| 72 | + }; |
| 73 | + // Expression in lambda with void return type is ValueDiscardingExpr |
| 74 | + Runnable r1 = () -> toString(); |
| 75 | + Runnable r2 = () -> { |
| 76 | + toString(); |
| 77 | + }; |
| 78 | + // Lambda with method call with `void` return type is not a ValueDiscardingExpr |
| 79 | + Runnable r3 = () -> System.out.println(); |
| 80 | + Runnable r4 = () -> { |
| 81 | + System.out.println(); |
| 82 | + }; |
| 83 | + |
| 84 | + // Method reference with non-void return type has no ValueDiscardingExpr |
| 85 | + Supplier<Object> supplier3 = StmtExpr::new; |
| 86 | + IntFunction<Object> f = String[]::new; |
| 87 | + Supplier<Object> supplier4 = this::toString; |
| 88 | + |
| 89 | + // Method reference with void return type has ValueDiscardingExpr in implicit method body |
| 90 | + Runnable r5 = StmtExpr::new; |
| 91 | + Runnable r6 = this::toString; |
| 92 | + // Interestingly a method reference expression with function type (int)->void allows usage of |
| 93 | + // array creation expressions, even though a regular StatementExpression would not allow it, |
| 94 | + // for example the ExpressionStatement `new String[1];` is not allowed by the JLS |
| 95 | + IntConsumer c = String[]::new; |
| 96 | + |
| 97 | + // Method reference referring to method with `void` return type is not a ValueDiscardingExpr |
| 98 | + Runnable r7 = System.out::println; |
| 99 | + } |
| 100 | +} |
0 commit comments