Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X 06e72374a -> a7354c298
GROOVY-8743: Abstract method in trait should not have method body (cherry picked from commit 45bd8295ebe85b63d8b0bd425fc03a530c6868b7) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c405e6a9 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c405e6a9 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c405e6a9 Branch: refs/heads/GROOVY_2_6_X Commit: c405e6a998a9ae1dc57882d994fcdd96f3d6d628 Parents: 06e7237 Author: Daniel Sun <sun...@apache.org> Authored: Sat Aug 11 03:31:20 2018 +0800 Committer: Daniel Sun <sun...@apache.org> Committed: Sat Aug 11 04:19:41 2018 +0800 ---------------------------------------------------------------------- .../apache/groovy/parser/antlr4/AstBuilder.java | 12 +++++----- .../groovy/parser/antlr4/SyntaxErrorTest.groovy | 4 ++++ .../src/test/resources/fail/Trait_01.groovy | 23 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/c405e6a9/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 02fef29..f708bd0 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 @@ -1582,8 +1582,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov @Override public MethodNode visitMethodDeclaration(MethodDeclarationContext ctx) { - validateMethodDeclaration(ctx); - ModifierManager modifierManager = createModifierManager(ctx); if (modifierManager.containsAny(VAR)) { @@ -1637,7 +1635,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov return methodNode; } - private void validateMethodDeclaration(MethodDeclarationContext ctx) { + private void validateMethodDeclaration(MethodDeclarationContext ctx, MethodNode methodNode, ModifierManager modifierManager, ClassNode classNode) { if (1 == ctx.t || 2 == ctx.t || 3 == ctx.t) { // 1: normal method declaration; 2: abstract method declaration; 3: normal method declaration OR abstract method declaration if (!(asBoolean(ctx.modifiersOpt().modifiers()) || asBoolean(ctx.returnType()))) { throw createParsingFailedException("Modifiers or return type is required", ctx); @@ -1650,17 +1648,21 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov } } + /* if (2 == ctx.t) { if (asBoolean(ctx.methodBody())) { throw createParsingFailedException("Abstract method should not have method body", ctx); } } - } + */ - private void validateMethodDeclaration(MethodDeclarationContext ctx, MethodNode methodNode, ModifierManager modifierManager, ClassNode classNode) { boolean isAbstractMethod = methodNode.isAbstract(); boolean hasMethodBody = asBoolean(methodNode.getCode()); + if (isAbstractMethod && hasMethodBody) { + throw createParsingFailedException("Abstract method should not have method body", ctx); + } + if (9 == ctx.ct) { // script if (isAbstractMethod || !hasMethodBody) { // method should not be declared abstract in the script throw createParsingFailedException("You can not define a " + (isAbstractMethod ? "abstract" : "") + " method[" + methodNode.getName() + "] " + (!hasMethodBody ? "without method body" : "") + " in the script. Try " + (isAbstractMethod ? "removing the 'abstract'" : "") + (isAbstractMethod && !hasMethodBody ? " and" : "") + (!hasMethodBody ? " adding a method body" : ""), methodNode); http://git-wip-us.apache.org/repos/asf/groovy/blob/c405e6a9/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 1712849..d18abfb 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 @@ -236,6 +236,10 @@ class SyntaxErrorTest extends GroovyTestCase { TestUtils.doRunAndShouldFail('fail/UnaryOperator_02x.groovy'); } + void "test groovy core - Trait"() { + TestUtils.shouldFail('fail/Trait_01.groovy'); + } + /**************************************/ static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) { ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST) http://git-wip-us.apache.org/repos/asf/groovy/blob/c405e6a9/subprojects/parser-antlr4/src/test/resources/fail/Trait_01.groovy ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Trait_01.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Trait_01.groovy new file mode 100644 index 0000000..f30b4e5 --- /dev/null +++ b/subprojects/parser-antlr4/src/test/resources/fail/Trait_01.groovy @@ -0,0 +1,23 @@ +/* + * 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. + */ +package fail + +trait Trait_01 { + abstract m() {} +}