This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 01a8385f43 GROOVY-11292, GROOVY-11750, GROOVY-11768: non-sealed super 
class
01a8385f43 is described below

commit 01a8385f43db6bbf9cf84ccef49b96029168c37e
Author: Eric Milles <[email protected]>
AuthorDate: Fri Oct 3 11:20:24 2025 -0500

    GROOVY-11292, GROOVY-11750, GROOVY-11768: non-sealed super class
    
    4_0_X backport
---
 .../groovy/ast/decompiled/DecompiledClassNode.java |  4 +-
 .../groovy/classgen/ClassCompletionVerifier.java   | 70 +++++++++++-----------
 .../org/codehaus/groovy/classgen/Verifier.java     | 26 +++-----
 src/test/groovy/bugs/Groovy11292.groovy            | 57 +++++++++++++++---
 4 files changed, 93 insertions(+), 64 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java 
b/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
index e919d090db..7a9b103e45 100644
--- a/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
@@ -96,7 +96,7 @@ public class DecompiledClassNode extends ClassNode {
         List<AnnotationStub> annotations = classData.annotations;
         if (annotations != null) {
             for (AnnotationStub stub : annotations) {
-                if (stub.className.equals("groovy.transform.Sealed")) {
+                if ("groovy.transform.Sealed".equals(stub.className)) {
                     return true;
                 }
             }
@@ -104,7 +104,7 @@ public class DecompiledClassNode extends ClassNode {
         // check Java "sealed"
         try {
             return ReflectionUtils.isSealed(getTypeClass());
-        } catch (NoClassDefFoundError ignored) {
+        } catch (AssertionError | LinkageError ignore) {
         }
         return false;
     }
diff --git 
a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java 
b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
index bbdd5a7add..a6745708d3 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
@@ -315,53 +315,53 @@ public class ClassCompletionVerifier extends 
ClassCodeVisitorSupport {
     }
 
     private void checkClassForExtendingFinalOrSealed(final ClassNode cn) {
-        boolean sealed = 
Boolean.TRUE.equals(cn.getNodeMetaData(groovy.transform.Sealed.class));
-        if (sealed && cn.getPermittedSubclasses().isEmpty()) {
-            addError("Sealed " + getDescription(cn) + " has no explicit or 
implicit permitted subclasses.", cn);
-            return;
-        }
         boolean isFinal = isFinal(cn.getModifiers());
-        if (sealed && isFinal) {
-            addError("The " + getDescription(cn) + " cannot be both final and 
sealed.", cn);
-            return;
+        boolean isSealed = 
Boolean.TRUE.equals(cn.getNodeMetaData(groovy.transform.Sealed.class));
+        boolean isNonSealed = cn.getAnnotations().stream().anyMatch(an -> 
an.getClassNode().getName().equals("groovy.transform.NonSealed")); // 
GROOVY-11768
+
+        ClassNode sc = cn.getSuperClass();
+        if (sc != null && isFinal(sc.getModifiers())) {
+            addError("You are not allowed to extend the final " + 
getDescription(sc) + ".", cn);
         }
-        boolean explicitNonSealed = nonSealed(cn);
-        ClassNode superCN = cn.getSuperClass();
-        boolean sealedSuper = superCN != null && superCN.isSealed();
-        boolean sealedInterface = 
Arrays.stream(cn.getInterfaces()).anyMatch(ClassNode::isSealed);
-        boolean nonSealedSuper = superCN != null && nonSealed(superCN);
-        boolean nonSealedInterface = 
Arrays.stream(cn.getInterfaces()).anyMatch(this::nonSealed);
 
-        if (explicitNonSealed && !(sealedSuper || sealedInterface || 
nonSealedSuper || nonSealedInterface)) {
-            addError("The " + getDescription(cn) + " cannot be non-sealed as 
it has no sealed parent.", cn);
-            return;
+        if (isFinal && isNonSealed) {
+            addError("The " + getDescription(cn) + " cannot be both final and 
non-sealed.", cn);
         }
-        if (sealedSuper || sealedInterface) {
-            if (sealed && explicitNonSealed) {
-                addError("The " + getDescription(cn) + " cannot be both sealed 
and non-sealed.", cn);
-                return;
+        if (isSealed) {
+            if (isFinal) {
+                addError("The " + getDescription(cn) + " cannot be both final 
and sealed.", cn);
             }
-            if (isFinal && explicitNonSealed) {
-                addError("The " + getDescription(cn) + " cannot be both final 
and non-sealed.", cn);
-                return;
+            if (isNonSealed) {
+                addError("The " + getDescription(cn) + " cannot be both sealed 
and non-sealed.", cn);
             }
-            if (sealedSuper) {
-                checkSealedParent(cn, superCN);
+            if (cn.getPermittedSubclasses().isEmpty()) {
+                addError("Sealed " + getDescription(cn) + " has no explicit or 
implicit permitted subclasses.", cn);
             }
-            if (sealedInterface) {
-                for (ClassNode candidate : cn.getInterfaces()) {
-                    if (candidate.isSealed()) {
-                        checkSealedParent(cn, candidate);
-                    }
+        }
+
+        boolean sealedSuper = sc != null && sc.isSealed();
+        boolean nonSealedSuper = sc != null && isNonSealed(sc);
+        boolean sealedInterface = 
Arrays.stream(cn.getInterfaces()).anyMatch(ClassNode::isSealed);
+        boolean nonSealedInterface = 
Arrays.stream(cn.getInterfaces()).anyMatch(this::isNonSealed);
+
+        if (isNonSealed && !(sealedSuper || sealedInterface || nonSealedSuper 
|| nonSealedInterface)) {
+            addError("The " + getDescription(cn) + " cannot be non-sealed as 
it has no sealed parent.", cn);
+        }
+        if (sealedSuper) {
+            checkSealedParent(cn, sc);
+        }
+        if (sealedInterface) {
+            for (ClassNode si : cn.getInterfaces()) {
+                if (si.isSealed()) {
+                    checkSealedParent(cn, si);
                 }
             }
         }
-        if (superCN == null || !isFinal(superCN.getModifiers())) return;
-        addError("You are not allowed to extend the final " + 
getDescription(superCN) + ".", cn);
     }
 
-    private boolean nonSealed(final ClassNode cn) {
-        if 
(Boolean.TRUE.equals(cn.getNodeMetaData(groovy.transform.NonSealed.class))) {
+    private boolean isNonSealed(final ClassNode cn) {
+        if (cn.isPrimaryClassNode()
+                && 
Boolean.TRUE.equals(cn.getNodeMetaData(groovy.transform.NonSealed.class))) {
             return true;
         }
         ClassNode sc = cn.getSuperClass(); // GROOVY-11292, GROOVY-11750: 
check super class
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java 
b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 60678f7470..63352d5e02 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -24,8 +24,6 @@ import groovy.lang.MetaClass;
 import groovy.transform.CompileStatic;
 import groovy.transform.Generated;
 import groovy.transform.Internal;
-import groovy.transform.NonSealed;
-import groovy.transform.Sealed;
 import groovy.transform.stc.POJO;
 import org.apache.groovy.ast.tools.ClassNodeUtils;
 import org.apache.groovy.util.BeanUtils;
@@ -165,8 +163,6 @@ public class Verifier implements GroovyClassVisitor, 
Opcodes {
     public static final String __TIMESTAMP = "__timeStamp";
     public static final String __TIMESTAMP__ = "__timeStamp__239_neverHappen";
 
-    private static final Class<Sealed> SEALED_TYPE = Sealed.class;
-    private static final Class<NonSealed> NON_SEALED_TYPE = NonSealed.class;
     private static final Parameter[] SET_METACLASS_PARAMS = {new 
Parameter(ClassHelper.METACLASS_TYPE, "mc")};
 
     private ClassNode classNode;
@@ -272,7 +268,7 @@ public class Verifier implements GroovyClassVisitor, 
Opcodes {
         checkForDuplicateMethods(node);
         checkForDuplicateConstructors(node);
         addCovariantMethods(node);
-        detectNonSealedClasses(node);
+        detectNonSealedType(node);
         checkFinalVariables(node);
     }
 
@@ -286,21 +282,15 @@ public class Verifier implements GroovyClassVisitor, 
Opcodes {
         }
     }
 
-    private static void detectNonSealedClasses(final ClassNode node) {
+    private static void detectNonSealedType(final ClassNode node) {
         if (isFinal(node.getModifiers())) return;
-        if (Boolean.TRUE.equals(node.getNodeMetaData(SEALED_TYPE))) return;
-        if (Boolean.TRUE.equals(node.getNodeMetaData(NON_SEALED_TYPE))) return;
-        ClassNode sn = node.getSuperClass();
-        boolean found = false;
-        while (sn != null && !sn.equals(ClassHelper.OBJECT_TYPE)) {
-            if (sn.isSealed()) {
-                found = true;
-                break;
+        if 
(Boolean.TRUE.equals(node.getNodeMetaData(groovy.transform.Sealed.class))) 
return;
+        if 
(Boolean.TRUE.equals(node.getNodeMetaData(groovy.transform.NonSealed.class))) 
return;
+        for (ClassNode sc = node.getSuperClass(); sc != null && 
!isObjectType(sc); sc = sc.getSuperClass()) {
+            if (sc.isSealed()) {
+                node.putNodeMetaData(groovy.transform.NonSealed.class, 
Boolean.TRUE);
+                return;
             }
-            sn = sn.getSuperClass();
-        }
-        if (found) {
-            node.putNodeMetaData(NON_SEALED_TYPE, Boolean.TRUE);
         }
     }
 
diff --git a/src/test/groovy/bugs/Groovy11292.groovy 
b/src/test/groovy/bugs/Groovy11292.groovy
index 07c167b1d8..3ec8245cbb 100644
--- a/src/test/groovy/bugs/Groovy11292.groovy
+++ b/src/test/groovy/bugs/Groovy11292.groovy
@@ -30,6 +30,20 @@ final class Groovy11292 {
 
     @Test
     void testClassWithNonSealedParent1() {
+        assertScript '''import java.lang.ref.SoftReference // non-sealed type
+
+            class TestReference<T> extends SoftReference<T> {
+                TestReference(T referent) {
+                    super(referent)
+                }
+            }
+
+            assert new TestReference(null)
+        '''
+    }
+
+    @Test
+    void testClassWithNonSealedParent2() {
         assumeTrue(isAtLeastJdk('17.0'))
 
         def config = new CompilerConfiguration(
@@ -74,17 +88,42 @@ final class Groovy11292 {
         }
     }
 
+    // GROOVY-11768
     @Test
-    void testClassWithNonSealedParent2() {
-        assertScript '''import java.lang.ref.SoftReference // non-sealed type
+    void testClassWithNonSealedParent3() {
+        assumeTrue(isAtLeastJdk('17.0'))
 
-            class TestReference<T> extends SoftReference<T> {
-                TestReference(T referent) {
-                    super(referent)
-                }
-            }
+        def config = new CompilerConfiguration(
+            targetDirectory: File.createTempDir(),
+            jointCompilationOptions: [memStub: true]
+        )
 
-            assert new TestReference(null)
-        '''
+        def parentDir = File.createTempDir()
+        try {
+            def a = new File(parentDir, 'A.java')
+            a.write '''
+                public abstract sealed class A permits B {}
+            '''
+            def b = new File(parentDir, 'B.java')
+            b.write '''
+                public abstract non-sealed class B extends A {}
+            '''
+            def c = new File(parentDir, 'C.java')
+            c.write '''
+                public class C extends B {}
+            '''
+            def d = new File(parentDir, 'D.groovy')
+            d.write '''
+                class D extends C {}
+            '''
+
+            def loader = new GroovyClassLoader(this.class.classLoader)
+            def cu = new JavaAwareCompilationUnit(config, loader)
+            cu.addSources(a, b, c, d)
+            cu.compile()
+        } finally {
+            config.targetDirectory.deleteDir()
+            parentDir.deleteDir()
+        }
     }
 }

Reply via email to