changeset 57874:144bf23fdc90 amber-demo-II

Automatic merge with records-and-sealed
author mcimadamore
date Tue, 01 Oct 2019 22:35:58 +0000
parents 8631ab0c719e 4a89d252a4ef
children fb489259f857
files test/langtools/tools/javac/sealed/sealedExtensionNeg/SealedExtensionNegTest.java
diffstat 7 files changed, 55 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Tue Oct 01 22:35:58 2019 +0000
@@ -864,6 +864,10 @@
                         }
                     }
                 }
+
+                if (!isNonSealed(tree.sym) && !isFinal(tree.sym) && !isSealed(tree.sym)) {
+                    log.error(tree, Errors.NonSealedSealedOrFinalExpected);
+                }
             }
 
             boolean hasSuperTypesInSealedHierarchy = !superTypesInASealedHierarchy(tree.sym, env, false).isEmpty();
--- a/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/test/langtools/tools/javac/sealed/CheckSubtypesOfSealedTest.java	Tue Oct 01 22:35:58 2019 +0000
@@ -44,12 +44,12 @@
     static final String testSource =
             "public class SealedClasses {\n" +
             "    sealed abstract class SAC {}\n" +
-            "    abstract class SAC2 extends SAC {}\n" +
-            "    class SAC3 extends SAC {}\n" +
-            "    class SAC4 extends SAC2 {}\n" +
+            "    sealed abstract class SAC2 extends SAC {}\n" +
+            "    final class SAC3 extends SAC {}\n" +
+            "    final class SAC4 extends SAC2 {}\n" +
             "    sealed interface SI {}\n" +
-            "    interface SSI extends SI {}\n" +
-            "    class SAC5 implements SI, SSI {}\n" +
+            "    sealed interface SSI extends SI {}\n" +
+            "    final class SAC5 implements SI, SSI {}\n" +
             "    non-sealed abstract class SAC6 extends SAC {}\n" +
             "    non-sealed class SAC7 extends SAC {}\n" +
             "}";
--- a/test/langtools/tools/javac/sealed/CheckingAttributeAtRuntimeTest.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/test/langtools/tools/javac/sealed/CheckingAttributeAtRuntimeTest.java	Tue Oct 01 22:35:58 2019 +0000
@@ -39,21 +39,21 @@
 
     sealed class Sealed1 permits Sub1 {}
 
-    class Sub1 extends Sealed1 {}
+    final class Sub1 extends Sealed1 {}
 
     sealed interface SealedI1 permits Sub2 {}
 
-    class Sub2 implements SealedI1 {}
+    final class Sub2 implements SealedI1 {}
 
     sealed class Sealed2 {}
 
-    class Sub3 extends Sealed2 {}
+    final class Sub3 extends Sealed2 {}
 
-    class Sub4 extends Sealed2 {}
+    final class Sub4 extends Sealed2 {}
 
     sealed interface SealedI2 {}
 
-    class Sub5 implements SealedI2 {}
+    final class Sub5 implements SealedI2 {}
 
     non-sealed interface Int1 extends SealedI2 {}
 
--- a/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/test/langtools/tools/javac/sealed/SealedCompilationTests.java	Tue Oct 01 22:35:58 2019 +0000
@@ -105,30 +105,30 @@
         // with permits
         assertOK("class SealedTest {\n" +
                 "    sealed class SC permits C_SC { }\n" +
-                "    class C_SC extends SC { }\n" +
+                "    final class C_SC extends SC { }\n" +
                 "}");
         assertOK("class SealedTest {\n" +
                 "    sealed abstract class SAC permits C_SAC { }\n" +
-                "    class C_SAC extends SAC { }\n" +
+                "    final 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" +
+                "    final class C_SI implements SI { }\n" +
                 "    non-sealed interface I_SI extends SI { }\n" +
                 "}");
 
         // wo permits
         assertOK("class SealedTest {\n" +
                 "    sealed class SC { }\n" +
-                "    class C_SC extends SC { }\n" +
+                "    final class C_SC extends SC { }\n" +
                 "}");
         assertOK("class SealedTest {\n" +
                 "    sealed abstract class SAC { }\n" +
-                "    class C_SAC extends SAC { }\n" +
+                "    final class C_SAC extends SAC { }\n" +
                 "}");
         assertOK("class SealedTest {\n" +
                 "    sealed interface SI { }\n" +
-                "    class C_SI implements SI { }\n" +
+                "    final class C_SI implements SI { }\n" +
                 "    non-sealed interface I_SI extends SI { }\n" +
                 "}");
     }
