This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new 7c86745904 GROOVY-11297: Fail to identify duplicate constructor
declaration (port to 2_5_X)
7c86745904 is described below
commit 7c867459046ea53eafdcee4f1480d8b4604ba1a6
Author: Paul King <[email protected]>
AuthorDate: Thu Jan 25 16:55:18 2024 +1000
GROOVY-11297: Fail to identify duplicate constructor declaration (port to
2_5_X)
---
src/main/java/org/codehaus/groovy/classgen/Verifier.java | 13 +++++++++++++
src/test/gls/classes/methods/RepetitiveMethodTest.groovy | 10 ++++++++++
2 files changed, 23 insertions(+)
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index c9b3763df1..39b9f360ce 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -255,6 +255,7 @@ public class Verifier implements GroovyClassVisitor,
Opcodes {
node.getObjectInitializerStatements().clear();
node.visitContents(this);
checkForDuplicateMethods(node);
+ checkForDuplicateConstructors(node);
addCovariantMethods(node);
checkFinalVariables(node);
@@ -307,6 +308,18 @@ public class Verifier implements GroovyClassVisitor,
Opcodes {
}
}
+ private static void checkForDuplicateConstructors(final ClassNode cn) {
+ Set<String> descriptors = new HashSet<>();
+ for (ConstructorNode cons : cn.getDeclaredConstructors()) {
+ String mySig = methodDescriptorWithoutReturnType(cons);
+ if (descriptors.contains(mySig)) {
+ throw new RuntimeParserException("The constructor " +
cons.getText() +
+ " duplicates another constructor of the same signature",
sourceOf(cons));
+ }
+ descriptors.add(mySig);
+ }
+ }
+
private static String scriptBodySignatureWithoutReturnType(ClassNode cn) {
for (MethodNode mn : cn.getMethods()) {
if (mn.isScriptBody()) return
methodDescriptorWithoutReturnType(mn);
diff --git a/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
b/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
index b724e2d90e..e9824f23b3 100644
--- a/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
+++ b/src/test/gls/classes/methods/RepetitiveMethodTest.groovy
@@ -48,4 +48,14 @@ class RepetitiveMethodTest extends CompilableTestSupport {
}
'''
}
+
+ void testRepetitiveConstructor() {
+ def message = shouldNotCompile('''
+ class A {
+ A(int x) {}
+ A(int y) {}
+ }
+ ''')
+ assert message.contains('duplicates another constructor of the same
signature')
+ }
}