changeset 57064:fc8b51dff863 records-and-sealed

consolidating sealed types tests
author vromero
date Wed, 04 Sep 2019 19:13:09 -0400
parents 67ce79d04761
children d45dce240059 a7be222931ad ca43f98c088c
files src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.java test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.out test/langtools/tools/javac/sealed/NotSealedTest.java test/langtools/tools/javac/sealed/NotSealedTest.out test/langtools/tools/javac/sealed/PermitsInNoSealedClass.java test/langtools/tools/javac/sealed/PermitsInNoSealedClass.out test/langtools/tools/javac/sealed/SealedCompilationTests.java test/langtools/tools/javac/sealed/SealedTest01.java test/langtools/tools/javac/sealed/SealedTest01.out test/langtools/tools/javac/sealed/SealedTest02.java
diffstat 11 files changed, 220 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Sep 04 18:34:55 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Sep 04 19:13:09 2019 -0400
@@ -2485,7 +2485,6 @@
 
         if (ct.permitted != null && !ct.permitted.isEmpty()) {
             c.flags_field |= SEALED;
-            c.flags_field &= ~FINAL;
         }
 
         // reset and read rest of classinfo
--- a/test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.java	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke test for sealed classes
- * @compile/fail/ref=AnonymousAndLambdaCantExtendSealed.out --enable-preview -source ${jdk.version} -XDrawDiagnostics AnonymousAndLambdaCantExtendSealed.java
- */
-
-class AnonymousAndLambdaCantExtendSealed {
-    sealed interface I1 extends Runnable {
-        public static I1 i = () -> {};
-    }
-
-    sealed interface I2 extends Runnable {
-        public static void foo() { new I2() { public void run() { } }; }
-    }
-}
--- a/test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.out	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-AnonymousAndLambdaCantExtendSealed.java:9:30: compiler.err.cant.inherit.from.sealed: AnonymousAndLambdaCantExtendSealed.I1
-AnonymousAndLambdaCantExtendSealed.java:13:40: compiler.err.cant.inherit.from.sealed: AnonymousAndLambdaCantExtendSealed.I2
-2 errors
--- a/test/langtools/tools/javac/sealed/NotSealedTest.java	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke test for sealed classes
- * @compile/fail/ref=NotSealedTest.out --enable-preview -source ${jdk.version} -XDrawDiagnostics NotSealedTest.java
- */
-
-import java.lang.annotation.*;
-
-class NotSealedTest {
-    sealed class Super {}
-    sealed non-sealed class Sub extends Super {}
-
-    final sealed class Super2 {}
-
-    final non-sealed class Super3 {}
-
-    non-sealed class NoSealedSuper {}
-
-    sealed public void m() {}
-}
--- a/test/langtools/tools/javac/sealed/NotSealedTest.out	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-NotSealedTest.java:11:23: compiler.err.illegal.combination.of.modifiers: sealed, non_sealed
-NotSealedTest.java:13:18: compiler.err.illegal.combination.of.modifiers: final, sealed
-NotSealedTest.java:15:22: compiler.err.illegal.combination.of.modifiers: final, non_sealed
-NotSealedTest.java:19:24: compiler.err.mod.not.allowed.here: sealed
-NotSealedTest.java:17:16: compiler.err.non.sealed.with.no.sealed.supertype
-5 errors
--- a/test/langtools/tools/javac/sealed/PermitsInNoSealedClass.java	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke test for sealed classes
- * @compile/fail/ref=PermitsInNoSealedClass.out --enable-preview -source ${jdk.version} -XDrawDiagnostics PermitsInNoSealedClass.java
- */
-
-import java.lang.annotation.*;
-
-class PermitsInNoSealedClass {
-    class NotSealed permits Sub3 {}
-
-    class Sub3 extends NotSealed {}
-}
--- a/test/langtools/tools/javac/sealed/PermitsInNoSealedClass.out	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-PermitsInNoSealedClass.java:10:21: compiler.err.permits.in.no.sealed.class
-1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Wed Sep 04 19:13:09 2019 -0400
@@ -0,0 +1,220 @@
+/*
+ * Copyright (c) 2019, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+
+import java.io.IOException;
+import java.lang.annotation.ElementType;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.testng.ITestResult;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+import tools.javac.combo.JavacTemplateTestBase;
+
+import static java.lang.annotation.ElementType.*;
+import static java.util.stream.Collectors.toList;
+import static org.testng.Assert.assertEquals;
+
+/**
+ * SealedCompilationTests
+ *
+ * @test
+ * @summary Negative compilation tests, and positive compilation (smoke) tests for sealed types
+ * @library /lib/combo
+ * @modules jdk.compiler/com.sun.tools.javac.util
+ * @run testng SealedCompilationTests
+ */
+@Test
+public class SealedCompilationTests extends JavacTemplateTestBase {
+
+    // @@@ When sealed types become a permanent feature, we don't need these any more
+    private static String[] PREVIEW_OPTIONS = {"--enable-preview", "-source",
+            Integer.toString(Runtime.version().feature())};
+
+    // -- test framework code --
+
+    @AfterMethod
+    public void dumpTemplateIfError(ITestResult result) {
+        // Make sure offending template ends up in log file on failure
+        if (!result.isSuccess()) {
+            System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(),
+                    sourceFiles.stream().map(p -> p.snd).collect(toList()));
+        }
+    }
+
+    private String expand(String... constructs) {
+        String s = "#";
+        for (String c : constructs)
+            s = s.replace("#", c);
+        return s;
+    }
+
+    private void assertCompile(String program, Runnable postTest) {
+        reset();
+        addCompileOptions(PREVIEW_OPTIONS);
+        addSourceFile("SealedTest.java", new StringTemplate(program));
+        try {
+            compile();
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        postTest.run();
+    }
+
+    private void assertOK(String... constructs) {
+        assertCompile(expand(constructs), this::assertCompileSucceeded);
+    }
+
+    private void assertOKWithWarning(String warning, String... constructs) {
+        assertCompile(expand(constructs), () -> assertCompileSucceededWithWarning(warning));
+    }
+
+    private void assertFail(String expectedDiag, String... constructs) {
+        assertCompile(expand(constructs), () -> assertCompileFailed(expectedDiag));
+    }
+
+    // -- Actual test cases start here --
+
+    public void testSuccessExpected() {
+        // with permits
+        assertOK("class SealedTest {\n" +
+                "    sealed class SC permits C_SC { }\n" +
+                "    class C_SC extends SC { }\n" +
+                "}");
+        assertOK("class SealedTest {\n" +
+                "    sealed abstract class SAC permits C_SAC { }\n" +
+                "    class C_SAC extends SAC { }\n" +
+                "}");
+        assertOK("class SealedTest {\n" +
+                "    sealed interface SI permits C_SI, I_SI { }\n" +
+                "    class C_SI implements SI { }\n" +
+                "    interface I_SI extends SI { }\n" +
+                "}");
+
+        // wo permits
+        assertOK("class SealedTest {\n" +
+                "    sealed class SC { }\n" +
+                "    class C_SC extends SC { }\n" +
+                "}");
+        assertOK("class SealedTest {\n" +
+                "    sealed abstract class SAC { }\n" +
+                "    class C_SAC extends SAC { }\n" +
+                "}");
+        assertOK("class SealedTest {\n" +
+                "    sealed interface SI { }\n" +
+                "    class C_SI implements SI { }\n" +
+                "    interface I_SI extends SI { }\n" +
+                "}");
+    }
+
+    public void testErrorExpected() {
+        assertFail("compiler.err.cant.inherit.from.sealed","class SealedTest {\n" +
+                "    sealed class SC permits C_SC { }\n" +
+                "    class C_SC extends SC { }\n" +
+                "    class C_SC2 extends SC { }\n" +
+                "}");
+        assertFail("compiler.err.cant.inherit.from.sealed","class SealedTest {\n" +
+                "    sealed abstract class SAC permits C_SAC {}\n" +
+                "    class C_SAC extends SAC {}\n" +
+                "    class C_SAC2 extends SAC {}\n" +
+                "}");
+        assertFail("compiler.err.cant.inherit.from.sealed","class SealedTest {\n" +
+                "    sealed interface SI permits C_SI, I_SI {}\n" +
+                "    class C_SI implements SI {}\n" +
+                "    interface I_SI extends SI {}\n" +
+                "    class C_SI2 implements SI {}\n" +
+                "    interface I_SI2 extends SI {}\n" +
+                "}");
+    }
+
+    public void testValidUsesOfSealed() {
+        for (String s : List.of(
+                "class SealedTest {\n" +
+                "    String sealed;\n" +
+                "}",
+                "class SealedTest {\n" +
+                "    void test(String sealed) { }\n" +
+                "}",
+                "class SealedTest {\n" +
+                "    void test() {\n" +
+                "        String sealed = null;\n" +
+                "    }\n" +
+                "}",
+                "class sealed {}")) {
+            assertOK(s);
+        }
+    }
+
+    public void testPermitsInNoSealedClass() {
+        assertFail("compiler.err.permits.in.no.sealed.class",
+                "class SealedTest {\n" +
+                "    class NotSealed permits Sub {}\n" +
+                "    class Sub extends NotSealed {}\n" +
+                "}");
+    }
+
+    public void testWrongUseOfModifiers() {
+        assertFail("compiler.err.non.sealed.with.no.sealed.supertype",
+                "class SealedTest {\n" +
+                        "    non-sealed class NoSealedSuper {}\n" +
+                        "}");
+        assertFail("compiler.err.illegal.combination.of.modifiers",
+                "class SealedTest {\n" +
+                        "    final non-sealed class Super {}\n" +
+                        "}");
+        assertFail("compiler.err.illegal.combination.of.modifiers",
+                "class SealedTest {\n" +
+                        "    final sealed class Super {}\n" +
+                        "}");
+        assertFail("compiler.err.illegal.combination.of.modifiers",
+                "class SealedTest {\n" +
+                        "    final sealed non-sealed class Super {}\n" +
+                        "}");
+        assertFail("compiler.err.illegal.combination.of.modifiers",
+                "class SealedTest {\n" +
+                        "    sealed class Super {}\n" +
+                        "    sealed non-sealed class Sub extends Super {}\n" +
+                        "}");
+        assertFail("compiler.err.mod.not.allowed.here",
+                "class SealedTest {\n" +
+                        "    sealed public void m() {}\n" +
+                        "}");
+    }
+
+    public void testAnonymousAndLambdaCantExtendSealed() {
+        assertFail("compiler.err.cant.inherit.from.sealed",
+                "sealed interface I1 extends Runnable {\n" +
+                        "    public static I1 i = () -> {};\n" +
+                        "}");
+        assertFail("compiler.err.cant.inherit.from.sealed",
+                "sealed interface I2 extends Runnable {\n" +
+                        "    public static void foo() { new I2() { public void run() { } }; }\n" +
+                        "}");
+    }
+}
--- a/test/langtools/tools/javac/sealed/SealedTest01.java	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary smoke test for sealed classes
- * @compile/fail/ref=SealedTest01.out --enable-preview -source ${jdk.version} -XDrawDiagnostics SealedTest01.java
- */
-
-class SealedTest01 {
-    static sealed class SC permits C_SC { }
-    static sealed abstract class SAC permits C_SAC { }
-    static sealed interface SI permits C_SI, I_SI { }
-
-    static class C_SC extends SC { }
-    static class C_SAC extends SAC { }
-    static class C_SI implements SI { }
-    static interface I_SI extends SI { }
-}
-
-class SealedTest01_Other {
-    static class C_SC extends SealedTest01.SC { }
-    static class C_SAC extends SealedTest01.SAC { }
-    static class C_SI implements SealedTest01.SI { }
-    static interface I_SI extends SealedTest01.SI { }
-}
--- a/test/langtools/tools/javac/sealed/SealedTest01.out	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-SealedTest01.java:19:43: compiler.err.cant.inherit.from.sealed: SealedTest01.SC
-SealedTest01.java:20:44: compiler.err.cant.inherit.from.sealed: SealedTest01.SAC
-SealedTest01.java:21:46: compiler.err.cant.inherit.from.sealed: SealedTest01.SI
-SealedTest01.java:22:47: compiler.err.cant.inherit.from.sealed: SealedTest01.SI
-4 errors
--- a/test/langtools/tools/javac/sealed/SealedTest02.java	Wed Sep 04 18:34:55 2019 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2017, 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.
- */
-
-/*
- * @test
- * @summary smoke test for parser and treatment of 'sealed'
- * @compile SealedTest02.java
- */
-
-class SealedTest02 {
-    String sealed;
-
-    void test(String sealed) { }
-
-    void test() {
-        String sealed = null;
-    }
-}