--- a/test/langtools/tools/javac/sealed/SealedDiffConfigurationsTest.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/test/langtools/tools/javac/sealed/SealedDiffConfigurationsTest.java	Tue Oct 01 22:35:58 2019 +0000
@@ -82,8 +82,8 @@
         tb.writeJavaFiles(test,
                           "class Test {\n" +
                            "    sealed class Sealed permits Sub1, Sub2 {}\n" +
-                           "    class Sub1 extends Sealed {}\n" +
-                           "    class Sub2 extends Sealed {}\n" +
+                           "    final class Sub1 extends Sealed {}\n" +
+                           "    final class Sub2 extends Sealed {}\n" +
                            "}");
 
         Path out = base.resolve("out");
@@ -110,8 +110,8 @@
         tb.writeJavaFiles(test,
                 "class Test {\n" +
                         "    sealed class Sealed {}\n" +
-                        "    class Sub1 extends Sealed {}\n" +
-                        "    class Sub2 extends Sealed {}\n" +
+                        "    final class Sub1 extends Sealed {}\n" +
+                        "    final class Sub2 extends Sealed {}\n" +
                         "}");
 
         Path out = base.resolve("out");
@@ -244,7 +244,7 @@
         tb.writeJavaFiles(test,
                           "class Test {\n" +
                            "    sealed class Sealed permits Sub1 {}\n" +
-                           "    class Sub1 extends Sealed {}\n" +
+                           "    final class Sub1 extends Sealed {}\n" +
                            "    class Sub2 extends Sealed {}\n" +
                            "}");
 
@@ -257,6 +257,33 @@
 
         List<String> expected = List.of(
                 "Test.java:4:24: compiler.err.cant.inherit.from.sealed: Test.Sealed",
+                "Test.java:4:5: compiler.err.non.sealed.sealed.or.final.expected",
+                "2 errors");
+        if (!error.containsAll(expected)) {
+            throw new AssertionError("Expected output not found. Expected: " + expected);
+        }
+    }
+
+    @Test
+    public void testSameCompilationUnitNeg2(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path test = src.resolve("Test");
+
+        tb.writeJavaFiles(test,
+                "class Test {\n" +
+                        "    sealed class Sealed permits Sub1 {}\n" +
+                        "    class Sub1 extends Sealed {}\n" +
+                        "}");
+
+        List<String> error = new JavacTask(tb)
+                .options("-XDrawDiagnostics", "--enable-preview", "-source", "14")
+                .files(findJavaFiles(test))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(OutputKind.DIRECT);
+
+        List<String> expected = List.of(
+                "Test.java:3:5: compiler.err.non.sealed.sealed.or.final.expected",
                 "1 error");
         if (!error.containsAll(expected)) {
             throw new AssertionError("Expected output not found. Expected: " + expected);
--- a/test/langtools/tools/javac/sealed/annotations/AnnotationProcessorOnSealedTypesTest.java	Mon Sep 30 23:05:57 2019 +0000
+++ b/test/langtools/tools/javac/sealed/annotations/AnnotationProcessorOnSealedTypesTest.java	Tue Oct 01 22:35:58 2019 +0000
@@ -66,16 +66,16 @@
     sealed class Super1 {}
 
     @Final
-    class Sub1 extends Super1 {}
+    final class Sub1 extends Super1 {}
 
     @Sealed
     sealed class Super2 {}
 
     @Sealed
-    class Sub2 extends Super3 {}
+    sealed class Sub2 extends Super3 {}
 
     @Final
-    class Sub3 extends Sub2 {}
+    final class Sub3 extends Sub2 {}
 
     @Sealed
     sealed class Super3 {}
--- a/test/langtools/tools/javac/sealed/sealedExtensionNeg/SealedExtensionNegTest.java	Mon Sep 30 23:05:57 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 2018, 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 check that sealed cant be extended by non-permitted subtypes
- * @compile --enable-preview -source 14 -XDdontErrorIfSealedExtended SealedExtensionNegTest.java
- * @run main/othervm --enable-preview SealedExtensionNegTest
- */
-
-public class SealedExtensionNegTest {
-    sealed interface SealedI permits SubI1 {}
-    class SubI1 implements SealedI {}
-    class SubI2 implements SealedI {}
-    interface I1 extends SealedI {}
-
-    sealed class Sealed permits Sub1 {}
-    class Sub1 extends Sealed {}
-    class Sub2 extends Sealed {}
-
-    public static void main(String... args) {
-        Class<?> subClass1 = SubI1.class;  // ok
-
-        try {
-            Class<?> subClass2 = SubI2.class;
-            throw new AssertionError("error expected");
-        } catch (VerifyError ve) {
-            // good
-        }
-
-        try {
-            Class<?> i1 = I1.class;
-            throw new AssertionError("error expected");
-        } catch (VerifyError ve) {
-            // good
-        }
-
-        Class<?> sub1 = Sub1.class;  // ok
-
-        try {
-            Class<?> subClass2 = Sub2.class;
-            throw new AssertionError("error expected");
-        } catch (VerifyError ve) {
-            // good
-        }
-    }
-}