changeset 56919:180ed622d987 records-and-sealed

dont allow anonymous classes or lambdas to extend or implement sealed classes
author vromero
date Mon, 08 Jul 2019 20:36:28 -0400
parents e7455c3923d9
children 17909be32d4f
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.java test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.out
diffstat 4 files changed, 27 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jul 08 19:09:33 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jul 08 20:36:28 2019 -0400
@@ -2672,6 +2672,10 @@
             Type currentTarget = targetInfo.target;
             Type lambdaType = targetInfo.descriptor;
 
+            if (((ClassSymbol)currentTarget.tsym).isSealed()) {
+                log.error(that, Errors.CantInheritFromSealed(currentTarget.tsym));
+            }
+
             if (currentTarget.isErroneous()) {
                 result = that.type = currentTarget;
                 return;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Mon Jul 08 19:09:33 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Mon Jul 08 20:36:28 2019 -0400
@@ -1091,7 +1091,11 @@
                     boolean isSealed = sealedParentPair.fst.tsym.isSealed();
                     if (areInSameCompilationUnit) {
                         if (sealedParentPair.fst.tsym.isSealed() && !((ClassType)sealedParentPair.fst.tsym.type).isPermittedExplicit) {
-                            sealedParentPair.fst.permitted = sealedParentPair.fst.permitted.prepend(tree.sym.type);
+                            if (tree.sym.isAnonymous()) {
+                                log.error(sealedParentPair.snd, Errors.CantInheritFromSealed(sealedParentPair.fst.tsym));
+                            } else {
+                                sealedParentPair.fst.permitted = sealedParentPair.fst.permitted.prepend(tree.sym.type);
+                            }
                         } else if (!dontErrorIfSealedExtended) {
                             log.error(sealedParentPair.snd, Errors.CantInheritFromSealed(sealedParentPair.fst.tsym));
                         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.java	Mon Jul 08 20:36:28 2019 -0400
@@ -0,0 +1,15 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary smoke test for sealed classes
+ * @compile/fail/ref=AnonymousAndLambdaCantExtendSealed.out -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() { } }; }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/sealed/AnonymousAndLambdaCantExtendSealed.out	Mon Jul 08 20:36:28 2019 -0400
@@ -0,0 +1,3 @@
+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