private static String unwrapJavaString(IRubyObject arg) { if (arg instanceof JavaProxy) { Object str = ((JavaProxy) arg).getObject(); return str instanceof String ? (String) str : null; } return null; }
private static Object unwrapObject( final IRubyObject wrapped, final Object defaultValue) { if ( wrapped instanceof JavaObject ) { return ((JavaObject) wrapped).getValue(); } if ( wrapped instanceof JavaProxy ) { return ((JavaProxy) wrapped).getObject(); } return defaultValue; }
private Method getMethod(ThreadContext context, String name, Class... argTypes) { try { return getObject().getClass().getMethod(name, argTypes); } catch (NoSuchMethodException nsme) { throw JavaMethod.newMethodNotFoundError(context.runtime, getObject().getClass(), name + CodegenUtils.prettyParams(argTypes), name); } }
protected Object cloneObject() { final Object object = getObject(); if (object instanceof Cloneable) { // sufficient for java.util collection classes e.g. HashSet, ArrayList Object clone = JavaUtil.clone(object); return clone == null ? object : clone; } return object; // this is what JRuby did prior to <= 9.0.5 }
@Override @SuppressWarnings("unchecked") public <T> T toJava(Class<T> type) { final Object object = getObject(); final Class<?> clazz = object.getClass(); if ( type.isAssignableFrom(clazz) ) return type.cast(object); if ( type.isAssignableFrom(getClass()) ) return type.cast(this); // e.g. IRubyObject.class throw getRuntime().newTypeError("failed to coerce " + clazz.getName() + " to " + type.getName()); }
/** * Unwrap a wrapped Java object. * @param object * @return Java object * @see JavaUtil#isJavaObject(IRubyObject) */ public static <T> T unwrapJavaObject(final IRubyObject object) { if ( object instanceof JavaProxy ) { return (T) ((JavaProxy) object).getObject(); } return (T) ((JavaObject) object.dataGetStruct()).getValue(); }
@JRubyMethod public static IRubyObject hash(IRubyObject recv) { if (recv instanceof JavaProxy) { return RubyFixnum.newFixnum(recv.getRuntime(), ((JavaProxy) recv).getObject().hashCode()); } return ((JavaObject) recv.dataGetStruct()).hash(); }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) { if (javaVarargsCallables != null) return call(context, self, clazz, name, IRubyObject.NULL_ARRAY); JavaProxy proxy = castJavaProxy(self); JavaMethod method = (JavaMethod) findCallableArityZero(self, name); return method.invokeDirect(context, proxy.getObject()); }
@JRubyMethod public static IRubyObject clone(final ThreadContext context, final IRubyObject self) { java.util.Collection coll = unwrapIfJavaObject(self); final JavaProxy dup = (JavaProxy) ((RubyBasicObject) self).rbClone(); if ( coll == dup.getObject() && ! (coll instanceof Cloneable) ) { dup.setObject( tryNewEqualInstance(coll) ); } return dup; }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) { JavaProxy proxy = castJavaProxy(self); JavaMethod method = (JavaMethod) findCallable(self, name, args, args.length); return method.invokeDirect( context, proxy.getObject(), convertArguments(method, args) ); }
@JRubyMethod(name = "synchronized") public static IRubyObject rbSynchronized(ThreadContext context, IRubyObject recv, Block block) { if (recv instanceof JavaProxy) { return JavaObject.ruby_synchronized(context, ((JavaProxy) recv).getObject(), block); } return ((JavaObject) recv.dataGetStruct()).ruby_synchronized(context, block); } }
@JRubyMethod public IRubyObject java_send(ThreadContext context, IRubyObject rubyName) { String name = rubyName.asJavaString(); Ruby runtime = context.runtime; JavaMethod method = new JavaMethod(runtime, getMethod(context, name)); return method.invokeDirect(context, getObject()); }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) { if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0}); JavaProxy proxy = castJavaProxy(self); JavaMethod method = (JavaMethod) findCallableArityOne(self, name, arg0); final Class<?>[] paramTypes = method.getParameterTypes(); Object cArg0 = arg0.toJava(paramTypes[0]); return method.invokeDirect(context, proxy.getObject(), cArg0); }
@JRubyMethod public static IRubyObject to_s(IRubyObject recv) { if (recv instanceof JavaProxy) { return JavaObject.to_s(recv.getRuntime(), ((JavaProxy) recv).getObject()); } final Object unwrap = recv.dataGetStruct(); if (unwrap != null) return ((JavaObject) unwrap).to_s(); return ((RubyBasicObject) recv).to_s(); }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1) { if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1}); JavaProxy proxy = castJavaProxy(self); JavaMethod method = (JavaMethod) findCallableArityTwo(self, name, arg0, arg1); final Class<?>[] paramTypes = method.getParameterTypes(); Object cArg0 = arg0.toJava(paramTypes[0]); Object cArg1 = arg1.toJava(paramTypes[1]); return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1); }
@JRubyMethod public IRubyObject java_send(ThreadContext context, IRubyObject rubyName, IRubyObject argTypes) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); Ruby runtime = context.runtime; checkArgSizeMismatch(runtime, 0, argTypesAry); JavaMethod method = new JavaMethod(runtime, getMethod(context, name)); return method.invokeDirect(context, getObject()); }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1, arg2}); JavaProxy proxy = castJavaProxy(self); JavaMethod method = (JavaMethod) findCallableArityThree(self, name, arg0, arg1, arg2); final Class<?>[] paramTypes = method.getParameterTypes(); Object cArg0 = arg0.toJava(paramTypes[0]); Object cArg1 = arg1.toJava(paramTypes[1]); Object cArg2 = arg2.toJava(paramTypes[2]); return method.invokeDirect(context, proxy.getObject(), cArg0, cArg1, cArg2); }
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) { if (block.isGiven()) { JavaProxy proxy = castJavaProxy(self); RubyProc proc = RubyProc.newProc(context.runtime, block, block.type); JavaMethod method = (JavaMethod) findCallableArityOne(self, name, proc); final Class<?>[] paramTypes = method.getParameterTypes(); Object cArg0 = proc.toJava(paramTypes[0]); return method.invokeDirect(context, proxy.getObject(), cArg0); } return call(context, self, clazz, name); }
@JRubyMethod public IRubyObject java_send(ThreadContext context, IRubyObject rubyName, IRubyObject argTypes, IRubyObject arg0) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); Ruby runtime = context.runtime; checkArgSizeMismatch(runtime, 1, argTypesAry); Class argTypeClass = (Class) argTypesAry.eltInternal(0).toJava(Class.class); JavaMethod method = new JavaMethod(runtime, getMethod(context, name, argTypeClass)); return method.invokeDirect(context, getObject(), arg0.toJava(argTypeClass)); }