This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY-11858 in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 0af3598443dac63329071dee92263c9162d52c32 Author: Eric Milles <[email protected]> AuthorDate: Sun Feb 15 08:47:51 2026 -0600 minor items --- .../groovy/runtime/metaclass/ClosureMetaClass.java | 6 +- .../gls/invocation/ClosureDelegationTest.groovy | 146 +++++++++++---------- 2 files changed, 80 insertions(+), 72 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java index 62140826cc..247cbf2048 100644 --- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java +++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java @@ -47,9 +47,7 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -253,7 +251,7 @@ public final class ClosureMetaClass extends MetaClassImpl { if (CLOSURE_DO_CALL_METHOD.equals(methodName) || CLOSURE_CALL_METHOD.equals(methodName)) { method = pickClosureMethod(argClasses); if (method == null && argClasses.length == 1 && List.class.isAssignableFrom(argClasses[0])) { - var list = (theArguments[0] instanceof Wrapper ? ((Wrapper) theArguments[0]).unwrap() : theArguments[0]); + var list = (theArguments[0] instanceof Wrapper wrapper ? wrapper.unwrap() : theArguments[0]); if (list != null) { var newArguments = ((List<?>) list).toArray(); var newArgClasses = MetaClassHelper.convertToTypeArray(newArguments); @@ -375,7 +373,7 @@ public final class ClosureMetaClass extends MetaClassImpl { throw first; } - private static final Set<String> INTERNAL_METHODS = new HashSet<>(Arrays.asList("curry", "ncurry", "rcurry", "leftShift", "rightShift")); + private static final Set<String> INTERNAL_METHODS = Set.of("curry", "ncurry", "rcurry", "leftShift", "rightShift"); private static boolean isInternalMethod(final String methodName) { return INTERNAL_METHODS.contains(methodName); } diff --git a/src/test/groovy/gls/invocation/ClosureDelegationTest.groovy b/src/test/groovy/gls/invocation/ClosureDelegationTest.groovy index c22479dd6a..77ca5d319f 100644 --- a/src/test/groovy/gls/invocation/ClosureDelegationTest.groovy +++ b/src/test/groovy/gls/invocation/ClosureDelegationTest.groovy @@ -18,108 +18,117 @@ */ package gls.invocation -import gls.CompilableTestSupport +import org.junit.jupiter.api.Test -class ClosureDelegationTest extends CompilableTestSupport { +import static groovy.test.GroovyAssert.assertScript +final class ClosureDelegationTest { + + @Test void testMissingMethodMissingMethod() { - assertScript """ -class A { - def methodMissing(String name, args) { - "A" - } -} + assertScript ''' + class A { + def methodMissing(String name, args) { + "A" + } + } -def methodMissing(String name, args) { - visited=true - throw new MissingMethodException(name,this.class,args) -} + def methodMissing(String name, args) { + visited=true + throw new MissingMethodException(name,this.class,args) + } -visited=false -def closure = { foo() } -closure.delegate = new A() -assert closure() == "A" -assert visited==true - """ + visited=false + def closure = { foo() } + closure.delegate = new A() + assert closure() == "A" + assert visited==true + ''' } + @Test void testInvokeMethodMissingMethod() { - assertScript """ -class A { - def invokeMethod(String name, args) { - "A" - } -} + assertScript ''' + class A { + def invokeMethod(String name, args) { + "A" + } + } -def methodMissing(String name, args) { - visited=true - throw new MissingMethodException(name,this.class,args) -} + def methodMissing(String name, args) { + visited=true + throw new MissingMethodException(name,this.class,args) + } -visited=false -def closure = { foo() } -closure.delegate = new A() -assert closure() == "A" -assert visited==true - """ + visited=false + def closure = { foo() } + closure.delegate = new A() + assert closure() == "A" + assert visited==true + ''' } + @Test void testMissingMethodInvokeMethod() { - assertScript """ -class A { - def methodMissing(String name, args) { - "A" - } -} + assertScript ''' + class A { + def methodMissing(String name, args) { + "A" + } + } -def invokeMethod(String name, args) { - visited=true - throw new MissingMethodException(name,this.class,args) -} + def invokeMethod(String name, args) { + visited=true + throw new MissingMethodException(name,this.class,args) + } -visited=false -def closure = { foo() } -closure.delegate = new A() -assert closure() == "A" -assert visited==true - """ + visited=false + def closure = { foo() } + closure.delegate = new A() + assert closure() == "A" + assert visited==true + ''' } + @Test void testInvokeMethodInvokeMethod() { - assertScript """ -class A { - def invokeMethod(String name, args) { - "A" - } -} + assertScript ''' + class A { + def invokeMethod(String name, args) { + "A" + } + } -def invokeMethod(String name, args) { - visited=true - throw new MissingMethodException(name,this.class,args) -} + def invokeMethod(String name, args) { + visited=true + throw new MissingMethodException(name,this.class,args) + } -visited=false -def closure = { foo() } -closure.delegate = new A() -assert closure() == "A" -assert visited==true - """ + visited=false + def closure = { foo() } + closure.delegate = new A() + assert closure() == "A" + assert visited==true + ''' } + @Test void testStaticMethod() { assertScript ''' class Foo { - static visited + static visited static closureInStaticContext = { foo() } static foo(){ visited = true } } + Foo.closureInStaticContext() assert Foo.visited == true ''' } + @Test void testStaticContextClosureDelegation() { assertScript ''' class Foo { @@ -130,8 +139,9 @@ assert visited==true static visited def foo() { visited = true } } + Foo.aClosure() assert Bar.visited == true ''' } -} \ No newline at end of file +}
