public IRubyObject allocate(Ruby runtime, RubyClass klazz) { return new JavaObject(runtime, klazz, null); } };
@JRubyMethod @Override public IRubyObject to_s() { return to_s(getRuntime(), dataGetStruct()); }
protected JavaObject(Ruby runtime, RubyClass rubyClass, Object value) { super(runtime, rubyClass); objectAccessor = rubyClass.getVariableAccessorForWrite("__wrap_struct__"); dataWrapStruct(value); }
@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 ?! }
@JRubyMethod(name = {"==", "eql?"}, required = 1) public IRubyObject op_equal(final IRubyObject other) { return equals(getRuntime(), getValue(), other); }
@JRubyMethod public RubyString java_type() { return getRuntime().newString(getJavaClass().getName()); }
@Override public final void dataWrapStruct(Object object) { this.javaObject = (JavaObject) object; this.object = javaObject.getValue(); }
@JRubyMethod public IRubyObject marshal_dump() { if (Serializable.class.isAssignableFrom(getJavaClass())) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(getValue()); return getRuntime().newString(new ByteList(baos.toByteArray())); } catch (IOException ioe) { throw getRuntime().newIOErrorFromException(ioe); } } else { throw getRuntime().newTypeError("no marshal_dump is defined for class " + getJavaClass()); } }
@JRubyMethod @Override public RubyFixnum hash() { return getRuntime().newFixnum(hashCode()); }
@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 Object getValue() { return dataGetStruct(); }
@JRubyMethod public RubyFixnum length() { throw getRuntime().newTypeError("not a java array"); }
@JRubyMethod public IRubyObject marshal_dump(ThreadContext context) { if (Serializable.class.isAssignableFrom(getJavaClass())) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(getValue()); return context.runtime.newString(new ByteList(baos.toByteArray(), false)); } catch (IOException ex) { throw context.runtime.newIOErrorFromException(ex); } } throw context.runtime.newTypeError("no marshal_dump is defined for class " + getJavaClass()); }
@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(); }
@JRubyMethod(name = {"==", "eql?"}, required = 1) public IRubyObject op_equal(IRubyObject other) { Object myValue = getValue(); return opEqualShared(myValue, other); }
JavaObject.createJavaObjectClass(runtime, javaModule); JavaArray.createJavaArrayClass(runtime, javaModule); JavaClass.createJavaClassClass(runtime, javaModule);
@Override public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) { final Object wrapped = self.dataGetStruct(); final Class<?> javaClass; if ( wrapped != null ) { javaClass = ((JavaObject) wrapped).getJavaClass(); } else { javaClass = self.getClass(); // NOTE what is this for? } return JavaClass.get(context.runtime, javaClass); }
@JRubyMethod public RubyString java_type() { return getRuntime().newString(getJavaClass().getName()); }
@Override public void dataWrapStruct(Object object) { this.javaObject = (JavaObject)object; this.object = javaObject.getValue(); }