Class JavaEmbedUtils

java.lang.Object
org.jruby.javasupport.JavaEmbedUtils

public class JavaEmbedUtils extends Object
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();
 
  • Constructor Details

    • JavaEmbedUtils

      public JavaEmbedUtils()
  • Method Details

    • initialize

      public static Ruby initialize(List<String> loadPaths)
      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

      public static Ruby initialize(List<String> loadPaths, RubyInstanceConfig config)
      Get an instance of a JRuby runtime.
      Parameters:
      loadPaths - additional load paths you wish to add
      config - a runtime configuration instance
      Returns:
      an instance
    • newObjectAdapter

      public static RubyObjectAdapter newObjectAdapter()
    • newRuntimeAdapter

      public static RubyRuntimeAdapter newRuntimeAdapter()
    • terminate

      public static void terminate(Ruby runtime)
      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 occur
      receiver - is the instance that will receive the method call
      method - is method to be called
      args - are the arguments to the method
      returnType - is the type we want it to conform to
      Returns:
      the result of the invocation.
    • rubyToJava

      public static <T> T rubyToJava(IRubyObject value, Class<T> type)
      Convert a Ruby object to a Java object.
    • rubyToJava

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

      public static <T> T rubyToJava(IRubyObject value)
      Convert the Ruby object to a Java Object.
      Parameters:
      value - to be converted
      Returns:
      the converted object
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, Object value)
      Convert a java object to a Ruby object.
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, boolean value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, byte value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, char value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, double value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, float value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, int value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, long value)
    • javaToRuby

      public static IRubyObject javaToRuby(Ruby runtime, short value)