You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2024/01/21 15:25:37 UTC
(groovy) 01/03: GROOVY-11292: Class without sealed parent cannot be non-sealed
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-11292
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit adaf3fa86ff925214132cbe1f3f23e7a838cb6fb
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jan 20 23:38:52 2024 +0800
GROOVY-11292: Class without sealed parent cannot be non-sealed
---
.github/workflows/groovy-build-test-main.yml | 2 +-
.../java/org/codehaus/groovy/ast/ClassNode.java | 12 +++
.../groovy/ast/decompiled/DecompiledClassNode.java | 14 +++
.../groovy/classgen/ClassCompletionVerifier.java | 3 +-
src/test/groovy/bugs/Groovy11292.groovy | 100 +++++++++++++++++++++
5 files changed, 128 insertions(+), 3 deletions(-)
diff --git a/.github/workflows/groovy-build-test-main.yml b/.github/workflows/groovy-build-test-main.yml
index 5e342f8fec..10e379cff2 100644
--- a/.github/workflows/groovy-build-test-main.yml
+++ b/.github/workflows/groovy-build-test-main.yml
@@ -26,7 +26,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04]
- java: [11, 17]
+ java: [11, 17, 21]
runs-on: ${{ matrix.os }}
env:
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GE_ACCESS_TOKEN }}
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassNode.java b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
index 0b726ef719..a2c9d98150 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
@@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.ast;
+import groovy.transform.NonSealed;
import org.apache.groovy.ast.tools.ClassNodeUtils;
import org.apache.groovy.lang.annotation.Incubating;
import org.codehaus.groovy.GroovyBugError;
@@ -47,6 +48,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import static java.lang.reflect.Modifier.isFinal;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.apache.groovy.ast.tools.MethodNodeUtils.getCodeAsBlock;
@@ -1333,6 +1335,16 @@ public class ClassNode extends AnnotatedNode {
return !getAnnotations(ClassHelper.SEALED_TYPE).isEmpty() || !getPermittedSubclasses().isEmpty();
}
+ @Incubating
+ public boolean isNonSealed() {
+ if (redirect != null) return redirect.isNonSealed();
+ if (Boolean.TRUE.equals(this.getNodeMetaData(NonSealed.class))) return true;
+
+ final ClassNode superClass = this.getSuperClass();
+ if (null == superClass) return false;
+ return superClass.isSealed() && !(isFinal(this.getModifiers()) || this.isSealed());
+ }
+
public boolean isResolved() {
if (clazz != null) return true;
if (redirect != null) return redirect.isResolved();
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..8cfe776512 100644
--- a/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
@@ -109,6 +109,20 @@ public class DecompiledClassNode extends ClassNode {
return false;
}
+ @Override
+ public boolean isNonSealed() {
+ Class<?> typeClass;
+ try {
+ typeClass = getTypeClass();
+ } catch (NoClassDefFoundError e) {
+ return false;
+ }
+
+ final Class<?> superclass = typeClass.getSuperclass();
+ if (null == superclass) return false;
+ return ReflectionUtils.isSealed(superclass) && !(Modifier.isFinal(typeClass.getModifiers()) || ReflectionUtils.isSealed(typeClass));
+ }
+
@Override
public String setName(String name) {
throw new UnsupportedOperationException();
diff --git a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
index 870704af79..33dbafa944 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
@@ -18,7 +18,6 @@
*/
package org.codehaus.groovy.classgen;
-import groovy.transform.NonSealed;
import groovy.transform.Sealed;
import org.apache.groovy.ast.tools.AnnotatedNodeUtils;
import org.apache.groovy.ast.tools.ClassNodeUtils;
@@ -368,7 +367,7 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
}
private boolean nonSealed(final ClassNode node) {
- return Boolean.TRUE.equals(node.getNodeMetaData(NonSealed.class));
+ return node.isNonSealed();
}
private void checkSealedParent(final ClassNode cn, final ClassNode parent) {
diff --git a/src/test/groovy/bugs/Groovy11292.groovy b/src/test/groovy/bugs/Groovy11292.groovy
new file mode 100644
index 0000000000..b4421e7800
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy11292.groovy
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package bugs
+
+import org.codehaus.groovy.control.CompilerConfiguration
+import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.isAtLeastJdk
+import static org.junit.Assume.assumeTrue
+
+final class Groovy11292 {
+ @Test
+ void testClassWithNonSealedParent_1() {
+ assumeTrue(isAtLeastJdk('17.0'))
+
+ def config = new CompilerConfiguration(
+ targetDirectory: File.createTempDir(),
+ jointCompilationOptions: [memStub: true]
+ )
+
+ def parentDir = File.createTempDir()
+ try {
+ def a = new File(parentDir, 'A.java')
+ a.write '''
+ public sealed class A permits B {}
+ '''
+ def b = new File(parentDir, 'B.java')
+ b.write '''
+ public non-sealed class B extends A {}
+ '''
+ def c = new File(parentDir, 'C.groovy')
+ c.write '''
+ class C extends B {}
+ '''
+ def d = new File(parentDir, 'D.groovy')
+ d.write '''
+ class D extends C {}
+ '''
+ def e = new File(parentDir, 'E.groovy')
+ e.write '''
+ @groovy.transform.CompileStatic
+ class E extends B {}
+ '''
+ def f = new File(parentDir, 'F.groovy')
+ f.write '''
+ @groovy.transform.CompileStatic
+ class F extends E {}
+ '''
+
+ def loader = new GroovyClassLoader(this.class.classLoader)
+ def cu = new JavaAwareCompilationUnit(config, loader)
+ cu.addSources(a, b, c, d, e, f)
+ cu.compile()
+ } finally {
+ config.targetDirectory.deleteDir()
+ parentDir.deleteDir()
+ }
+ }
+
+ @Test
+ void testClassWithNonSealedParent_2() {
+ assertScript '''
+ import org.codehaus.groovy.util.Finalizable
+ class TestReference<T>
+ extends java.lang.ref.SoftReference<T>
+ implements org.codehaus.groovy.util.Reference<T, Finalizable> {
+
+ final Finalizable handler
+
+ TestReference(T referent) {
+ super(referent)
+ }
+
+ @Override
+ Finalizable getHandler() {
+ return handler
+ }
+ }
+ assert new TestReference(null)
+ '''
+ }
+}