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 19:38:22 UTC
[groovy] branch GROOVY_3_0_X 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 GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new daff91108e GROOVY-10105: adapt script from other loader context
daff91108e is described below
commit daff91108e8d523d7f2711ec06cdee810f10f6e0
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Nov 30 12:16:06 2022 -0600
GROOVY-10105: adapt script from other loader context
3_0_X backport
---
.../org/codehaus/groovy/runtime/InvokerHelper.java | 63 ++++++++++++----------
1 file changed, 35 insertions(+), 28 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
index 197a848ab2..6d3afec018 100644
--- a/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
+++ b/src/main/java/org/codehaus/groovy/runtime/InvokerHelper.java
@@ -70,14 +70,10 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import static java.lang.Math.max;
-
/**
* A static helper class to make bytecode generation easier and act as a facade over the Invoker
*/
public class InvokerHelper {
- private static final Object[] EMPTY_MAIN_ARGS = new Object[]{new String[0]};
-
public static final Object[] EMPTY_ARGS = {};
protected static final Object[] EMPTY_ARGUMENTS = EMPTY_ARGS;
protected static final Class[] EMPTY_TYPES = {};
@@ -445,41 +441,54 @@ public class InvokerHelper {
}
static class NullScript extends Script {
- public NullScript() { this(new Binding()); }
public NullScript(Binding context) { super(context); }
+ public NullScript() { this(new Binding()); }
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 {
- final GroovyObject object = (GroovyObject) scriptClass.getDeclaredConstructor().newInstance();
- // it could just be a class, so let's wrap it in a Script
- // wrapper; though the bindings will be ignored
+ 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) {
+ Object object = scriptClass.getDeclaredConstructor().newInstance();
+ // adapt "new ScriptClass().main(args)" to Script
script = new Script(context) {
+ @Override
public Object run() {
- Object argsToPass = EMPTY_MAIN_ARGS;
+ Object[] mainArgs = {new String[0]};
try {
Object args = getProperty("args");
if (args instanceof String[]) {
- argsToPass = args;
+ mainArgs[0] = args;
}
} catch (MissingPropertyException e) {
// They'll get empty args since none exist in the context.
}
- object.invokeMethod(MAIN_METHOD_NAME, argsToPass);
- return null;
+ return InvokerHelper.invokeMethod(object, MAIN_METHOD_NAME, mainArgs);
}
};
- Map variables = context.getVariables();
MetaClass mc = getMetaClass(object);
+ Map variables = context.getVariables();
for (Object o : variables.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String key = entry.getKey().toString();
@@ -487,13 +496,11 @@ public class InvokerHelper {
setPropertySafe(key.startsWith("_") ? script : object, mc, key, entry.getValue());
}
}
- } 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<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException {
@@ -740,7 +747,7 @@ public class InvokerHelper {
}
private static int sizeLeft(int maxSize, StringBuilder buffer) {
- return maxSize == -1 ? maxSize : max(0, maxSize - buffer.length());
+ return maxSize == -1 ? maxSize : Math.max(0, maxSize - buffer.length());
}
private static String formatCollection(Collection collection, boolean verbose, int maxSize, boolean safe) {