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 2017/03/08 05:51:00 UTC

groovy git commit: GROOVY-6792: ClassFormatError if a method has dots within its name (closes #505)

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 157efaab8 -> bded34e8c


GROOVY-6792: ClassFormatError if a method has dots within its name (closes #505)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: bded34e8c0b6249190035165954996a3cf5bec9d
Parents: 157efaa
Author: paulk <pa...@asert.com.au>
Authored: Tue Feb 28 13:05:22 2017 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Wed Mar 8 15:50:43 2017 +1000

----------------------------------------------------------------------
 .../classgen/ClassCompletionVerifier.java       | 18 ++++++++-
 src/test/groovy/bugs/Groovy6792Bug.groovy       | 41 ++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/bded34e8/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 201014a..92f55ef 100644
--- a/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
+++ b/src/main/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
@@ -54,7 +54,7 @@ import static org.objectweb.asm.Opcodes.*;
  * </ul>
  */
 public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
-
+    private static final String[] INVALID_NAME_CHARS = {".", ":", "/", ";", "[", "<", ">"};
     private ClassNode currentClass;
     private SourceUnit source;
     private boolean inConstructor = false;
@@ -78,6 +78,7 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
             checkAbstractMethodVisibility(node);
             checkClassForOverwritingFinal(node);
             checkMethodsForIncorrectModifiers(node);
+            checkMethodsForIncorrectName(node);
             checkMethodsForWeakerAccess(node);
             checkMethodsForOverridingFinal(node);
             checkNoAbstractMethodsNonabstractClass(node);
@@ -289,6 +290,21 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
         }
     }
 
+    private void checkMethodsForIncorrectName(ClassNode cn) {
+        List<MethodNode> methods = cn.getAllDeclaredMethods();
+        for (MethodNode mNode : methods) {
+            String name = mNode.getName();
+            if (name.equals("<init>") || name.equals("<clinit>")) continue;
+            // Groovy allows more characters than Character.isValidJavaIdentifier() would allow
+            // if we find a good way to encode special chars we could remove (some of) these checks
+            for (String ch : INVALID_NAME_CHARS) {
+                if (name.contains(ch)) {
+                    addError("You are not allowed to have '" + ch + "' in a method name", mNode);
+                }
+            }
+        }
+    }
+
     private void checkMethodsForIncorrectModifiers(ClassNode cn) {
         if (!cn.isInterface()) return;
         for (MethodNode method : cn.getMethods()) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/bded34e8/src/test/groovy/bugs/Groovy6792Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy6792Bug.groovy b/src/test/groovy/bugs/Groovy6792Bug.groovy
new file mode 100644
index 0000000..9223174
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6792Bug.groovy
@@ -0,0 +1,41 @@
+/*
+ *  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 Groovy6792Bug extends CompilableTestSupport {
+    void testMethodWithSpecialCharsInName() {
+        assertScript """
+            class Foo {
+                static ",{}()|!?foo@#\\\$%^&*-=]\\\\bar'\\""(){ Foo.name }
+            }
+            assert Foo.",{}()|!?foo@#\\\$%^&*-=]\\\\bar'\\""() == 'Foo'
+        """
+    }
+
+    void testMethodWithInvalidName() {
+        def message = shouldNotCompile """
+            class Foo {
+                def "bar.baz"(){}
+            }
+        """
+        assert message.contains("You are not allowed to have '.' in a method name")
+    }
+}