Return-Path: X-Original-To: apmail-groovy-commits-archive@minotaur.apache.org Delivered-To: apmail-groovy-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2276718AF1 for ; Tue, 22 Dec 2015 10:47:41 +0000 (UTC) Received: (qmail 92940 invoked by uid 500); 22 Dec 2015 10:47:41 -0000 Delivered-To: apmail-groovy-commits-archive@groovy.apache.org Received: (qmail 92906 invoked by uid 500); 22 Dec 2015 10:47:40 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 92897 invoked by uid 99); 22 Dec 2015 10:47:40 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Dec 2015 10:47:40 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 89B81E01F5; Tue, 22 Dec 2015 10:47:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: pascalschumacher@apache.org To: commits@groovy.apache.org Message-Id: <75629afba45043a5bc86877268224b41@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: groovy git commit: GROOVY-6352 Check the arguments even for the default constructor Date: Tue, 22 Dec 2015 10:47:40 +0000 (UTC) Repository: groovy Updated Branches: refs/heads/master 73f5979a4 -> 5c8d97b2c GROOVY-6352 Check the arguments even for the default constructor Whether an explicit constructor exists or the default constructor needs to be used, the candidate constructors need to go through the same checks, so invalid arguments won't be accepted against the default constructor. Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5c8d97b2 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5c8d97b2 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5c8d97b2 Branch: refs/heads/master Commit: 5c8d97b2cade37a8d4c7c21ecb94d6d35e9e15a8 Parents: 73f5979 Author: Frank Pavageau Authored: Mon Dec 21 16:02:20 2015 +0100 Committer: pascalschumacher Committed: Tue Dec 22 11:46:13 2015 +0100 ---------------------------------------------------------------------- .../transform/stc/StaticTypeCheckingVisitor.java | 8 +++++++- .../transform/stc/ConstructorsSTCTest.groovy | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/5c8d97b2/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index dea84f2..ecaecf7 100644 --- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -3781,7 +3781,13 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { if (methods.isEmpty()) { MethodNode node = new ConstructorNode(Opcodes.ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, GENERATED_EMPTY_STATEMENT); node.setDeclaringClass(receiver); - return Collections.singletonList(node); + methods = Collections.singletonList(node); + if (receiver.isArray()) { + // No need to check the arguments against an array constructor: it just needs to exist. The array is + // created through coercion or by specifying its dimension(s), anyway, and would not match an + // arbitrary number of parameters. + return methods; + } } } else { methods = findMethodsWithGenerated(receiver,name); http://git-wip-us.apache.org/repos/asf/groovy/blob/5c8d97b2/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy b/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy index f0d0c42..709ca32 100644 --- a/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy +++ b/src/test/groovy/transform/stc/ConstructorsSTCTest.groovy @@ -42,6 +42,23 @@ class ConstructorsSTCTest extends StaticTypeCheckingTestCase { ''', 'No matching constructor found: java.awt.Dimension(int)' } + void testWrongNumberOfArgumentsWithDefaultConstructor() { + shouldFailWithMessages ''' + class X {} + def foo() { + new X("f") + } + println foo() + ''', 'Cannot find matching method X#(java.lang.String)' + } + + void testCreateArrayWithDefaultConstructor() { + assertScript ''' + String[] strings = ['a','b','c'] + int[] ints = new int[2] + ''' + } + void testIncorrectArgumentTypes() { // test that wrong number of arguments will fail shouldFailWithMessages '''