This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY_3_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push: new 222f6af GROOVY-9966: safer object expression transformation 222f6af is described below commit 222f6af33d93f97d974b63522c46cf2435888661 Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Thu Mar 4 16:34:27 2021 -0600 GROOVY-9966: safer object expression transformation --- .../groovy/control/StaticImportVisitor.java | 22 +++++++------- src/test/groovy/bugs/Groovy9966.groovy | 35 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java index 100e861..5f7e1a5 100644 --- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java +++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java @@ -140,14 +140,14 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer { } if (clazz == ArgumentListExpression.class) { Expression result = exp.transformExpression(this); - if (inPropertyExpression) { + if (foundArgs == null && inPropertyExpression) { foundArgs = result; } return result; } if (exp instanceof ConstantExpression) { Expression result = exp.transformExpression(this); - if (inPropertyExpression) { + if (foundConstant == null && inPropertyExpression) { foundConstant = result; } if (inAnnotation && exp instanceof AnnotationConstantExpression) { @@ -368,19 +368,18 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer { pexp.setSourcePosition(pe); return pexp; } + boolean oldInPropertyExpression = inPropertyExpression; - Expression oldFoundArgs = foundArgs; Expression oldFoundConstant = foundConstant; + Expression oldFoundArgs = foundArgs; inPropertyExpression = true; - foundArgs = null; foundConstant = null; + foundArgs = null; Expression objectExpression = transform(pe.getObjectExpression()); - boolean candidate = false; - if (objectExpression instanceof MethodCallExpression) { - candidate = ((MethodCallExpression)objectExpression).isImplicitThis(); - } - - if (foundArgs != null && foundConstant != null && candidate) { + if (foundArgs != null && foundConstant != null + && !foundConstant.getText().trim().isEmpty() + && objectExpression instanceof MethodCallExpression + && ((MethodCallExpression)objectExpression).isImplicitThis()) { Expression result = findStaticMethodImportFromModule(foundConstant, foundArgs); if (result != null) { objectExpression = result; @@ -388,8 +387,9 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer { } } inPropertyExpression = oldInPropertyExpression; - foundArgs = oldFoundArgs; foundConstant = oldFoundConstant; + foundArgs = oldFoundArgs; + pe.setObjectExpression(objectExpression); return pe; } diff --git a/src/test/groovy/bugs/Groovy9966.groovy b/src/test/groovy/bugs/Groovy9966.groovy new file mode 100644 index 0000000..17b77a8 --- /dev/null +++ b/src/test/groovy/bugs/Groovy9966.groovy @@ -0,0 +1,35 @@ +/* + * 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 org.junit.Test + +import static groovy.test.GroovyAssert.shouldFail + +final class Groovy9966 { + @Test + void testWhenStarsAlign() { + shouldFail MissingPropertyException, ''' + import static java.util.Arrays.* + def m(x) { return x } + final value = 123.456 + m("$value").missing + ''' + } +}