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 2021/08/25 12:18:25 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9966: safer object expression transformation

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 46f020b  GROOVY-9966: safer object expression transformation
46f020b is described below

commit 46f020bbd1a7f80c395ca039c489243191a4c3d1
Author: Eric Milles <er...@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 44ad833..173bbee 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -122,14 +122,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) {
@@ -351,19 +351,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;
@@ -371,8 +370,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
+        '''
+    }
+}