Package org.jruby.javasupport
Class JavaEmbedUtils
java.lang.Object
org.jruby.javasupport.JavaEmbedUtils
Utility functions to help embedders out.
These function consolidate logic that is used between BSF and JSR 223.
People who are embedding JRuby 'raw' should use these as well.
If at a later date, we discover a flaw or change how we do things, this utility class should provide some insulation.
Example: Ruby runtime = JavaEmbedUtils.initialize(List.of("a/custom/load/path")); RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter(); IRubyObject rubyObject = evaler.parse(runtime, expr.toString(), file, line).run()); SomeClassOrInterface javaObject = JavaEmbedUtils.rubyToJava(rubyObject, SomeClassOrInterface.class); runtime.terminate();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
All implementers can be run and will return the last value in the evaluation unit.static class
An evaluation unit which is based on running JRuby's interpreter (as opposed to the compiler). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Ruby
initialize
(List<String> loadPaths) Get an instance of a JRuby runtime.static Ruby
initialize
(List<String> loadPaths, RubyInstanceConfig config) Get an instance of a JRuby runtime.static <T> T
Convenience function for embeddersstatic IRubyObject
javaToRuby
(Ruby runtime, boolean value) static IRubyObject
javaToRuby
(Ruby runtime, byte value) static IRubyObject
javaToRuby
(Ruby runtime, char value) static IRubyObject
javaToRuby
(Ruby runtime, double value) static IRubyObject
javaToRuby
(Ruby runtime, float value) static IRubyObject
javaToRuby
(Ruby runtime, int value) static IRubyObject
javaToRuby
(Ruby runtime, long value) static IRubyObject
javaToRuby
(Ruby runtime, short value) static IRubyObject
javaToRuby
(Ruby runtime, Object value) Convert a java object to a Ruby object.static RubyObjectAdapter
static RubyRuntimeAdapter
static <T> T
rubyToJava
(Ruby runtime, IRubyObject value, Class<T> type) static <T> T
rubyToJava
(IRubyObject value) Convert the Ruby object to a Java Object.static <T> T
rubyToJava
(IRubyObject value, Class<T> type) Convert a Ruby object to a Java object.static void
Dispose of the runtime you initialized.
-
Constructor Details
-
JavaEmbedUtils
public JavaEmbedUtils()
-
-
Method Details
-
initialize
Get an instance of a JRuby runtime. Provide any loadpaths you want used at startup.- Parameters:
loadPaths
- to specify where to look for Ruby modules.- Returns:
- an instance
-
initialize
Get an instance of a JRuby runtime.- Parameters:
loadPaths
- additional load paths you wish to addconfig
- a runtime configuration instance- Returns:
- an instance
-
newObjectAdapter
-
newRuntimeAdapter
-
terminate
Dispose of the runtime you initialized.- Parameters:
runtime
- to be disposed of
-
invokeMethod
public static <T> T invokeMethod(Ruby runtime, Object receiver, String method, Object[] args, Class<T> returnType) Convenience function for embedders- Parameters:
runtime
- environment where the invoke will occurreceiver
- is the instance that will receive the method callmethod
- is method to be calledargs
- are the arguments to the methodreturnType
- is the type we want it to conform to- Returns:
- the result of the invocation.
-
rubyToJava
Convert a Ruby object to a Java object. -
rubyToJava
-
rubyToJava
Convert the Ruby object to a Java Object.- Parameters:
value
- to be converted- Returns:
- the converted object
-
javaToRuby
Convert a java object to a Ruby object. -
javaToRuby
-
javaToRuby
-
javaToRuby
-
javaToRuby
-
javaToRuby
-
javaToRuby
-
javaToRuby
-
javaToRuby
-