Top Description Inners Constructors Methods
org.jruby.javasupport

public Class JavaEmbedUtils

extends Object
Class Inheritance
Imports
java.io.InputStream, java.util.List, org.jcodings.specific.UTF8Encoding, org.jruby.ParseResult, .Ruby, .RubyArray, .RubyInstanceConfig, .RubyInteger, .RubyModule, .RubyObjectAdapter, .RubyRuntimeAdapter, .RubyString, org.jruby.ast.Node, org.jruby.runtime.Block, .Helpers, org.jruby.runtime.builtin.IRubyObject

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 and Inner Type Summary

Modifier and TypeClass and Description
public static interface
JavaEmbedUtils.EvalUnit

All implementers can be run and will return the last value in the evaluation unit.

public static class
JavaEmbedUtils.InterpretedEvalUnit

An evaluation unit which is based on running JRuby's interpreter (as opposed to the compiler).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Ruby

Returns:

an instance
initialize
(List<String>
to specify where to look for Ruby modules.
loadPaths
)

Get an instance of a JRuby runtime.

public static Ruby

Returns:

an instance
initialize
(List<String>
additional load paths you wish to add
loadPaths
,
RubyInstanceConfig
a runtime configuration instance
config
)

Get an instance of a JRuby runtime.

public static <T> T

Returns:

the result of the invocation.
invokeMethod
(Ruby
environment where the invoke will occur
runtime
,
Object
is the instance that will receive the method call
receiver
,
String
is method to be called
method
,
Object[]
are the arguments to the method
args
,
Class<T>
is the type we want it to conform to
returnType
)

Convenience function for embedders

public static IRubyObject
javaToRuby(Ruby runtime, Object value)

Convert a java object to a Ruby object.

public static IRubyObject
javaToRuby(Ruby runtime, boolean value)

public static IRubyObject
javaToRuby(Ruby runtime, byte value)

public static IRubyObject
javaToRuby(Ruby runtime, char value)

public static IRubyObject
javaToRuby(Ruby runtime, double value)

public static IRubyObject
javaToRuby(Ruby runtime, float value)

public static IRubyObject
javaToRuby(Ruby runtime, int value)

public static IRubyObject
javaToRuby(Ruby runtime, long value)

public static IRubyObject
javaToRuby(Ruby runtime, short value)

public static RubyObjectAdapter
public static RubyRuntimeAdapter
public static <T> T
rubyToJava(IRubyObject value, Class<T> type)

Convert a Ruby object to a Java object.

public static <T> T
rubyToJava(Ruby runtime, IRubyObject value, Class<T> type)

public static <T> T

Returns:

the converted object
rubyToJava
(IRubyObject
to be converted
value
)

Convert the Ruby object to a Java Object.

public static void
terminate(Ruby
to be disposed of
runtime
)

Dispose of the runtime you initialized.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

JavaEmbedUtilsback to summary
public JavaEmbedUtils()

Method Detail

initializeback to summary
public static Ruby initialize(List<String> loadPaths)

Get an instance of a JRuby runtime. Provide any loadpaths you want used at startup.

Parameters
loadPaths:List<String>

to specify where to look for Ruby modules.

Returns:Ruby

an instance

initializeback to summary
public static Ruby initialize(List<String> loadPaths, RubyInstanceConfig config)

Get an instance of a JRuby runtime.

Parameters
loadPaths:List<String>

additional load paths you wish to add

config:RubyInstanceConfig

a runtime configuration instance

Returns:Ruby

an instance

invokeMethodback to summary
public static <T> T invokeMethod(Ruby runtime, Object receiver, String method, Object[] args, Class<T> returnType)

Convenience function for embedders

Parameters
runtime:Ruby

environment where the invoke will occur

receiver:Object

is the instance that will receive the method call

method:String

is method to be called

args:Object[]

are the arguments to the method

returnType:Class<T>

is the type we want it to conform to

Returns:T

the result of the invocation.

Annotations
@SuppressWarnings:deprecation
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, Object value)

Convert a java object to a Ruby object.

Annotations
@SuppressWarnings:deprecation
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, boolean value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, byte value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, char value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, double value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, float value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, int value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, long value)
javaToRubyback to summary
public static IRubyObject javaToRuby(Ruby runtime, short value)
newObjectAdapterback to summary
public static RubyObjectAdapter newObjectAdapter()
newRuntimeAdapterback to summary
public static RubyRuntimeAdapter newRuntimeAdapter()
rubyToJavaback to summary
public static <T> T rubyToJava(IRubyObject value, Class<T> type)

Convert a Ruby object to a Java object.

rubyToJavaback to summary
public static <T> T rubyToJava(Ruby runtime, IRubyObject value, Class<T> type)
rubyToJavaback to summary
public static <T> T rubyToJava(IRubyObject value)

Convert the Ruby object to a Java Object.

Parameters
value:IRubyObject

to be converted

Returns:T

the converted object

terminateback to summary
public static void terminate(Ruby runtime)

Dispose of the runtime you initialized.

Parameters
runtime:Ruby

to be disposed of

org.jruby.javasupport back to summary

public Interface JavaEmbedUtils.EvalUnit

Known Direct Subinterfaces
org.jruby.embed.EmbedEvalUnit
Known Direct Implementers
org.jruby.javasupport.JavaEmbedUtils.InterpretedEvalUnit

All implementers can be run and will return the last value in the evaluation unit.

Method Summary

Modifier and TypeMethod and Description
public IRubyObject

Returns:

results of executing this evaluation unit.
run
()

Method Detail

runback to summary
public IRubyObject run()
Returns:IRubyObject

results of executing this evaluation unit.

org.jruby.javasupport back to summary

public Class JavaEmbedUtils.InterpretedEvalUnit

extends Object
implements EvalUnit
Class Inheritance
All Implemented Interfaces
org.jruby.javasupport.JavaEmbedUtils.EvalUnit

An evaluation unit which is based on running JRuby's interpreter (as opposed to the compiler).

Field Summary

Modifier and TypeField and Description
private final ParseResult
private final Ruby

Constructor Summary

AccessConstructor and Description
protected
protected
InterpretedEvalUnit(Ruby runtime, Node node)
Deprecated

Method Summary

Modifier and TypeMethod and Description
public IRubyObject
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

resultback to summary
private final ParseResult result
runtimeback to summary
private final Ruby runtime

Constructor Detail

InterpretedEvalUnitback to summary
protected InterpretedEvalUnit(Ruby runtime, ParseResult result)
InterpretedEvalUnitback to summary
protected InterpretedEvalUnit(Ruby runtime, Node node)

Deprecated

Annotations
@Deprecated

Method Detail

runback to summary
public IRubyObject run()

Implements org.jruby.javasupport.JavaEmbedUtils.EvalUnit.run.

Returns:IRubyObject

Doc from org.jruby.javasupport.JavaEmbedUtils.EvalUnit.run.

results of executing this evaluation unit.