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/11/23 15:48:19 UTC
(groovy) branch GROOVY-10381 updated (8ebd423669 -> 051810901c)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch GROOVY-10381
in repository https://gitbox.apache.org/repos/asf/groovy.git
discard 8ebd423669 GROOVY-10381: No compiler error for class that implements interfaces with duplicated default methods
new 051810901c GROOVY-10381: No compiler error for class that implements interfaces with duplicated default methods
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (8ebd423669)
\
N -- N -- N refs/heads/GROOVY-10381 (051810901c)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/main/java/org/codehaus/groovy/classgen/Verifier.java | 2 --
src/test/groovy/bugs/Groovy10381.groovy | 2 +-
2 files changed, 1 insertion(+), 3 deletions(-)
(groovy) 01/01: GROOVY-10381: No compiler error for class that implements interfaces with duplicated default methods
Posted by su...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY-10381
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 051810901c47794ccadd230ada8f271441c70856
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Nov 23 23:02:24 2024 +0800
GROOVY-10381: No compiler error for class that implements interfaces with duplicated default methods
---
.../org/codehaus/groovy/classgen/Verifier.java | 28 ++++
src/test/groovy/bugs/Groovy10381.groovy | 173 +++++++++++++++++++++
2 files changed, 201 insertions(+)
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 93b766ffbb..0ff3e7943a 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -232,6 +232,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
checkForDuplicateInterfaces(node);
+ checkForDuplicateDefaultMethods(node);
if (node.isInterface() || Traits.isTrait(node)) {
// interfaces have no constructors but this expects one,
@@ -275,6 +276,33 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
checkFinalVariables(node);
}
+ private static void checkForDuplicateDefaultMethods(ClassNode node) {
+ Map<String, MethodNode> defaultMethods = new HashMap<>(8);
+ node.getAllInterfaces().stream()
+ .flatMap(i -> i.getMethods().stream())
+ .filter(MethodNode::isDefault)
+ .forEach(m -> {
+ String signature = methodDescriptorWithoutReturnType(m);
+ MethodNode existing = defaultMethods.get(signature);
+ if (existing == null) {
+ defaultMethods.put(signature, m);
+ return;
+ }
+
+ ClassNode existingDeclaringClass = existing.getDeclaringClass();
+ ClassNode currentDeclaringClass = m.getDeclaringClass();
+ if (!(existingDeclaringClass.equals(currentDeclaringClass)
+ || existingDeclaringClass.implementsInterface(currentDeclaringClass)
+ || currentDeclaringClass.implementsInterface(existingDeclaringClass))) {
+ throw new RuntimeParserException(
+ (node.isInterface() ? "interface" : "class") + " " + node.getName()
+ + " inherits unrelated defaults for " + m.getTypeDescriptor()
+ + " from types " + existingDeclaringClass.getName()
+ + " and " + currentDeclaringClass.getName(), sourceOf(m));
+ }
+ });
+ }
+
private static final String[] INVALID_COMPONENTS = {"clone", "finalize", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait"};
private static void detectInvalidRecordComponentNames(final ClassNode node) {
diff --git a/src/test/groovy/bugs/Groovy10381.groovy b/src/test/groovy/bugs/Groovy10381.groovy
new file mode 100644
index 0000000000..11b6db772a
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10381.groovy
@@ -0,0 +1,173 @@
+/*
+ * 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.shouldFail
+
+final class Groovy10381 {
+ @Test
+ void testDuplicateDefaultMethodsFromGroovyClasses_implements1() {
+ def err = shouldFail '''
+ interface A {
+ default void m(String n) {}
+ }
+ interface B {
+ default void m(String n) {}
+ }
+ class C implements A, B {
+ void test() {
+ m('Hi')
+ }
+ }
+ '''
+ assert err =~ /class C inherits unrelated defaults for void m\(java.lang.String\) from types A and B/
+ }
+
+ @Test
+ void testDuplicateDefaultMethodsFromGroovyClasses_implements2() {
+ def err = shouldFail '''
+ interface BaseA {
+ default void m(String n) {}
+ }
+ interface A extends BaseA {
+ }
+ interface BaseB {
+ default void m(String n) {}
+ }
+ interface B extends BaseB {
+ }
+ class C implements A, B {
+ void test() {
+ m('Hi')
+ }
+ }
+ '''
+ assert err =~ /class C inherits unrelated defaults for void m\(java.lang.String\) from types BaseA and BaseB/
+ }
+
+ @Test
+ void testDuplicateDefaultMethodsFromGroovyClasses_extends() {
+ def err = shouldFail '''
+ public interface A extends List {
+ default void m(String n) {}
+ }
+ public interface B {
+ default void m(String n) {}
+ }
+ interface C extends A, B {
+ default void test() {
+ m('Hi')
+ }
+ }
+ '''
+ assert err =~ /interface C inherits unrelated defaults for void m\(java.lang.String\) from types A and B/
+ }
+
+ @Test
+ void testDuplicateDefaultMethodsFromJavaClasses_implements() {
+ def config = new CompilerConfiguration().tap {
+ jointCompilationOptions = [memStub: true]
+ targetDirectory = File.createTempDir()
+ }
+ File parentDir = File.createTempDir()
+ try {
+ def a = new File(parentDir, 'A.java')
+ a.write '''
+ public interface A {
+ default void m(String n) {}
+ }
+ '''
+
+ def b = new File(parentDir, 'B.java')
+ b.write '''
+ public interface B {
+ default void m(String n) {}
+ }
+ '''
+
+ def c = new File(parentDir, 'C.groovy')
+ c.write '''
+ class C implements A, B {
+ void test() {
+ m("test")
+ }
+ }
+ '''
+
+ def loader = new GroovyClassLoader(this.class.classLoader)
+ def cu = new JavaAwareCompilationUnit(config, loader)
+ cu.addSources(a, b, c)
+ cu.compile()
+ assert false
+ } catch (Exception e) {
+ assert e =~ /class C inherits unrelated defaults for m\(String\) from types A and B/
+ } finally {
+ parentDir.deleteDir()
+ config.targetDirectory.deleteDir()
+ }
+ }
+
+ @Test
+ void testDuplicateDefaultMethodsFromJavaClasses_extends() {
+ def config = new CompilerConfiguration().tap {
+ jointCompilationOptions = [memStub: true]
+ targetDirectory = File.createTempDir()
+ }
+ File parentDir = File.createTempDir()
+ try {
+ def a = new File(parentDir, 'A.java')
+ a.write '''
+ public interface A extends java.util.List {
+ default void m(String n) {}
+ }
+ '''
+
+ def b = new File(parentDir, 'B.java')
+ b.write '''
+ public interface B {
+ default void m(String n) {}
+ }
+ '''
+
+ def c = new File(parentDir, 'C.groovy')
+ c.write '''
+ interface C extends A, B {
+ default void test() {
+ m("test")
+ }
+ }
+ '''
+
+ def loader = new GroovyClassLoader(this.class.classLoader)
+ def cu = new JavaAwareCompilationUnit(config, loader)
+ cu.addSources(a, b, c)
+ cu.compile()
+ assert false
+ } catch (Exception e) {
+ assert e =~ /interface C inherits unrelated defaults for m\(String\) from types A and B/
+ } finally {
+ parentDir.deleteDir()
+ config.targetDirectory.deleteDir()
+ }
+ }
+}