You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/05/24 02:10:59 UTC

[groovy] 04/05: GROOVY-9141: add null check for classNode when checking for abstract method with body (closes #933)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 519cd72e361f1c712b1c495ebe2e4348cc8f478f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu May 23 12:19:50 2019 -0500

    GROOVY-9141: add null check for classNode when checking for abstract method with body (closes #933)
---
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   | 18 ++++-----
 src/test/groovy/bugs/Groovy9141.groovy             | 46 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index 40362d0..81ac6a4 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -901,7 +901,6 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
         ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
 
         if (classNode == null || !classNode.isAnnotationDefinition()) {
-
             assertNodeType(PARAMETERS, node);
             parameters = parameters(node);
             if (parameters == null) parameters = Parameter.EMPTY_ARRAY;
@@ -922,16 +921,17 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
         modifiers &= ~Opcodes.ACC_SYNTHETIC;
         methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
         if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
-            if (node == null) {
+            if (node != null) {
+                assertNodeType(SLIST, node);
+                code = statementList(node);
+            } else {
                 throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
             }
-            assertNodeType(SLIST, node);
-            code = statementList(node);
-        } else if (node != null && classNode.isAnnotationDefinition()) {
-            code = statement(node);
-            hasAnnotationDefault = true;
-        } else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
-            if (node != null) {
+        } else if (node != null) {
+            if (classNode != null && classNode.isAnnotationDefinition()) {
+                code = statement(node);
+                hasAnnotationDefault = true;
+            } else {
                 throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
             }
         }
diff --git a/src/test/groovy/bugs/Groovy9141.groovy b/src/test/groovy/bugs/Groovy9141.groovy
new file mode 100644
index 0000000..32dd3a4
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9141.groovy
@@ -0,0 +1,46 @@
+/*
+ *  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 groovy.bugs
+
+import gls.CompilableTestSupport
+import groovy.transform.CompileStatic
+
+@CompileStatic
+final class Groovy9141 extends CompilableTestSupport {
+
+    void testAbstractMethodWithBody1() {
+        def err = shouldNotCompile '''\
+            abstract def meth() {
+              println 42
+            }
+            '''.stripIndent()
+        assert err =~ / You can not define a abstract method\[meth\]  in the script. Try removing the 'abstract' /
+    }
+
+    void testAbstractMethodWithBody2() {
+        def err = shouldNotCompile '''\
+            class Main {
+              abstract def meth() {
+                println 42
+              }
+            }
+            '''.stripIndent()
+        assert err =~ / Can't have an abstract method in a non-abstract class. /
+    }
+}