From 22d3272117a384e0bf7429646b5ce7d1b98eab5d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 20 Nov 2008 19:47:57 +0000 Subject: [PATCH] updated for JRuby 1.1 --- .../scripting/jruby/JRubyScriptUtils.java | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/scripting/jruby/JRubyScriptUtils.java b/org.springframework.context/src/main/java/org/springframework/scripting/jruby/JRubyScriptUtils.java index 228cc99fec..67544bd901 100644 --- a/org.springframework.context/src/main/java/org/springframework/scripting/jruby/JRubyScriptUtils.java +++ b/org.springframework.context/src/main/java/org/springframework/scripting/jruby/JRubyScriptUtils.java @@ -34,7 +34,6 @@ import org.jruby.ast.Node; import org.jruby.exceptions.JumpException; import org.jruby.exceptions.RaiseException; import org.jruby.javasupport.JavaEmbedUtils; -import org.jruby.runtime.DynamicScope; import org.jruby.runtime.builtin.IRubyObject; import org.springframework.core.NestedRuntimeException; @@ -46,8 +45,7 @@ import org.springframework.util.StringUtils; /** * Utility methods for handling JRuby-scripted objects. * - *

As of Spring 2.5, this class supports JRuby 0.9.9, 0.9.9 and 1.0.x. - * Note that there is no support for JRuby 1.1 at this point! + *

As of Spring 3.0, this class requires JRuby 1.1 or higher. * * @author Rob Harrop * @author Juergen Hoeller @@ -56,11 +54,6 @@ import org.springframework.util.StringUtils; */ public abstract class JRubyScriptUtils { - // Determine whether the old JRuby 0.9 parse method is available (incompatible with 1.0) - private final static Method oldParseMethod = ClassUtils.getMethodIfAvailable( - Ruby.class, "parse", new Class[] {String.class, String.class, DynamicScope.class}); - - /** * Create a new JRuby-scripted object from the given script source, * using the default {@link ClassLoader}. @@ -85,23 +78,12 @@ public abstract class JRubyScriptUtils { public static Object createJRubyObject(String scriptSource, Class[] interfaces, ClassLoader classLoader) { Ruby ruby = initializeRuntime(); - Node scriptRootNode = null; - /* TODO: make this JRuby 1.1 compliant - - Node scriptRootNode = (oldParseMethod != null ? - (Node) ReflectionUtils.invokeMethod(oldParseMethod, ruby, new Object[] {scriptSource, "", null}) : - ruby.parse(scriptSource, "", null, 0)); - */ - IRubyObject rubyObject = null; - /** TODO: make this JRuby 1.1 compliant - IRubyObject rubyObject = ruby.eval(scriptRootNode); - */ + Node scriptRootNode = ruby.parseEval(scriptSource, "", null, 0); + IRubyObject rubyObject = ruby.runNormally(scriptRootNode, false); if (rubyObject instanceof RubyNil) { String className = findClassName(scriptRootNode); - /** TODO: make this JRuby 1.1 compliant - rubyObject = ruby.evalScript("\n" + className + ".new"); - */ + rubyObject = ruby.evalScriptlet("\n" + className + ".new"); } // still null? if (rubyObject instanceof RubyNil) { @@ -141,12 +123,12 @@ public abstract class JRubyScriptUtils { if (node instanceof ClassNode) { return (ClassNode) node; } - List children = node.childNodes(); - for (int i = 0; i < children.size(); i++) { - Node child = (Node) children.get(i); + List children = node.childNodes(); + for (Node child : children) { if (child instanceof ClassNode) { return (ClassNode) child; - } else if (child instanceof NewlineNode) { + } + else if (child instanceof NewlineNode) { NewlineNode nn = (NewlineNode) child; Node found = findClassNode(nn.getNextNode()); if (found instanceof ClassNode) { @@ -154,8 +136,7 @@ public abstract class JRubyScriptUtils { } } } - for (int i = 0; i < children.size(); i++) { - Node child = (Node) children.get(i); + for (Node child : children) { Node found = findClassNode(child); if (found instanceof ClassNode) { return (ClassNode) found; @@ -184,7 +165,7 @@ public abstract class JRubyScriptUtils { return (isProxyForSameRubyObject(args[0]) ? Boolean.TRUE : Boolean.FALSE); } else if (ReflectionUtils.isHashCodeMethod(method)) { - return new Integer(this.rubyObject.hashCode()); + return this.rubyObject.hashCode(); } else if (ReflectionUtils.isToStringMethod(method)) { String toStringResult = this.rubyObject.toString();