groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject groovy git commit: GROOVY-6352 Check the arguments even for the default constructor
Date Tue, 22 Dec 2015 10:47:40 GMT
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 <fpavageau@ekino.com>
Authored: Mon Dec 21 16:02:20 2015 +0100
Committer: pascalschumacher <pascalschumacher@gmx.net>
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<init>(int)'
     }
 
+    void testWrongNumberOfArgumentsWithDefaultConstructor() {
+        shouldFailWithMessages '''
+            class X {}
+            def foo() {
+              new X("f")
+            }
+            println foo()
+        ''', 'Cannot find matching method X#<init>(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 '''


Mime
View raw message