groovy git commit: Validate the duplicated parameters

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

groovy git commit: Validate the duplicated parameters

sunlan
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 0cbaabf40 -> fef2102a7


Validate the duplicated parameters

(cherry picked from commit df389dd)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fef2102a
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fef2102a
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fef2102a

Branch: refs/heads/GROOVY_2_6_X
Commit: fef2102a761aa0387348d1bbd628b4e0eb905f7d
Parents: 0cbaabf
Author: sunlan <[hidden email]>
Authored: Sat Jul 15 15:05:56 2017 +0800
Committer: sunlan <[hidden email]>
Committed: Sat Jul 15 15:09:30 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 18 +++++++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  2 ++
 .../resources/fail/MethodDeclaration_02x.groovy | 21 ++++++++++++++++++++
 .../resources/fail/MethodDeclaration_03x.groovy | 21 ++++++++++++++++++++
 4 files changed, 62 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/fef2102a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 20f64a5..1cf025f 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -3370,9 +3370,27 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             parameterList.add(this.visitLastFormalParameter(ctx.lastFormalParameter()));
         }
 
+        validateParameterList(parameterList);
+
         return parameterList.toArray(new Parameter[0]);
     }
 
+    private void validateParameterList(List<Parameter> parameterList) {
+        for (int n = parameterList.size(), i = n - 1; i >= 0; i--) {
+            Parameter parameter = parameterList.get(i);
+
+            for (Parameter otherParameter : parameterList) {
+                if (otherParameter == parameter) {
+                    continue;
+                }
+
+                if (otherParameter.getName().equals(parameter.getName())) {
+                    throw createParsingFailedException("Duplicated parameter '" + parameter.getName() + "' found.", parameter);
+                }
+            }
+        }
+    }
+
     @Override
     public Parameter visitFormalParameter(FormalParameterContext ctx) {
         return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), ctx.type(), null, ctx.variableDeclaratorId(), ctx.expression());

http://git-wip-us.apache.org/repos/asf/groovy/blob/fef2102a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 432be7b..7d4ffa1 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -149,6 +149,8 @@ class SyntaxErrorTest extends GroovyTestCase {
 
     void "test groovy core - MethodDeclaration"() {
         TestUtils.shouldFail('fail/MethodDeclaration_01.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_03x.groovy');
     }
 
     void "test groovy core - ConstructorDeclaration"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/fef2102a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
new file mode 100644
index 0000000..9d766f3
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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.
+ */
+
+def c = {a, a -> println a}
+c(1, 2)

http://git-wip-us.apache.org/repos/asf/groovy/blob/fef2102a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
new file mode 100644
index 0000000..d2c61ab
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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.
+ */
+
+def c(a, a) { println a}
+c(1, 2)

Loading...