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/04/07 13:31:03 UTC

[20/50] [abbrv] groovy git commit: GROOVY-8046: ClassFormatError void field/variable/parameter (closes #513)

GROOVY-8046: ClassFormatError void field/variable/parameter (closes #513)


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

Branch: refs/heads/parrot
Commit: 439e297529c1c63bd145f0fe9d8a6d2621da520a
Parents: 48f99a1
Author: paulk <pa...@asert.com.au>
Authored: Wed Mar 15 09:48:05 2017 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Wed Mar 15 12:22:53 2017 +1000

----------------------------------------------------------------------
 .../classgen/ClassCompletionVerifier.java       | 23 +++++++--
 src/test/groovy/bugs/Groovy8046Bug.groovy       | 52 ++++++++++++++++++++
 2 files changed, 70 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/439e2975/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java b/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
index c2da690..3cea76a 100644
--- a/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
+++ b/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
@@ -43,6 +43,7 @@ import org.codehaus.groovy.syntax.Types;
 import org.codehaus.groovy.transform.trait.Traits;
 
 import static java.lang.reflect.Modifier.*;
+import static org.codehaus.groovy.ast.ClassHelper.VOID_TYPE;
 import static org.objectweb.asm.Opcodes.*;
 /**
  * Checks that a class satisfies various conditions including:
@@ -260,6 +261,10 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         return "field '" + node.getName() + "'";
     }
 
+    private static String getDescription(Parameter node) {
+        return "parameter '" + node.getName() + "'";
+    }
+
     private void checkAbstractDeclaration(MethodNode methodNode) {
         if (!methodNode.isAbstract()) return;
         if (isAbstract(currentClass.getModifiers())) return;
@@ -272,11 +277,8 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         ClassNode superCN = cn.getSuperClass();
         if (superCN == null) return;
         if (!isFinal(superCN.getModifiers())) return;
-        StringBuilder msg = new StringBuilder();
-        msg.append("You are not allowed to overwrite the final ");
-        msg.append(getDescription(superCN));
-        msg.append(".");
-        addError(msg.toString(), cn);
+        String msg = "You are not allowed to overwrite the final " + getDescription(superCN) + ".";
+        addError(msg, cn);
     }
 
     private void checkImplementsAndExtends(ClassNode node) {
@@ -407,6 +409,11 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         checkMethodModifiers(node);
         checkGenericsUsage(node, node.getParameters());
         checkGenericsUsage(node, node.getReturnType());
+        for (Parameter param : node.getParameters()) {
+            if (param.getType().equals(VOID_TYPE)) {
+                addError("The " + getDescription(param) + " in " +  getDescription(node) + " has invalid type void", param);
+            }
+        }
         super.visitMethod(node);
     }
 
@@ -485,6 +492,9 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         }
         checkInterfaceFieldModifiers(node);
         checkGenericsUsage(node, node.getType());
+        if (node.getType().equals(VOID_TYPE)) {
+            addError("The " + getDescription(node) + " has invalid type void", node);
+        }
         super.visitField(node);
     }
 
@@ -635,6 +645,9 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         checkInvalidDeclarationModifier(expression, ACC_SYNCHRONIZED, "synchronized");
         checkInvalidDeclarationModifier(expression, ACC_TRANSIENT, "transient");
         checkInvalidDeclarationModifier(expression, ACC_VOLATILE, "volatile");
+        if (expression.getVariableExpression().getOriginType().equals(VOID_TYPE)) {
+            addError("The variable '" + expression.getVariableExpression().getName() + "' has invalid type void", expression);
+        }
     }
 
     private void checkInvalidDeclarationModifier(DeclarationExpression expression, int modifier, String modName) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/439e2975/src/test/groovy/bugs/Groovy8046Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8046Bug.groovy b/src/test/groovy/bugs/Groovy8046Bug.groovy
new file mode 100644
index 0000000..c8bf633
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8046Bug.groovy
@@ -0,0 +1,52 @@
+/*
+ *  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
+
+class Groovy8046Bug extends CompilableTestSupport {
+    void testFieldShouldNotHavePrimitiveVoidType() {
+        def message = shouldNotCompile """
+            class MyClass {
+                void field
+            }
+        """
+        assert message.contains("The field 'field' has invalid type void")
+    }
+
+    void testParameterShouldNotHavePrimitiveVoidType() {
+        def message = shouldNotCompile """
+            class MyClass {
+                int foo(void param) {}
+            }
+        """
+        assert message.contains("The parameter 'param' in method 'int foo(void)' has invalid type void")
+    }
+
+    void testLocalVariableShouldNotHavePrimitiveVoidType() {
+        def message = shouldNotCompile """
+            class MyClass {
+                def foo() {
+                    void bar = null
+                }
+            }
+        """
+        assert message.contains("The variable 'bar' has invalid type void")
+    }
+}