You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/03/06 10:35:54 UTC
[groovy] branch master updated: GROOVY-9966: safer object
expression transformation
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new e81ae9e GROOVY-9966: safer object expression transformation
e81ae9e is described below
commit e81ae9e0ac8423646404fa0a772479dee09d7a05
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
+ '''
+ }
+}