Class JavaUtil

java.lang.Object
org.jruby.javasupport.JavaUtil

public class JavaUtil extends Object
  • Field Details

  • Constructor Details

    • JavaUtil

      public JavaUtil()
  • Method Details

    • convertJavaArrayToRuby

      public static IRubyObject[] convertJavaArrayToRuby(Ruby runtime, Object[] objects)
    • convertStringArrayToRuby

      public static IRubyObject[] convertStringArrayToRuby(Ruby runtime, String[] strings, JavaUtil.StringConverter converter)
    • convertJavaArrayToRubyWithNesting

      public static RubyArray convertJavaArrayToRubyWithNesting(ThreadContext context, Object array)
    • getJavaConverter

      public static JavaUtil.JavaConverter getJavaConverter(Class clazz)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, Object object)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, Object object, Class javaClass)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, int i)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, long l)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, float f)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, double d)
    • convertJavaToRuby

      public static IRubyObject convertJavaToRuby(Ruby runtime, boolean b)
    • convertJavaToUsableRubyObject

      public static IRubyObject convertJavaToUsableRubyObject(Ruby runtime, Object object)
      Returns a usable RubyObject; for types that are not converted to Ruby native types, a Java proxy will be returned.
      Parameters:
      runtime -
      object -
      Returns:
      corresponding Ruby type, or a functional Java proxy
    • convertJavaToUsableRubyObjectWithConverter

      public static IRubyObject convertJavaToUsableRubyObjectWithConverter(Ruby runtime, Object object, JavaUtil.JavaConverter converter)
    • convertJavaArrayElementToRuby

      public static IRubyObject convertJavaArrayElementToRuby(Ruby runtime, JavaUtil.JavaConverter converter, Object array, int i)
    • primitiveToWrapper

      public static Class<?> primitiveToWrapper(Class<?> type)
    • isDuckTypeConvertable

      public static boolean isDuckTypeConvertable(Class<?> argumentType, Class<?> targetType)
    • convertProcToInterface

      public static <T> T convertProcToInterface(ThreadContext context, RubyObject rubyObject, Class<T> targetType)
    • convertProcToInterface

      public static <T> T convertProcToInterface(ThreadContext context, RubyBasicObject rubyObject, Class<T> targetType)
    • getNumericConverter

      public static <T> JavaUtil.NumericConverter<T> getNumericConverter(Class<T> target)
    • isJavaObject

      public static boolean isJavaObject(IRubyObject object)
      Test if a passed instance is a wrapper Java object.
      Parameters:
      object -
      Returns:
      true if the object is wrapping a Java object
    • unwrapJavaObject

      public static <T> T unwrapJavaObject(IRubyObject object)
      Unwrap a wrapped Java object.
      Parameters:
      object -
      Returns:
      Java object
      See Also:
    • unwrapIfJavaObject

      public static <T> T unwrapIfJavaObject(IRubyObject object)
      Unwrap if the passed object is a Java object, otherwise return object.
      Parameters:
      object -
      Returns:
      java object or passed object
      See Also:
    • inspectObject

      public static RubyString inspectObject(ThreadContext context, Object obj)
    • unwrapJavaValue

      public static <T> T unwrapJavaValue(IRubyObject object)
      Parameters:
      object -

      Note: Returns null if not a wrapped Java value.

      Returns:
      unwrapped Java (object's) value
    • getJavaPropertyName

      public static String getJavaPropertyName(String beanMethodName)
      For methods that match /(get|set|is)([A-Z0-9])(.*)/, return the "name" part of the property with leading lower-case.

      Note: Does not use regular expression for performance reasons.

      Parameters:
      beanMethodName - the bean method from which to extract a name
      Returns:
      the bean property name (or null)
    • toJavaGetName

      public static String toJavaGetName(String propertyName)
    • toJavaIsName

      public static String toJavaIsName(String propertyName)
    • getRubyCasedName

      public static String getRubyCasedName(String javaCasedName)
      Build a Ruby name from a Java name by treating '_' as divider and successive caps as all the same word.
      Parameters:
      javaCasedName -
      Returns:
      Ruby (under-score) cased named e.g. "get_foo_bar"
    • getJavaCasedName

      public static String getJavaCasedName(String javaCasedName)
    • getRubyNamesForJavaName

      public static Set<String> getRubyNamesForJavaName(String javaName, List<Method> methods)
      Given a simple Java method name and the Java Method objects that represent all its overloads, add to the given nameSet all possible Ruby names that would be valid.
      Parameters:
      javaName -
      methods -
      Returns:
      method names
    • convertArguments

      public static Object[] convertArguments(IRubyObject[] args, Class<?>[] types)
    • convertArguments

      public static Object[] convertArguments(IRubyObject[] args, Class<?>[] types, int offset)
    • clone

      public static <T> T clone(Object object)
      Clone a Java object, assuming its class has an accessible clone method.
      Parameters:
      object -
      Returns:
      cloned object or null (if method is not found or inaccessible)
    • getHandleSafe

      public static MethodHandle getHandleSafe(Method method, Class caller, MethodHandles.Lookup lookup)
    • getGetterSafe

      public static MethodHandle getGetterSafe(Field field, Class caller, MethodHandles.Lookup lookup)
    • getSetterSafe

      public static MethodHandle getSetterSafe(Field field, Class caller, MethodHandles.Lookup lookup)
    • trySimpleConversions

      public static IRubyObject trySimpleConversions(Ruby runtime, Object object)
    • objectFromJavaProxy

      public static Object objectFromJavaProxy(IRubyObject self)
    • getPrimitiveClass

      public static Class<?> getPrimitiveClass(String name)
    • unwrapJava

      public static <T> T unwrapJava(Object wrapped, T defaultValue)
    • unwrapJava

      @Deprecated(since="10.0.0.0") public static <T> T unwrapJava(IRubyObject wrapped)
      Deprecated.
    • unwrapJava

      public static <T> T unwrapJava(ThreadContext context, IRubyObject wrapped)
    • getJavaClass

      @Deprecated(since="10.0.0.0") public static Class<?> getJavaClass(RubyModule type) throws TypeError
      Deprecated.
      Throws:
      TypeError
    • getJavaClass

      public static Class<?> getJavaClass(ThreadContext context, RubyModule type) throws TypeError
      Get the associated JavaClass for a Java proxy module/class or wrapper.

      Note: Works best when passed module/class is assumed to be a Java proxy wrapper.

      Parameters:
      type -
      Returns:
      class
      Throws:
      TypeError
    • getJavaClass

      public static Class<?> getJavaClass(RubyModule type, Supplier<Class<?>> ifNone)
      Get the associated JavaClass for a Java proxy module/class or wrapper.
      Parameters:
      type -
      ifNone - fallback if none Java class wrapper
      Returns:
      class or the result of the supplier function