public IRubyObject getValue() { IRubyObject result = JavaUtil.convertJavaToRuby(runtime, functions, BSFFunctions.class); return result instanceof JavaObject ? Java.wrap(runtime, result) : result; }
@Deprecated public static long convertRubyToJavaLong(IRubyObject rubyObject) { return (Long) convertRubyToJava(rubyObject, long.class); }
private IRubyObject convertReturn(Object result) { if (result != null && result.getClass() != boxedReturnType) { // actual type does not exactly match method return type, re-get converter // FIXME: when the only autoconversions are primitives, this won't be needed return JavaUtil.convertJavaToUsableRubyObject(getRuntime(), result); } return JavaUtil.convertJavaToUsableRubyObjectWithConverter(getRuntime(), result, returnConverter); }
public static IRubyObject convertJavaToRuby(Ruby runtime, Object object, Class javaClass) { return convertJavaToUsableRubyObjectWithConverter(runtime, object, getJavaConverter(javaClass)); }
@Deprecated public static Object coerceOtherToType(ThreadContext context, IRubyObject arg, Class target) { if (isDuckTypeConvertable(arg.getClass(), target)) { RubyObject rubyObject = (RubyObject) arg; if (!rubyObject.respondsTo("java_object")) { return convertProcToInterface(context, rubyObject, target); } } // it's either as converted as we can make it via above logic or it's // not one of the types we convert, so just pass it out as-is without wrapping return arg; }
/** * Convenience function for embedders * * @param runtime environment where the invoke will occur * @param receiver is the instance that will receive the method call * @param method is method to be called * @param args are the arguments to the method * @param returnType is the type we want it to conform to * @return the result of the invocation. */ public static Object invokeMethod(Ruby runtime, Object receiver, String method, Object[] args, Class returnType) { IRubyObject rubyReceiver = receiver != null ? JavaUtil.convertJavaToRuby(runtime, receiver) : runtime.getTopSelf(); IRubyObject[] rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args); // Create Ruby proxies for any input arguments that are not primitives. for (int i = 0; i < rubyArgs.length; i++) { IRubyObject obj = rubyArgs[i]; if (obj instanceof JavaObject) rubyArgs[i] = Java.wrap(runtime, obj); } IRubyObject result = rubyReceiver.callMethod(runtime.getCurrentContext(), method, rubyArgs); return rubyToJava(runtime, result, returnType); }
return convertArgument(runtime, value, value.getClass()); if ( isDuckTypeConvertable(rubyObject.getClass(), javaClass) ) { return convertProcToInterface(context, (RubyObject) rubyObject, javaClass);
public static RubyArray convertJavaArrayToRubyWithNesting(final ThreadContext context, final Object array) { final int length = Array.getLength(array); final IRubyObject[] rubyElements = new IRubyObject[length]; for ( int i = 0; i < length; i++ ) { final Object element = Array.get(array, i); if ( element instanceof ArrayJavaProxy ) { rubyElements[i] = convertJavaArrayToRubyWithNesting(context, ((ArrayJavaProxy) element).getObject()); } else if ( element != null && element.getClass().isArray() ) { rubyElements[i] = convertJavaArrayToRubyWithNesting(context, element); } else { rubyElements[i] = convertJavaToUsableRubyObject(context.runtime, element); } } return context.runtime.newArrayNoCopy(rubyElements); }
@Override public final IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) { try { return JavaUtil.convertJavaToUsableRubyObject(context.runtime, field.get(null)); } catch (IllegalAccessException ex) { return handleGetException(context.runtime, ex); } }
public static IRubyObject arefDirect(Ruby runtime, Object array, JavaUtil.JavaConverter javaConverter, int index) { try { return JavaUtil.convertJavaArrayElementToRuby(runtime, javaConverter, array, index); } catch (IndexOutOfBoundsException e) { throw mapIndexOutOfBoundsException(runtime, array, index); } }
private static RubyArray entriesCommon(Ruby runtime, String path) { String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path, null); checkDirIsTwoSlashesOnWindows(runtime, adjustedPath); Object[] files = getEntries(runtime, adjustedPath).toArray(); return runtime.newArrayNoCopy(JavaUtil.convertJavaArrayToRuby(runtime, files)); }
@JRubyMethod(name = { "to_a", "entries" }) // @override Enumerable#to_a public static IRubyObject to_a(final ThreadContext context, final IRubyObject self, final Block block) { final Ruby runtime = context.runtime; final RubyArray ary = runtime.newArray(); java.lang.Iterable iterable = unwrapIfJavaObject(self); java.util.Iterator iterator = iterable.iterator(); while ( iterator.hasNext() ) { final Object value = iterator.next(); ary.append( convertJavaToUsableRubyObject(runtime, value) ); } return ary; }
@JRubyMethod(name = { "to_a", "entries" }) public static RubyArray to_a(final ThreadContext context, final IRubyObject self) { final Object[] array = ((java.util.Collection) unwrapIfJavaObject(self)).toArray(); if ( IRubyObject.class.isAssignableFrom(array.getClass().getComponentType()) ) { return RubyArray.newArrayMayCopy(context.runtime, (IRubyObject[]) array); } return RubyArray.newArrayNoCopy(context.runtime, convertJavaArrayToRuby(context.runtime, array)); }
@JRubyMethod(name = { "to_a", "entries" }, alias = "to_ary") public RubyArray to_a(ThreadContext context) { final Object array = getObject(); return JavaUtil.convertJavaArrayToRubyWithNesting(context, array); }
public static <T> T convertProcToInterface(ThreadContext context, RubyObject rubyObject, Class<T> targetType) { return convertProcToInterface(context, (RubyBasicObject) rubyObject, targetType); }
/** * Build a Ruby name from a Java name by treating '_' as divider and successive * caps as all the same word. * @param javaCasedName * @return Ruby (under-score) cased named e.g. "get_foo_bar" */ public static String getRubyCasedName(final String javaCasedName) { final char[] javaName = javaCasedName.toCharArray(); final int len = javaName.length; final StringBuilder rubyName = new StringBuilder(len + 8); int behind = 0; for (int i = 0; i < len; i++) { if ( behind < 2 ) behind++; else behind = consume(rubyName, javaName, i); } if (behind == 2) { final char c1 = javaName[len - 1], c2 = javaName[len - 2]; rubyName.append( toLowerCase( c2 ) ); if ( isUpperCase( c1 ) && ! isUpperCase( c2 ) ) rubyName.append('_'); rubyName.append( toLowerCase( c1 ) ); } else if (behind > 0) { if ( behind > 1 ) { rubyName.append( toLowerCase( javaName[len - 2] ) ); } rubyName.append( toLowerCase( javaName[len - 1] ) ); } return rubyName.toString(); }
public static IRubyObject[] convertStringArrayToRuby(final Ruby runtime, final String[] strings, StringConverter converter) { if ( strings == null || strings.length == 0 ) return IRubyObject.NULL_ARRAY; IRubyObject[] rubyObjects = new IRubyObject[strings.length]; for (int i = 0; i < strings.length; i++) { rubyObjects[i] = convertJavaToUsableRubyObjectWithConverter(runtime, strings[i], converter); } return rubyObjects; }
/** * Convenience function for embedders * * @param runtime environment where the invoke will occur * @param receiver is the instance that will receive the method call * @param method is method to be called * @param args are the arguments to the method * @param returnType is the type we want it to conform to * @return the result of the invocation. */ public static Object invokeMethod(Ruby runtime, Object receiver, String method, Object[] args, Class returnType) { IRubyObject rubyReceiver = receiver != null ? JavaUtil.convertJavaToRuby(runtime, receiver) : runtime.getTopSelf(); IRubyObject[] rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args); // Create Ruby proxies for any input arguments that are not primitives. for (int i = 0; i < rubyArgs.length; i++) { IRubyObject obj = rubyArgs[i]; if (obj instanceof JavaObject) rubyArgs[i] = Java.wrap(runtime, obj); } IRubyObject result = rubyReceiver.callMethod(runtime.getCurrentContext(), method, rubyArgs); return rubyToJava(runtime, result, returnType); }
Object value = ((JavaObject) rubyObject).getValue(); return convertArgument(rubyObject.getRuntime(), value, value.getClass()); if (isDuckTypeConvertable(rubyObject.getClass(), javaClass)) { return convertProcToInterface(context, (RubyObject) rubyObject, javaClass);
@Deprecated public static Object coerceOtherToType(ThreadContext context, IRubyObject arg, Class target) { if (isDuckTypeConvertable(arg.getClass(), target)) { RubyObject rubyObject = (RubyObject) arg; if (!rubyObject.respondsTo("java_object")) { return convertProcToInterface(context, rubyObject, target); } } // it's either as converted as we can make it via above logic or it's // not one of the types we convert, so just pass it out as-is without wrapping return arg; }