groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: Validate method declaration
Date Sat, 03 Jun 2017 08:43:02 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X ca30386c7 -> f3d87bcd6


Validate method declaration


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 2d5dcd34e5398daf77b4472bde9c382fd6e041f5
Parents: ca30386
Author: sunlan <sunlan@apache.org>
Authored: Tue May 30 15:14:21 2017 +0800
Committer: paulk <paulk@asert.com.au>
Committed: Sat Jun 3 18:21:51 2017 +1000

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 13 +++++++++---
 .../groovy/parser/antlr4/ModifierManager.java   |  4 ++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  1 +
 .../resources/fail/ClassDeclaration_02x.groovy  | 22 ++++++++++++++++++++
 4 files changed, 37 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2d5dcd34/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 2feec71..ec1020b 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
@@ -896,7 +896,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                                 modifiers,
                                 ClassHelper.OBJECT_TYPE);
             }
-
         }
 
         this.configureAST(classNode, ctx);
@@ -1364,12 +1363,19 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         String className = classNode.getNodeMetaData(CLASS_NAME);
         int modifiers = modifierManager.getClassMemberModifiersOpValue();
 
-        if (!asBoolean(ctx.returnType())
-                && asBoolean(ctx.methodBody())
+        boolean hasReturnType = asBoolean(ctx.returnType());
+        boolean hasMethodBody = asBoolean(ctx.methodBody());
+
+        if (!hasReturnType
+                && hasMethodBody
                 && methodName.equals(className)) { // constructor declaration
 
             methodNode = createConstructorNodeForClass(methodName, parameters, exceptions,
code, classNode, modifiers);
         } else { // class memeber method declaration
+            if (!hasReturnType && hasMethodBody && (0 == modifierManager.getModifierCount()))
{
+                throw createParsingFailedException("Invalid method declaration: " + methodName,
ctx);
+            }
+
             methodNode = createMethodNodeForClass(ctx, modifierManager, methodName, returnType,
parameters, exceptions, code, classNode, modifiers);
         }
 
@@ -2831,6 +2837,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
         anonymousInnerClass.setUsingGenerics(false);
         anonymousInnerClass.setAnonymous(true);
+        anonymousInnerClass.putNodeMetaData(CLASS_NAME, fullName);
         this.configureAST(anonymousInnerClass, ctx);
 
         classNodeStack.push(anonymousInnerClass);

http://git-wip-us.apache.org/repos/asf/groovy/blob/2d5dcd34/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierManager.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierManager.java
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierManager.java
index 22d6d69..4032938 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierManager.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierManager.java
@@ -59,6 +59,10 @@ class ModifierManager {
         this.modifierNodeList = Collections.unmodifiableList(asBoolean((Object) modifierNodeList)
? modifierNodeList : Collections.<ModifierNode>emptyList());
     }
 
+    public int getModifierCount() {
+        return modifierNodeList.size();
+    }
+
     private void validate(List<ModifierNode> modifierNodeList) {
         Map<ModifierNode, Integer> modifierNodeCounter = new LinkedHashMap<>(modifierNodeList.size());
         int visibilityModifierCnt = 0;

http://git-wip-us.apache.org/repos/asf/groovy/blob/2d5dcd34/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 67a68f8..f311642 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
@@ -143,6 +143,7 @@ class SyntaxErrorTest extends GroovyTestCase {
 
     void "test groovy core - ClassDeclaration"() {
         TestUtils.doRunAndShouldFail('fail/ClassDeclaration_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/ClassDeclaration_02x.groovy');
     }
 
     void "test groovy core - MethodDeclaration"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/2d5dcd34/subprojects/parser-antlr4/src/test/resources/fail/ClassDeclaration_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/ClassDeclaration_02x.groovy
b/subprojects/parser-antlr4/src/test/resources/fail/ClassDeclaration_02x.groovy
new file mode 100644
index 0000000..662642d
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/ClassDeclaration_02x.groovy
@@ -0,0 +1,22 @@
+/*
+ *  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.
+ */
+class Foo {}
+new Foo() {
+    Foo() {}
+}


Mime
View raw message