34
34
import org .jruby .exceptions .JumpException ;
35
35
import org .jruby .exceptions .RaiseException ;
36
36
import org .jruby .javasupport .JavaEmbedUtils ;
37
- import org .jruby .runtime .DynamicScope ;
38
37
import org .jruby .runtime .builtin .IRubyObject ;
39
38
40
39
import org .springframework .core .NestedRuntimeException ;
46
45
/**
47
46
* Utility methods for handling JRuby-scripted objects.
48
47
*
49
- * <p>As of Spring 2.5, this class supports JRuby 0.9.9, 0.9.9 and 1.0.x.
50
- * <b>Note that there is no support for JRuby 1.1 at this point!</b>
48
+ * <p>As of Spring 3.0, this class requires JRuby 1.1 or higher.
51
49
*
52
50
* @author Rob Harrop
53
51
* @author Juergen Hoeller
56
54
*/
57
55
public abstract class JRubyScriptUtils {
58
56
59
- // Determine whether the old JRuby 0.9 parse method is available (incompatible with 1.0)
60
- private final static Method oldParseMethod = ClassUtils .getMethodIfAvailable (
61
- Ruby .class , "parse" , new Class [] {String .class , String .class , DynamicScope .class });
62
-
63
-
64
57
/**
65
58
* Create a new JRuby-scripted object from the given script source,
66
59
* using the default {@link ClassLoader}.
@@ -85,23 +78,12 @@ public static Object createJRubyObject(String scriptSource, Class[] interfaces)
85
78
public static Object createJRubyObject (String scriptSource , Class [] interfaces , ClassLoader classLoader ) {
86
79
Ruby ruby = initializeRuntime ();
87
80
88
- Node scriptRootNode = null ;
89
- /* TODO: make this JRuby 1.1 compliant
90
-
91
- Node scriptRootNode = (oldParseMethod != null ?
92
- (Node) ReflectionUtils.invokeMethod(oldParseMethod, ruby, new Object[] {scriptSource, "", null}) :
93
- ruby.parse(scriptSource, "", null, 0));
94
- */
95
- IRubyObject rubyObject = null ;
96
- /** TODO: make this JRuby 1.1 compliant
97
- IRubyObject rubyObject = ruby.eval(scriptRootNode);
98
- */
81
+ Node scriptRootNode = ruby .parseEval (scriptSource , "" , null , 0 );
82
+ IRubyObject rubyObject = ruby .runNormally (scriptRootNode , false );
99
83
100
84
if (rubyObject instanceof RubyNil ) {
101
85
String className = findClassName (scriptRootNode );
102
- /** TODO: make this JRuby 1.1 compliant
103
- rubyObject = ruby.evalScript("\n" + className + ".new");
104
- */
86
+ rubyObject = ruby .evalScriptlet ("\n " + className + ".new" );
105
87
}
106
88
// still null?
107
89
if (rubyObject instanceof RubyNil ) {
@@ -141,21 +123,20 @@ private static ClassNode findClassNode(Node node) {
141
123
if (node instanceof ClassNode ) {
142
124
return (ClassNode ) node ;
143
125
}
144
- List children = node .childNodes ();
145
- for (int i = 0 ; i < children .size (); i ++) {
146
- Node child = (Node ) children .get (i );
126
+ List <Node > children = node .childNodes ();
127
+ for (Node child : children ) {
147
128
if (child instanceof ClassNode ) {
148
129
return (ClassNode ) child ;
149
- } else if (child instanceof NewlineNode ) {
130
+ }
131
+ else if (child instanceof NewlineNode ) {
150
132
NewlineNode nn = (NewlineNode ) child ;
151
133
Node found = findClassNode (nn .getNextNode ());
152
134
if (found instanceof ClassNode ) {
153
135
return (ClassNode ) found ;
154
136
}
155
137
}
156
138
}
157
- for (int i = 0 ; i < children .size (); i ++) {
158
- Node child = (Node ) children .get (i );
139
+ for (Node child : children ) {
159
140
Node found = findClassNode (child );
160
141
if (found instanceof ClassNode ) {
161
142
return (ClassNode ) found ;
@@ -184,7 +165,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
184
165
return (isProxyForSameRubyObject (args [0 ]) ? Boolean .TRUE : Boolean .FALSE );
185
166
}
186
167
else if (ReflectionUtils .isHashCodeMethod (method )) {
187
- return new Integer ( this .rubyObject .hashCode () );
168
+ return this .rubyObject .hashCode ();
188
169
}
189
170
else if (ReflectionUtils .isToStringMethod (method )) {
190
171
String toStringResult = this .rubyObject .toString ();
0 commit comments