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 2022/11/30 18:35:14 UTC

[groovy] branch master updated: GROOVY-10105: adapt script from other loader context

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 903c4faf46 GROOVY-10105: adapt script from other loader context
903c4faf46 is described below

commit 903c4faf466b08546c851d37b6c22b83545620fd
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Nov 30 12:16:06 2022 -0600

    GROOVY-10105: adapt script from other loader context
---
 .../org/codehaus/groovy/runtime/InvokerHelper.java | 47 ++++++++++++++--------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
index f226d55832..f664720379 100644
--- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -417,30 +417,45 @@ public class InvokerHelper {
 
     static class NullScript extends Script {
 
-        public NullScript() {
-            this(new Binding());
-        }
         public NullScript(Binding context) {
             super(context);
         }
 
+        public NullScript() {
+            this(new Binding());
+        }
+
         @Override
         public Object run() {
             return null;
         }
-
     }
-    public static Script createScript(Class scriptClass, Binding context) {
-        Script script;
 
+    @SuppressWarnings("unchecked")
+    public static Script createScript(final Class scriptClass, final Binding context) {
         if (scriptClass == null) {
-            script = new NullScript(context);
-        } else {
-            try {
-                if (Script.class.isAssignableFrom(scriptClass)) {
-                    script = newScript(scriptClass, context);
-                } else {
-                    // wrap call "ScriptClass.main(args)" with a Script instance
+            return new NullScript(context);
+        }
+
+        try {
+            Script script;
+            if (Script.class.isAssignableFrom(scriptClass)) {
+                script = newScript(scriptClass, context);
+            } else {
+                try {
+                    Class<?> glBinding = scriptClass.getClassLoader().loadClass(Binding.class.getName());
+                    Constructor<?> contextualConstructor = scriptClass.getDeclaredConstructor(glBinding);
+                    Object binding = glBinding.getDeclaredConstructor(Map.class).newInstance(context.getVariables());
+                    Object scriptx = contextualConstructor.newInstance(binding);
+                    // adapt "new ScriptClass(binding).run()" to Script
+                    script = new Script() {
+                        @Override
+                        public Object run() {
+                            return InvokerHelper.invokeMethod(scriptx, "run", EMPTY_ARGUMENTS);
+                        }
+                    };
+                } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ignore) {
+                    // adapt "ScriptClass.main(args)" to Script
                     script = new Script(context) {
                         @Override
                         public Object run() {
@@ -465,11 +480,11 @@ public class InvokerHelper {
                         }
                     });
                 }
-            } catch (Exception e) {
-                throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e, e);
             }
+            return script;
+        } catch (Exception e) {
+            throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e, e);
         }
-        return script;
     }
 
     public static Script newScript(Class<? extends Script> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {