You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/05/30 07:14:58 UTC
groovy git commit: Validate method declaration
Repository: groovy
Updated Branches:
refs/heads/master 7a89d1859 -> e308ecf93
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/e308ecf9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e308ecf9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e308ecf9
Branch: refs/heads/master
Commit: e308ecf93b8cabeb1a33f1b66c6498769e9fc8fb
Parents: 7a89d18
Author: sunlan <su...@apache.org>
Authored: Tue May 30 15:14:21 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue May 30 15:14:49 2017 +0800
----------------------------------------------------------------------
.../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/e308ecf9/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 e498a03..17a6c16 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
@@ -883,7 +883,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
modifiers,
ClassHelper.OBJECT_TYPE);
}
-
}
this.configureAST(classNode, ctx);
@@ -1338,12 +1337,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);
}
@@ -2798,6 +2804,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/e308ecf9/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 c3fb287..2bbcc01 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
@@ -58,6 +58,10 @@ class ModifierManager {
this.modifierNodeList = Collections.unmodifiableList(asBoolean((Object) modifierNodeList) ? modifierNodeList : Collections.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/e308ecf9/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/e308ecf9/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() {}
+}