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 2023/10/17 17:30:20 UTC
[groovy] branch master updated: GROOVY-6567: `null` closure delegate for `NullObject` method receiver
This is an automated email from the ASF dual-hosted git repository.
emilles 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 f605090b88 GROOVY-6567: `null` closure delegate for `NullObject` method receiver
f605090b88 is described below
commit f605090b88fc2b1a7bffcd32b032d38d7b0989fa
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 17 12:23:21 2023 -0500
GROOVY-6567: `null` closure delegate for `NullObject` method receiver
---
.../runtime/metaclass/ClosureMetaMethod.java | 23 +++++++------
src/test/groovy/bugs/Groovy6567.groovy | 38 ++++++++++++++++++++++
2 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
index 5859fceebe..037080aabb 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
@@ -28,6 +28,7 @@ import org.codehaus.groovy.runtime.GeneratedClosure;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.runtime.MethodClosure;
+import org.codehaus.groovy.runtime.NullObject;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -41,19 +42,19 @@ import java.util.List;
*/
public class ClosureMetaMethod extends MetaMethod implements ClosureInvokingMethod {
+ private final String name;
private final Closure callable;
private final CachedMethod doCall;
- private final String name;
- private final CachedClass declaringClass;
+ private final CachedClass declaringClass;
public ClosureMetaMethod(String name, Closure c, CachedMethod doCall) {
this(name, c.getOwner().getClass(), c, doCall);
}
public ClosureMetaMethod(String name, Class declaringClass, Closure c, CachedMethod doCall) {
- super (doCall.getNativeParameterTypes());
+ super(doCall.getNativeParameterTypes());
this.name = name;
- callable = c;
+ this.callable = c;
this.doCall = doCall;
this.declaringClass = ReflectionCache.getCachedClass(declaringClass);
}
@@ -79,12 +80,14 @@ public class ClosureMetaMethod extends MetaMethod implements ClosureInvokingMeth
}
@Override
- public Object invoke(final Object object, Object[] arguments) {
- Closure cloned = (Closure) callable.clone();
- cloned.setDelegate(object);
-
- arguments = coerceArgumentsToClasses(arguments);
- return doCall.invoke(cloned, arguments);
+ public Object invoke(final Object object, final Object[] arguments) {
+ Closure clone = (Closure) callable.clone();
+ if (object == NullObject.getNullObject()) {
+ clone.setDelegate(null); // GROOVY-6567
+ } else {
+ clone.setDelegate(object);
+ }
+ return doCall.invoke(clone, coerceArgumentsToClasses(arguments));
}
/**
diff --git a/src/test/groovy/bugs/Groovy6567.groovy b/src/test/groovy/bugs/Groovy6567.groovy
new file mode 100644
index 0000000000..b6cf20a778
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6567.groovy
@@ -0,0 +1,38 @@
+/*
+ * 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
+
+final class Groovy6567 {
+
+ @Test
+ void testNullDelegate() {
+ try {
+ Object.metaClass.method = {
+ delegate?.getClass()
+ }
+ def nil = null
+ def ret = nil.method()
+ assert ret !instanceof Class
+ } finally {
+ Object.metaClass = null
+ }
+ }
+}