/** * Because we can't physically associate an ID with a Java object, we can * only use the identity hashcode here. * * @return The identity hashcode for the Java object. */ @Override public IRubyObject id() { return getRuntime().newFixnum(System.identityHashCode(getObject())); }
/** * Because we can't physically associate an ID with a Java object, we can * only use the identity hashcode here. * * @return The identity hashcode for the Java object. */ public IRubyObject id() { return getRuntime().newFixnum(System.identityHashCode(getObject())); }
/** * Because we can't physically associate an ID with a Java object, we can * only use the identity hashcode here. * * @return The identity hashcode for the Java object. */ @Override public IRubyObject id() { return getRuntime().newFixnum(System.identityHashCode(getObject())); }
/** * Because we can't physically associate an ID with a Java object, we can * only use the identity hashcode here. * * @return The identity hashcode for the Java object. */ public IRubyObject id() { return getRuntime().newFixnum(System.identityHashCode(getObject())); }
@Override @SuppressWarnings("unchecked") public <T> T toJava(Class<T> type) { final Object object = getObject(); final Class clazz = object.getClass(); if ( type.isPrimitive() ) { if ( type == Void.TYPE ) return null; if ( object instanceof Number && type != Boolean.TYPE || object instanceof Character && type == Character.TYPE || object instanceof Boolean && type == Boolean.TYPE ) { // FIXME in more permissive call paths, like invokedynamic, this can allow // precision-loading downcasts to happen silently return (T) object; } } else if ( type.isAssignableFrom(clazz) ) { if ( Java.OBJECT_PROXY_CACHE || metaClass.getCacheProxy() ) { getRuntime().getJavaSupport().getObjectProxyCache().put(object, this); } return type.cast(object); } else if ( type.isAssignableFrom(getClass()) ) return type.cast(this); // e.g. IRubyObject.class throw getRuntime().newTypeError("failed to coerce " + clazz.getName() + " to " + type.getName()); } }
@Override @SuppressWarnings("unchecked") public <T> T toJava(Class<T> type) { final Object object = getObject(); final Class clazz = object.getClass(); if ( type.isPrimitive() ) { if ( type == Void.TYPE ) return null; if ( object instanceof Number && type != Boolean.TYPE || object instanceof Character && type == Character.TYPE || object instanceof Boolean && type == Boolean.TYPE ) { // FIXME in more permissive call paths, like invokedynamic, this can allow // precision-loading downcasts to happen silently return (T) object; } } else if ( type.isAssignableFrom(clazz) ) { if ( Java.OBJECT_PROXY_CACHE || metaClass.getCacheProxy() ) { getRuntime().getJavaSupport().getObjectProxyCache().put(object, this); } return type.cast(object); } else if ( type.isAssignableFrom(getClass()) ) return type.cast(this); // e.g. IRubyObject.class throw getRuntime().newTypeError("failed to coerce " + clazz.getName() + " to " + type.getName()); } }
@Override public Object toJava(Class type) { Object obj = getObject(); Class cls = obj.getClass(); if (type.isPrimitive()) { if (type == Void.TYPE) return null; if (obj instanceof Number && type != Boolean.TYPE || obj instanceof Character && type == Character.TYPE || obj instanceof Boolean && type == Boolean.TYPE) { // FIXME in more permissive call paths, like invokedynamic, this can allow // precision-loading downcasts to happen silently return obj; } } else if (type.isAssignableFrom(cls)) { if (Java.OBJECT_PROXY_CACHE || metaClass.getCacheProxy()) { getRuntime().getJavaSupport().getObjectProxyCache().put(obj, this); } return obj; } throw getRuntime().newTypeError("failed to coerce " + cls.getName() + " to " + type.getName()); } }
@Override public Object toJava(Class type) { Object obj = getObject(); Class cls = obj.getClass(); if (type.isPrimitive()) { if (type == Void.TYPE) return null; if (obj instanceof Number && type != Boolean.TYPE || obj instanceof Character && type == Character.TYPE || obj instanceof Boolean && type == Boolean.TYPE) { // FIXME in more permissive call paths, like invokedynamic, this can allow // precision-loading downcasts to happen silently return obj; } } else if (type.isAssignableFrom(cls)) { if (Java.OBJECT_PROXY_CACHE || metaClass.getCacheProxy()) { getRuntime().getJavaSupport().getObjectProxyCache().put(obj, this); } return obj; } throw getRuntime().newTypeError("failed to coerce " + cls.getName() + " to " + type.getName()); } }
@JRubyMethod(name = "===", meta = true) public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) { Ruby runtime = context.runtime; // special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception if (other instanceof ConcreteJavaProxy && (recv == runtime.getException() || recv == runtime.getStandardError())) { Object object = ((ConcreteJavaProxy)other).getObject(); if (object instanceof Throwable && !(object instanceof FlowControlException)) { return context.runtime.getTrue(); } } // fall back on default logic return ((RubyClass)recv).op_eqq(context, other); }
@JRubyMethod(name = "===", meta = true) public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) { Ruby runtime = context.runtime; // special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception if (other instanceof ConcreteJavaProxy && (recv == runtime.getException() || recv == runtime.getStandardError())) { Object object = ((ConcreteJavaProxy)other).getObject(); if (object instanceof Throwable && !(object instanceof FlowControlException)) { return context.runtime.getTrue(); } } // fall back on default logic return ((RubyClass)recv).op_eqq(context, other); }
@JRubyMethod(name = "===", meta = true) public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) { Ruby runtime = context.runtime; // special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception if (other instanceof ConcreteJavaProxy && (recv == runtime.getException() || recv == runtime.getStandardError())) { Object object = ((ConcreteJavaProxy)other).getObject(); if (object instanceof Throwable && !(object instanceof FlowControlException)) { if (recv == runtime.getException() || object instanceof java.lang.Exception) { return context.tru; } } } // fall back on default logic return ((RubyClass)recv).op_eqq(context, other); }
@JRubyMethod(name = "===", meta = true) public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) { Ruby runtime = context.runtime; // special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception if (other instanceof ConcreteJavaProxy && (recv == runtime.getException() || recv == runtime.getStandardError())) { Object object = ((ConcreteJavaProxy)other).getObject(); if (object instanceof Throwable && !(object instanceof FlowControlException)) { if (recv == runtime.getException() || object instanceof java.lang.Exception) { return context.tru; } } } // fall back on default logic return ((RubyClass)recv).op_eqq(context, other); }
private static void maybeRaiseJavaException(final Ruby runtime, final IRubyObject[] args, final int argc, final IRubyObject cause) { // Check for a Java exception ConcreteJavaProxy exception = null; switch (argc) { case 0: IRubyObject lastException = runtime.getGlobalVariables().get("$!"); if (lastException instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy) lastException; } break; case 1: if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy) args[0]; } break; } if (exception != null) { // looks like someone's trying to raise a Java exception. Let them. Object maybeThrowable = exception.getObject(); if (!(maybeThrowable instanceof Throwable)) { throw runtime.newTypeError("can't raise a non-Throwable Java object"); } final Throwable ex = (Throwable) maybeThrowable; Helpers.throwException(ex); return; // not reached } }
private static void maybeRaiseJavaException(final Ruby runtime, final IRubyObject[] args, final int argc, final IRubyObject cause) { // Check for a Java exception ConcreteJavaProxy exception = null; switch (argc) { case 0: IRubyObject lastException = runtime.getGlobalVariables().get("$!"); if (lastException instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy) lastException; } break; case 1: if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy) args[0]; } break; } if (exception != null) { // looks like someone's trying to raise a Java exception. Let them. Object maybeThrowable = exception.getObject(); if (!(maybeThrowable instanceof Throwable)) { throw runtime.newTypeError("can't raise a non-Throwable Java object"); } final Throwable ex = (Throwable) maybeThrowable; Helpers.throwException(ex); return; // not reached } }
@JRubyMethod(name = "<=>", required = 1) public IRubyObject op_cmp(IRubyObject other) { Class me = javaClass(); Class them = null; // dig out the other class if (other instanceof JavaClass) { JavaClass otherClass = (JavaClass) other; them = otherClass.javaClass(); } else if (other instanceof ConcreteJavaProxy) { ConcreteJavaProxy proxy = (ConcreteJavaProxy)other; if (proxy.getObject() instanceof Class) { them = (Class)proxy.getObject(); } } if (them != null) { if (this.javaClass() == them) { return getRuntime().newFixnum(0); } if (them.isAssignableFrom(me)) { return getRuntime().newFixnum(-1); } if (me.isAssignableFrom(them)) { return getRuntime().newFixnum(1); } } // can't do a comparison return getRuntime().getNil(); }
@JRubyMethod(name = "<=>", required = 1) public IRubyObject op_cmp(IRubyObject other) { Class me = javaClass(); Class them = null; // dig out the other class if (other instanceof JavaClass) { JavaClass otherClass = (JavaClass) other; them = otherClass.javaClass(); } else if (other instanceof ConcreteJavaProxy) { ConcreteJavaProxy proxy = (ConcreteJavaProxy)other; if (proxy.getObject() instanceof Class) { them = (Class)proxy.getObject(); } } if (them != null) { if (this.javaClass() == them) { return getRuntime().newFixnum(0); } if (them.isAssignableFrom(me)) { return getRuntime().newFixnum(-1); } if (me.isAssignableFrom(them)) { return getRuntime().newFixnum(1); } } // can't do a comparison return getRuntime().getNil(); }
@JRubyMethod(name = "<=>", required = 1) public IRubyObject op_cmp(IRubyObject other) { final Class<?> thisClass = javaClass(); Class<?> otherClass = null; // dig out the other class if (other instanceof JavaClass) { otherClass = ( (JavaClass) other ).javaClass(); } else if (other instanceof ConcreteJavaProxy) { ConcreteJavaProxy proxy = (ConcreteJavaProxy) other; final Object wrapped = proxy.getObject(); if ( wrapped instanceof Class ) { otherClass = (Class) wrapped; } } if ( otherClass != null ) { if ( thisClass == otherClass ) { return getRuntime().newFixnum(0); } if ( otherClass.isAssignableFrom(thisClass) ) { return getRuntime().newFixnum(-1); } if ( thisClass.isAssignableFrom(otherClass) ) { return getRuntime().newFixnum(1); } } // can't do a comparison return getRuntime().getNil(); }
@JRubyMethod(name = "<=>", required = 1) public IRubyObject op_cmp(IRubyObject other) { final Class<?> thisClass = javaClass(); Class<?> otherClass = null; // dig out the other class if (other instanceof JavaClass) { otherClass = ( (JavaClass) other ).javaClass(); } else if (other instanceof ConcreteJavaProxy) { ConcreteJavaProxy proxy = (ConcreteJavaProxy) other; final Object wrapped = proxy.getObject(); if ( wrapped instanceof Class ) { otherClass = (Class) wrapped; } } if ( otherClass != null ) { if ( thisClass == otherClass ) { return getRuntime().newFixnum(0); } if ( otherClass.isAssignableFrom(thisClass) ) { return getRuntime().newFixnum(-1); } if ( thisClass.isAssignableFrom(otherClass) ) { return getRuntime().newFixnum(1); } } // can't do a comparison return getRuntime().getNil(); }
@JRubyMethod(name = "raise", optional = 3, module = true, visibility = Visibility.PRIVATE, omit = true) public static IRubyObject rbRaise(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { Ruby runtime = context.runtime; // Check for a Java exception ConcreteJavaProxy exception = null; if (args.length == 0 && runtime.getGlobalVariables().get("$!") instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy)runtime.getGlobalVariables().get("$!"); } else if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy)args[0]; } if (exception != null) { // looks like someone's trying to raise a Java exception. Let them. Object maybeThrowable = exception.getObject(); if (maybeThrowable instanceof Throwable) { // yes, we're cheating here. Helpers.throwException((Throwable)maybeThrowable); return recv; // not reached } else { throw runtime.newTypeError("can't raise a non-Throwable Java object"); } } else { return RubyKernel.raise(context, recv, args, block); } }
@JRubyMethod(name = "raise", optional = 3, module = true, visibility = Visibility.PRIVATE, omit = true) public static IRubyObject rbRaise(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { Ruby runtime = context.runtime; // Check for a Java exception ConcreteJavaProxy exception = null; if (args.length == 0 && runtime.getGlobalVariables().get("$!") instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy)runtime.getGlobalVariables().get("$!"); } else if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) { exception = (ConcreteJavaProxy)args[0]; } if (exception != null) { // looks like someone's trying to raise a Java exception. Let them. Object maybeThrowable = exception.getObject(); if (maybeThrowable instanceof Throwable) { // yes, we're cheating here. Helpers.throwException((Throwable)maybeThrowable); return recv; // not reached } else { throw runtime.newTypeError("can't raise a non-Throwable Java object"); } } else { return RubyKernel.raise(context, recv, args, block); } }