Copilot commented on code in PR #2995:
URL: https://github.com/apache/groovy/pull/2995#discussion_r4362256802


##########
src/main/java/groovy/grape/GrabAnnotationTransformation.java:
##########
@@ -486,8 +486,10 @@ private void callGrabAsStaticInitIfNeeded(final ClassNode 
classNode, final Class
             for (Map.Entry<String, String> e : systemProperties.entrySet()) {
                 block.addStatement(stmt(callX(SYSTEM_CLASSNODE, "setProperty", 
args(constX(e.getKey()), constX(e.getValue())))));
             }
-            StaticMethodCallExpression enabled = callX(SYSTEM_CLASSNODE, 
"getProperty", args(constX("groovy.grape.enable"), constX("true")));
-            grabInitializers.add(ifS(eqX(enabled, constX("true")), block));
+            // only set the properties when the grab they support will 
actually run
+            Expression grabAvailable = andX(callX(grapeClassNode, 
"getEnableGrapes"),
+                                            notNullX(callX(grapeClassNode, 
"getInstance")));
+            grabInitializers.add(ifS(grabAvailable, block));

Review Comment:
   The generated call to `getEnableGrapes` assumes that method exists on 
`Grape`. If `enableGrapes` is a boolean property following standard JavaBeans 
conventions, the getter may be `isEnableGrapes()` instead, which would cause a 
runtime `MissingMethodException` in compiled scripts. Prefer generating a 
property access for `enableGrapes` (so Groovy resolves `is*` vs `get*` 
correctly), or update the call to match the actual `Grape` API method name.



##########
src/test/groovy/bugs/Groovy12461.groovy:
##########
@@ -0,0 +1,92 @@
+/*
+ *  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 bugs
+
+import groovy.grape.Grape
+import groovy.grape.GrapeEngine
+import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.BeforeEach
+import org.junit.jupiter.api.Test
+
+/**
+ * The system properties of {@code @GrabConfig} are only set when the grab 
runs.
+ */
+final class Groovy12461 {
+
+    private static final String PROBE = 'groovy.test.grabconfig.probe'
+
+    private static final String SCRIPT = """
+        @GrabConfig(systemProperties = '$PROBE=set-by-grabconfig')
+        @Grab('org.example:not-fetched:1.0')
+        import java.util.List
+        System.getProperty('$PROBE')
+    """
+
+    private GrapeEngine savedInstance
+    private boolean savedEnableGrapes
+    private String savedImpl
+
+    @BeforeEach
+    void setUp() {
+        savedInstance = Grape.instance
+        savedEnableGrapes = Grape.enableGrapes
+        savedImpl = System.getProperty('groovy.grape.impl')
+        System.clearProperty(PROBE)
+    }
+
+    @AfterEach
+    void tearDown() {
+        Grape.instance = savedInstance
+        Grape.enableGrapes = savedEnableGrapes
+        if (savedImpl == null) System.clearProperty('groovy.grape.impl')
+        else System.setProperty('groovy.grape.impl', savedImpl)
+        System.clearProperty(PROBE)
+    }
+
+    @Test
+    void testPropertiesSetWhenGrabRuns() {
+        Grape.instance = new NoOpGrapeEngine()
+        assert new GroovyShell().evaluate(SCRIPT) == 'set-by-grabconfig'
+    }
+
+    @Test
+    void testPropertiesNotSetWhenGrapesDisabled() {
+        Grape.instance = new NoOpGrapeEngine()
+        Grape.enableGrapes = false
+        assert new GroovyShell().evaluate(SCRIPT) == null
+    }
+
+    @Test
+    void testPropertiesNotSetWhenNoEngineAvailable() {
+        Grape.instance = null
+        System.setProperty('groovy.grape.impl', 
'org.example.NoSuchGrapeEngine')
+        assert new GroovyShell().evaluate(SCRIPT) == null
+    }
+
+    private static class NoOpGrapeEngine implements GrapeEngine {
+        Object grab(String endorsedModule) { null }
+        Object grab(Map args) { null }
+        Object grab(Map args, Map... dependencies) { null }
+        Map<String, Map<String, List<String>>> enumerateGrapes() { [:] }
+        URI[] resolve(Map args, Map... dependencies) { new URI[0] }
+        URI[] resolve(Map args, List depsInfo, Map... dependencies) { new 
URI[0] }
+        Map[] listDependencies(ClassLoader classLoader) { new Map[0] }

Review Comment:
   Consider adding `@Override` annotations to these `GrapeEngine` method 
implementations. It’s a low-cost way to ensure the test double stays in sync 
with the interface (e.g., if method signatures change) and helps the compiler 
catch accidental mismatches.



##########
src/test/groovy/bugs/Groovy12461.groovy:
##########
@@ -0,0 +1,92 @@
+/*
+ *  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 bugs
+
+import groovy.grape.Grape
+import groovy.grape.GrapeEngine
+import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.BeforeEach
+import org.junit.jupiter.api.Test
+
+/**
+ * The system properties of {@code @GrabConfig} are only set when the grab 
runs.
+ */
+final class Groovy12461 {
+
+    private static final String PROBE = 'groovy.test.grabconfig.probe'
+
+    private static final String SCRIPT = """
+        @GrabConfig(systemProperties = '$PROBE=set-by-grabconfig')
+        @Grab('org.example:not-fetched:1.0')
+        import java.util.List
+        System.getProperty('$PROBE')
+    """
+
+    private GrapeEngine savedInstance
+    private boolean savedEnableGrapes
+    private String savedImpl
+
+    @BeforeEach
+    void setUp() {
+        savedInstance = Grape.instance
+        savedEnableGrapes = Grape.enableGrapes
+        savedImpl = System.getProperty('groovy.grape.impl')
+        System.clearProperty(PROBE)
+    }
+
+    @AfterEach
+    void tearDown() {
+        Grape.instance = savedInstance
+        Grape.enableGrapes = savedEnableGrapes
+        if (savedImpl == null) System.clearProperty('groovy.grape.impl')
+        else System.setProperty('groovy.grape.impl', savedImpl)
+        System.clearProperty(PROBE)
+    }

Review Comment:
   These tests mutate global JVM state (`Grape.instance`, `Grape.enableGrapes`, 
and system properties). If the build enables JUnit 5 parallel execution, this 
can become flaky due to cross-test interference. Consider adding JUnit 5 
resource locks (e.g., locking `Resources.SYSTEM_PROPERTIES` and a custom lock 
for `groovy.grape.Grape`) or otherwise marking the class to run in the same 
thread to ensure isolation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to