You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/03/04 22:38:41 UTC

[groovy] branch GROOVY-9966 created (now a47baec)

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

emilles pushed a change to branch GROOVY-9966
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at a47baec  GROOVY-9966: safer object expression transformation

This branch includes the following new commits:

     new a47baec  GROOVY-9966: safer object expression transformation

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9966: safer object expression transformation

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9966
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit a47baeca77d019d113547270e7dfa90111ab9eda
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 9b84041..08416e3 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -134,14 +134,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) {
@@ -362,19 +362,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;
@@ -382,8 +381,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
+        '''
+    }
+}