diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java index e83eed26b01..bc036b1c1b8 100644 --- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java +++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java @@ -268,6 +268,7 @@ public void visitClass(final ClassNode node) { node.getObjectInitializerStatements().clear(); node.visitContents(this); checkForDuplicateMethods(node); + checkForDuplicateConstructors(node); addCovariantMethods(node); detectNonSealedClasses(node); checkFinalVariables(node); @@ -393,6 +394,18 @@ private static void checkForDuplicateMethods(final ClassNode cn) { } } + private static void checkForDuplicateConstructors(final ClassNode cn) { + Set 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(final 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 b724e2d90e6..e9824f23b3e 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') + } }