001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import org.jscripter.emu.java.*;
023 
024 import js.Js;
025 
026 /**
027  * <p>The <b>internal</b> root of the Java class hierarchy, emulating a standard <tt>java.lang</tt> 
028  * interface or class with the same simple name as this one.</p>
029  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
030  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
031  * 
032  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
033  * 
034  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
035  */
036 public class Object
037 {
038     /**
039      * <p>Internally constructs a new {@link Object}.</p>
040      * @since 1.0
041      * @javascript Re-compilers must report error on end-users directly using this constructor.
042      */
043     public Object() {}
044 
045     /**
046      * <p>Returns the runtime class of this {@link Object}.</p>
047      * <p>Note that for the emulation, {@link Object}s can be anything including primitive values.</p>
048      * @return The object after casting, or <tt>null</tt> if <tt>obj</tt> is <tt>null</tt>.
049      * @since 1.0
050      * @javascript Re-compilers must report error on end-users directly using this method.
051      * It must redirect resolution of <tt>java.lang.Object.getClass()</tt> method to this one.
052      */
053     public final java.lang.Class<?> getJavaClass() {
054         return (java.lang.Class<?>)(java.lang.Object)JavaClass.getClass(this);
055     }
056 
057     /**
058      * <p>Creates and returns a copy of this object.</p>
059      * <p>Note that for the emulation, {@link Object}s can be anything including primitive values.</p>
060      * @return A clone of this instance.
061      * @throws java.lang.CloneNotSupportedException if the object's class does not support the 
062      * {@link Cloneable} interface. Subclasses that override the {@link #clone()} method can also 
063      * throw this exception to indicate that an instance cannot be cloned.
064      * @since 1.0
065      * @javascript Re-compilers must report error on end-users directly using this method.
066      */
067     @Override
068     protected java.lang.Object clone() throws java.lang.CloneNotSupportedException {
069         return Js.cond(
070                 (java.lang.Object)this instanceof JavaObject,
071                 ((JavaObject)(java.lang.Object)this).clone(), 
072                 JavaObject.clone(this)
073         );
074     }
075 
076     /**
077      * <p>Indicates whether some other object is "equal to" this one.</p>
078      * <p>Note that for the emulation, {@link Object}s can be anything including primitive values.</p>
079      * @param obj The object to compare with.
080      * @return <tt>true</tt>if this object is the same as the <tt>obj</tt> argument; <tt>false</tt> otherwise.
081      * @since 1.0
082      * @javascript Re-compilers must report error on end-users directly using this method.
083      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
084      */
085     @Override
086     public boolean equals(java.lang.Object obj) {
087         return Js.cond(
088                 (java.lang.Object)this instanceof JavaObject,
089                 ((JavaObject)(java.lang.Object)this).equals(obj), 
090                 Js.eq(this, obj)
091         );
092     }
093 
094     /**
095      * <p>Returns a hash code for this {@link Object}.</p>
096      * <p>Note that for the emulation, {@link Object}s can be anything including primitive values.</p>
097      * @return A hash code value for this {@link Object}.
098      * @since 1.0
099      * @javascript Re-compilers must report error on end-users directly using this method.
100      * For efficiency, the invocation of this instance emulation method with single statement can be in-lined in re-compilation.
101      */
102     @Override
103     public int hashCode() {
104         return ((java.lang.Integer)Js.cond(
105                 (java.lang.Object)this instanceof JavaObject,
106                 ((JavaObject)(java.lang.Object)this).hashCode(), 
107                 System.identityHashCode(this)
108         )).intValue();
109     }
110 
111     /**
112      * <p>Returns a string representing this {@link Object}'s value.</p>
113      * <p>Note that for the emulation, {@link Object}s can be anything including primitive values.</p>
114      * @return A string representation of this {@link Object}.
115      * @since 1.0
116      * @javascript Re-compilers must report error on end-users directly using this method.
117      */
118     @Override
119     public java.lang.String toString() {
120         return Js.cond(
121                 (java.lang.Object)this instanceof JavaObject,
122                 ((JavaObject)(java.lang.Object)this).toString(), 
123                 Js.toString(this)
124         );
125     }
126 }