@Override public void dataWrapStruct(Object object) { this.javaObject = (JavaObject)object; this.object = javaObject.getValue(); }
@Override public final void dataWrapStruct(Object object) { this.javaObject = (JavaObject) object; this.object = javaObject.getValue(); }
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; }
/** * High-level object conversion utility function 'java_to_primitive' is the low-level version */ @Deprecated public static IRubyObject java_to_ruby(Ruby runtime, IRubyObject object) { if (object instanceof JavaObject) { return JavaUtil.convertJavaToUsableRubyObject(runtime, ((JavaObject) object).getValue()); } return object; }
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; }
@Override @SuppressWarnings("unchecked") public <T> T toJava(Class<T> target) { final Object value = getValue(); if ( value == null ) return null; if ( target.isAssignableFrom( value.getClass() ) ) { return target.cast(value); } return super.toJava(target); }
private static Object getWrappedObject(IRubyObject other, Object def) { if (other instanceof JavaObject) { return ((JavaObject)other).getValue(); } else if (other instanceof JavaProxy) { return ((JavaProxy)other).getObject(); } else { return def; } }
/** * Will return the Java interface that most closely can represent * this object, when working through JAva integration * translations. */ public Class getJavaClass() { Object obj = dataGetStruct(); if (obj instanceof JavaObject) { return ((JavaObject)obj).getValue().getClass(); } return getClass(); }
@Override public boolean equals(final Object other) { final Object otherValue; if ( other instanceof IRubyObject ) { otherValue = unwrapObject((IRubyObject) other, NEVER); } else { otherValue = other; } if ( otherValue == NEVER ) return false; return getValue() == otherValue; // TODO seems weird why not equals ?! }
@Override @SuppressWarnings("unchecked") public <T> T toJava(Class<T> target) { final Object value = getValue(); if ( value == null ) return null; if ( target.isAssignableFrom( value.getClass() ) ) { return target.cast(value); } return super.toJava(target); }
@Deprecated public static final <T> T convertParameter(IRubyObject parameter, Class<T> paramClass) { return paramClass.cast(parameter instanceof JavaObject ? ((JavaObject) parameter).getValue() : parameter.toJava(paramClass)); }
/** * High-level object conversion utility function 'java_to_primitive' is the low-level version */ @Deprecated public static IRubyObject java_to_ruby(Ruby runtime, IRubyObject object) { if (object instanceof JavaObject) { return JavaUtil.convertJavaToUsableRubyObject(runtime, ((JavaObject) object).getValue()); } return object; }
@Deprecated public static IRubyObject java_to_primitive(IRubyObject recv, IRubyObject object, Block unusedBlock) { if (object instanceof JavaObject) { return JavaUtil.convertJavaToRuby(recv.getRuntime(), ((JavaObject) object).getValue()); } return object; }
@JRubyMethod(name = "synchronized") public final IRubyObject ruby_synchronized(ThreadContext context, Block block) { final Object lock = getValue(); synchronized (lock != null ? lock : NULL_LOCK) { return block.yield(context, null); } }
/** * 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(name = {"==", "eql?"}, required = 1) public IRubyObject op_equal(IRubyObject other) { Object myValue = getValue(); return opEqualShared(myValue, other); }
@JRubyMethod(name = "equal?", required = 1) public IRubyObject same(final IRubyObject other) { final Ruby runtime = getRuntime(); final Object thisValue = getValue(); final Object otherValue = unwrapObject(other, NEVER); if ( otherValue == NEVER ) { // not a wrapped object return runtime.getFalse(); } if ( ! (other instanceof JavaObject) ) return runtime.getFalse(); return runtime.newBoolean(thisValue == otherValue); }
@JRubyMethod(name = "equal?", required = 1) public IRubyObject same(final IRubyObject other) { final Ruby runtime = getRuntime(); final Object thisValue = getValue(); final Object otherValue = unwrapObject(other, NEVER); if ( otherValue == NEVER ) { // not a wrapped object return runtime.getFalse(); } if ( ! (other instanceof JavaObject) ) return runtime.getFalse(); return runtime.newBoolean(thisValue == otherValue); }
public IRubyObject aset(IRubyObject index, IRubyObject value) { if (! (index instanceof RubyInteger)) { throw getRuntime().newTypeError(index, getRuntime().getInteger()); } int intIndex = (int) ((RubyInteger) index).getLongValue(); if (! (value instanceof JavaObject)) { throw getRuntime().newTypeError("not a java object:" + value); } Object javaObject = ((JavaObject) value).getValue(); ArrayUtils.setWithExceptionHandlingDirect(getRuntime(), javaObject, intIndex, javaObject); return value; }
public IRubyObject aset(IRubyObject index, IRubyObject value) { if (! (index instanceof RubyInteger)) { throw getRuntime().newTypeError(index, getRuntime().getInteger()); } int intIndex = (int) ((RubyInteger) index).getLongValue(); if (! (value instanceof JavaObject)) { throw getRuntime().newTypeError("not a java object:" + value); } Object javaObject = ((JavaObject) value).getValue(); ArrayUtils.setWithExceptionHandlingDirect(getRuntime(), javaObject, intIndex, javaObject); return value; }