Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2001 Alan Moore <alan_moore@gmx.net> Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2004 Thomas E Enebo <enebo@acm.org> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.javasupport;
 
 import org.jruby.Ruby;

Author(s):
jpetersen
 
 @JRubyClass(name="Java::JavaObject")
 public class JavaObject extends RubyObject {
 
     private static Object NULL_LOCK = new Object();
     private final RubyClass.VariableAccessor objectAccessor;
 
     protected JavaObject(Ruby runtimeRubyClass rubyClassObject value) {
         super(runtimerubyClass);
          = rubyClass.getVariableAccessorForWrite("__wrap_struct__");
         dataWrapStruct(value);
     }
 
     @Override
     public Object dataGetStruct() {
         return .get(this);
     }
 
     @Override
     public void dataWrapStruct(Object object) {
         .set(thisobject);
     }
 
     protected JavaObject(Ruby runtimeObject value) {
         this(runtimeruntime.getJavaSupport().getJavaObjectClass(), value);
     }
 
     public static JavaObject wrap(Ruby runtimeObject value) {
         if (value != null) {
             if (value instanceof Class) {
                 return JavaClass.get(runtime, (Class<?>) value);
             } else if (value.getClass().isArray()) {
                 return new JavaArray(runtimevalue);
             }
         }
         return new JavaObject(runtimevalue);
     }
 
     @JRubyMethod(meta = true)
    public static IRubyObject wrap(ThreadContext contextIRubyObject selfIRubyObject object) {
        Ruby runtime = context.runtime;
        Object obj = getWrappedObject(object);
        if (obj == return runtime.getNil();
        return wrap(runtimeobj);
    }
    @Override
    public Class<?> getJavaClass() {
        Object dataStruct = dataGetStruct();
        return dataStruct != null ? dataStruct.getClass() : .;
    }
    public Object getValue() {
        return dataGetStruct();
    }
    public static RubyClass createJavaObjectClass(Ruby runtimeRubyModule javaModule) {
        // FIXME: Ideally JavaObject instances should be marshallable, which means that
        // the JavaObject metaclass should have an appropriate allocator. JRUBY-414
        RubyClass result = javaModule.defineClassUnder("JavaObject"runtime.getObject(), );
        registerRubyMethods(runtimeresult);
        result.getMetaClass().undefineMethod("new");
        result.getMetaClass().undefineMethod("allocate");
        return result;
    }
    protected static void registerRubyMethods(Ruby runtimeRubyClass result) {
        result.defineAnnotatedMethods(JavaObject.class);
    }
    @Override
    public boolean equals(Object other) {
        Object myValue = getValue();
        Object otherValue = other;
        if (other instanceof IRubyObject) {
            otherValue = getWrappedObject((IRubyObject)other);
        }
        if (otherValue == ) {
            // not a wrapped object
            return false;
        }
        return myValue == otherValue;
    }
    @Override
    public int hashCode() {
        Object dataStruct = dataGetStruct();
        if (dataStruct != null) {
            return dataStruct.hashCode();
        }
        return 0;
    }
    @Override
    public RubyFixnum hash() {
        return getRuntime().newFixnum(hashCode());
    }
    @Override
    public IRubyObject to_s() {
        return to_s(getRuntime(), dataGetStruct());
    }
    public static IRubyObject to_s(Ruby runtimeObject dataStruct) {
        if (dataStruct != null) {
            String stringValue = dataStruct.toString();
            if (stringValue != null) {
                return RubyString.newUnicodeString(runtimedataStruct.toString());
            }
            return runtime.getNil();
        }
        return RubyString.newEmptyString(runtime);
    }
    @JRubyMethod(name = {"==""eql?"}, required = 1)
    public IRubyObject op_equal(IRubyObject other) {
        Object myValue = getValue();
        return opEqualShared(myValueother);
    }
    public static IRubyObject op_equal(JavaProxy selfIRubyObject other) {
        Object myValue = self.getObject();
        return opEqualShared(myValueother);
    }
    private static IRubyObject opEqualShared(Object myValueIRubyObject other) {
        Ruby runtime = other.getRuntime();
        Object otherValue = getWrappedObject(other);
        if (other == ) {
            // not a wrapped object
            return runtime.getFalse();
        }
        if (myValue == null && otherValue == null) {
            return runtime.getTrue();
        }
        return runtime.newBoolean(myValue.equals(otherValue));
    }
    @JRubyMethod(name = "equal?", required = 1)
    public IRubyObject same(IRubyObject other) {
        Ruby runtime = getRuntime();
        Object myValue = getValue();
        Object otherValue = getWrappedObject(other);
        if (other == ) {
            // not a wrapped object
            return runtime.getFalse();
        }
        if (myValue == null && otherValue == null) {
            return getRuntime().getTrue();
        }
        if (!(other instanceof JavaObject)) return runtime.getFalse();
        boolean isSame = getValue() == ((JavaObjectother).getValue();
        return isSame ? getRuntime().getTrue() : getRuntime().getFalse();
    }
    private static Object getWrappedObject(IRubyObject otherObject def) {
        if (other instanceof JavaObject) {
            return ((JavaObject)other).getValue();
        } else if (other instanceof JavaProxy) {
            return ((JavaProxy)other).getObject();
        } else {
            return def;
        }
    }
    public RubyString java_type() {
        return getRuntime().newString(getJavaClass().getName());
    }
    public IRubyObject java_class() {
        return JavaClass.get(getRuntime(), getJavaClass());
    }
    public RubyFixnum length() {
        throw getRuntime().newTypeError("not a java array");
    }
    @JRubyMethod(name = "[]", required = 1)
    public IRubyObject aref(IRubyObject index) {
        throw getRuntime().newTypeError("not a java array");
    }
    @JRubyMethod(name = "[]=", required = 2)
    public IRubyObject aset(IRubyObject indexIRubyObject someValue) {
        throw getRuntime().newTypeError("not a java array");
    }
    @JRubyMethod(name = "fill", required = 3)
    public IRubyObject afill(IRubyObject beginIndexIRubyObject endIndexIRubyObject someValue) {
        throw getRuntime().newTypeError("not a java array");
    }
    @JRubyMethod(name = "java_proxy?")
    public IRubyObject is_java_proxy() {
        return getRuntime().getTrue();
    }
    @JRubyMethod(name = "synchronized")
    public IRubyObject ruby_synchronized(ThreadContext contextBlock block) {
        Object lock = getValue();
        synchronized (lock != null ? lock : ) {
            return block.yield(contextnull);
        }
    }
    
    public static IRubyObject ruby_synchronized(ThreadContext contextObject lockBlock block) {
        synchronized (lock != null ? lock : ) {
            return block.yield(contextnull);
        }
    }
    
    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());
        }
    }
    public IRubyObject marshal_load(ThreadContext contextIRubyObject str) {
        try {
            ByteList byteList = str.convertToString().getByteList();
            ByteArrayInputStream bais = new ByteArrayInputStream(byteList.getUnsafeBytes(), byteList.getBegin(), byteList.getRealSize());
            ObjectInputStream ois = new JRubyObjectInputStream(context.runtimebais);
            dataWrapStruct(ois.readObject());
            return this;
        } catch (IOException ioe) {
            throw context.runtime.newIOErrorFromException(ioe);
        } catch (ClassNotFoundException cnfe) {
            throw context.runtime.newTypeError("Class not found unmarshaling Java type: " + cnfe.getLocalizedMessage());
        }
    }
    @Override
    public Object toJava(Class cls) {
        if (getValue() == null) {
            // THIS SHOULD NEVER HAPPEN, but it DOES
            return getValue();
        }
        
        if (cls.isAssignableFrom(getValue().getClass())) {
            return getValue();
        }
        
        return super.toJava(cls);
    }
    private static final ObjectAllocator JAVA_OBJECT_ALLOCATOR = new ObjectAllocator() {
        public IRubyObject allocate(Ruby runtimeRubyClass klazz) {
            return new JavaObject(runtimeklazznull);
        }
    };
New to GrepCode? Check out our FAQ X