GROOVY-6862: Traits dont allow $ in identifiers where normal classes do (closes 
#464)


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

Branch: refs/heads/parrot
Commit: ef7384c7464671bb1294fecbd90ec4f470f8a89c
Parents: 84cffe0
Author: paulk <pa...@asert.com.au>
Authored: Thu Nov 24 08:38:12 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Fri Nov 25 22:08:52 2016 +1000

----------------------------------------------------------------------
 .../groovy/transform/trait/TraitComposer.java   |  2 +-
 src/test/groovy/bugs/Groovy6862Bug.groovy       | 43 ++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ef7384c7/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java 
b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
index 2e36887..4b05908 100644
--- a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
+++ b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
@@ -143,7 +143,7 @@ public abstract class TraitComposer {
             String name = methodNode.getName();
             Parameter[] helperMethodParams = methodNode.getParameters();
             boolean isAbstract = methodNode.isAbstract();
-            if (!isAbstract && helperMethodParams.length > 0 && 
((methodNode.getModifiers() & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) && 
!name.contains("$")) {
+            if (!isAbstract && helperMethodParams.length > 0 && 
((methodNode.getModifiers() & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) && 
(!name.contains("$") || (methodNode.getModifiers() & Opcodes.ACC_SYNTHETIC) == 
0)) {
                 ArgumentListExpression argList = new ArgumentListExpression();
                 argList.addExpression(new VariableExpression("this"));
                 Parameter[] origParams = new 
Parameter[helperMethodParams.length - 1];

http://git-wip-us.apache.org/repos/asf/groovy/blob/ef7384c7/src/test/groovy/bugs/Groovy6862Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy6862Bug.groovy 
b/src/test/groovy/bugs/Groovy6862Bug.groovy
new file mode 100644
index 0000000..cc5c98e
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6862Bug.groovy
@@ -0,0 +1,43 @@
+/*
+ *  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
+
+class Groovy6862Bug extends GroovyTestCase {
+    void testDollarAllowedInTraitMethodNames() {
+        assertScript '''
+            trait Test {
+                def foo_ok() { 'trait non-$ instance OK' }
+                def foo$oops() { 'trait $ instance OK' }
+                static sfoo_ok() { 'trait non-$ static OK' }
+                static sfoo$oops() { 'trait $ static OK' }
+            }
+            class Foo implements Test {
+                def foo$ok() { 'class $ instance OK' }
+                static sfoo$ok() { 'class $ static OK' }
+            }
+            def foo = new Foo()
+            assert foo.foo$ok() == 'class $ instance OK'
+            assert foo.foo_ok() == 'trait non-$ instance OK'
+            assert foo.foo$oops() == 'trait $ instance OK'
+            assert Foo.sfoo$ok() == 'class $ static OK'
+            assert Foo.sfoo_ok() == 'trait non-$ static OK'
+            assert Foo.sfoo$oops() == 'trait $ static OK'
+        '''
+    }
+}

Reply via email to