From 0ee3117d6ef2cdad88c2304a917e88311b8a3b7f Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 11 Jan 2023 17:43:08 +0100 Subject: [PATCH] =?UTF-8?q?Test=20f=C3=BCr=20Java17=20Grammatik-Konstrukte?= =?UTF-8?q?=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/parser/Java17Rules.jav | 631 +++++++++++++++++++++++++++ 1 file changed, 631 insertions(+) create mode 100644 src/test/java/parser/Java17Rules.jav diff --git a/src/test/java/parser/Java17Rules.jav b/src/test/java/parser/Java17Rules.jav new file mode 100644 index 00000000..b824ed49 --- /dev/null +++ b/src/test/java/parser/Java17Rules.jav @@ -0,0 +1,631 @@ +/* Test Case für antlr4-Grammatik für Java 17 +Typen wurden aus der originalen Datei entfernt +Quelle: https://github.com/antlr/grammars-v4/blob/master/java/java/examples/AllInOne17.java */ + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; + +@interface Dummy { +} + +@interface Dummy2 { +} + +@Target({ElementType.TYPE, ElementType.TYPE_USE}) +@interface Dummy3 { +} + +/** + * https://openjdk.java.net/jeps/361 + */ +class SwitchExpressions { + + final static private C = 10; + + static class SC1 { + final static C = 100; + } + + enum E1 { + ONE; + } + + fn1(n) { + final var k = 4; + var r = switch (n) { + case 1, 2, 3 + 3, k, C, SC1.C -> 3 + SC1.C; + case 20 -> 3 + 4 + C - k; + case 21 -> { + int ff = 222; + yield ff; + } + case 22 -> { + yield 33 + 3; + } + case 99 -> { + throw new RuntimeException(""); + } + default -> 0; + }; + return r; + } + + fn2(s) { + return switch (s) { + //case null -> "n"; + case "a" -> ""; + case "b", "c" -> "a"; + default -> "o"; + }; + } + + fn3(final t) { + return switch (t) { + case 1 -> 2; + default -> t; + }; + } + + fn4() { + + fn1(switch (1) { + case 1 -> 0; + case 2 -> 2; + default -> 1; + }); + } + + fn5() { + var e = E1.ONE; + return switch (e) { + case ONE -> 0; + //default -> 1; + }; + } + + fn6() { + switch (1) { + case 1 -> { + + } + } + } + + fn7() { + switch (1) { + case 1 -> { + } + case 2 -> { + } + } + } + + fn8() { + var i = 1; + switch (1) { + + } + var f = 2; + switch (2) { + case 2 -> { + f = 3; + } + } + } + + fn9(s) { + switch (s) { + case "" -> { + } + default -> { + } + } + } + + fn10() { + var i = switch (1) { + case 1 -> switch (2) { + case 2 -> 0; + default -> 2; + }; + default -> 2; + }; + } + + fn11() { + switch (1) { + case 1 -> throw new RuntimeException(""); + } + } + + fn12() { + var v = 1; + var n = switch (1) { + case 1: + var g = 1; + System.out.println(); + yield v; + default: + yield 3; + }; + return n; + } + + fn13() { + int n; + switch (1) { + case 1 -> n = 1; + } + } + + fn14() { + switch (1) { + default -> { + } + } + + var n = 1; + var m = switch (n) { + case 1 -> 2; + case 2 -> 2; + default -> 1; + }; + + var m = switch (n) { + case 2: + yield 2; + default: + yield 3; + }; + + + } +} + +/** + * https://openjdk.java.net/jeps/394 + */ +class PatternMatching4instanceof { + + fn1(n) { + if (n instanceof Long var) { + var v = var; + } else if (n instanceof open) { + var v = open; + } else if (n instanceof Byte) { + // + } else { + throw new RuntimeException(""); + } + + if (!(n instanceof l)) ; + + if (n instanceof final @Dummy @Dummy2 l && l.byteValue() == 1 + || n instanceof @Dummy @Dummy2 final b && b.intValue() == 1) ; + + if (n instanceof Long) ; + if (n instanceof ox) ; + if (n instanceof l) ; + if (n instanceof final l) ; + if (n instanceof @Dummy l) ; + if (n instanceof @Dummy @Dummy2 l) ; + if (n instanceof final @Dummy l) ; + if (n instanceof final @Dummy @Dummy2 l) ; + if (n instanceof @Dummy final Long l) ; + if (n instanceof @Dummy @Dummy2 final l) ; + } +} + +/** + * https://openjdk.java.net/jeps/406 + +class PatternMatching4switchExp { + + f(i) { + } + + f1(obj) { + switch (obj) { + case null -> f(0); + case s -> f(1); + case a -> f(2); + default -> f(-1); + } + } + + f2(obj) { + switch (obj) { + case null -> f(0); + case l -> f(1); + case i -> f(1); + case a -> f(2); + default -> f(-1); + } + } + + f3(o) { + switch (o) { + case null: + case l: + f(0); + break; + default: + break; + } + } + + enum E1 { + var; + } + + f4() { + var var = E1.var; + switch (var) { + case var: + return; + default: + break; + } + + switch (var) { + case var -> { + } + default -> { + } + } + } + + f5(n) { + return switch (n) { + case l && l.intValue() == 1 && l.byteValue() == 1 -> l.byteValue(); + case var -> var.byteValue(); + case i -> i.byteValue(); + default -> throw new RuntimeException(""); + }; + } + + f6(obj) { + b = true; + return switch (obj) { + case var && b -> t -> var; + default -> t -> "Default string"; + }; + } + + dummy() { + return 0; + } + + f7(obj) { + b = true; + b2 = true; + b3 = true; + return switch (obj) { + case (((s) && (b && b2)) && s.length() > 0 && dummy() == 1) -> t -> s; + case (((i && b && b2) && (b && b2)) && b3 && (b && b2)) -> t -> ""; + case (((i && b && b2) && (b && b2)) && b3 && (b && b2 && !b3)) -> { + yield t -> ""; + } + case final l && (b ? b2 : b3) -> { + yield t -> ""; + } + default -> t -> "Default string"; + }; + } + + f8(o, i) { + switch (i) { + case 1, 2: + case 3, 4: { + } + } + + switch (o) { + case b: { + } + default: { + } + } + + var f = switch (o) { + case final l: { + yield switch (o) { + case b -> 1; + default -> 0; + }; + } + default: { + yield 1; + } + }; + } +}*/ + +/** + * https://openjdk.java.net/jeps/395 + */ +class Records { + + interface I1 { + + } + + final record R1(@Dummy2 @Dummy x) { + + R1(x) { + this.x = x; + } + + enum E { + ONE; + + record ER() { + + } + } + + class C { + record CR() { + + } + } + + interface I { + record IR() { + + } + } + + final static private record R() implements I1 { + } + + final static protected record R2() implements I1 { + } + + final static public record R3() implements I1 { + } + + final static record R4() implements I1 { + } + } + + record R2() { + public interface TM1 { + AR() { + + } + } + } + + record R3(x, y) { + } + + record R4(x, y) implements I1 { + + } + + fn1() { + final record Pt(x, y) implements I1, R1.I { + fn(T t) { + } + + f() { + } + + //final int x; implicitly defined + + Pt(x, y) { + this.x = x; + this.y = y; + } + + //private int c = 1; not allowed + private final static C = 1; //allowed + + static class C { + + } + } + + p = new Pt<>(1, 2); + p.fn(1L); + + + } + +} + +/** + * https://openjdk.java.net/jeps/409 + */ +class SealedClasses { + + interface I1 { + } + + class C0 { + } + + sealed class SC1 extends C0 implements I1 permits FC1, FC2 { + + } + + sealed class SC2 { + f() { + var non = 1; + var sealed = 2; + var ns = non - sealed; + var permits = 1; + var record = 1; + } + } + + final class FC1 extends SC1 { + + } + + final class FC2 extends SC1 { + + } + + non-sealed class NSC1 extends SC2 { + + } + + class C1 extends NSC1 { + + } +} + +class Ids { + class oo { + + class opens { + + enum E { + provides; + } + + class provides { + + f() { + + var b1 = new opens<>().new provides<>() { + }; + var b2 = new opens().new provides() { + }; + } + + g() { + var e = E.provides; + switch (e) { + case provides: + break; + } + } + + var() { + return null; + } + + get() { + return null; + } + + class with { + + } + + static class SS { + interface Sup { + get(); + } + } + + h() { + var o = get().var(); + + var s = @Issue1897.Dum1 provides.with::new; + } + + class R { + + f() { + } + } + } + + + } + } + + static class opens { + enum requires { + opens; + + } + + public static with(s) { + + } + + interface with { + default f() { + } + } + + class exports implements with { + g() { + with.super.f(); + } + } + + @interface to { + + } + + class module { + public static with(String s) { + try { + + } catch (var) { + + } + } + } + + record provides(to) { + + f() { + + opens o = new opens(); + var b = (opens, with) -> 1L; + var c = opens.module::with; + } + } + } + +} + +class Yield { + + f(o) { + + final var yield = 1; + return switch (o) { + case l -> { + //var yield = 1; + yield yield; + } + default -> { + yield yield; + } + }; + } + + yield(yield){ + return yield; + } +} + + +class IF_PERMITS { + final class T1 implements I1 { + + } + + final class T2 implements I1 { + + } + + interface I2 { + } + + sealed interface I1 extends I2 permits T1, T2 { + + } + +} \ No newline at end of file