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/3a31e958 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/3a31e958 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/3a31e958 Branch: refs/heads/GROOVY_2_4_X Commit: 3a31e958a2a948f5c1cd5e4a052d59e3ca04e65d Parents: ecefae0 Author: paulk <[email protected]> Authored: Thu Nov 24 08:38:12 2016 +1000 Committer: paulk <[email protected]> Committed: Fri Nov 25 22:11:33 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/3a31e958/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 38e2624..27bbe98 100644 --- a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java +++ b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java @@ -142,7 +142,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/3a31e958/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' + ''' + } +}
