2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
29 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
32 * Class {@code Object} is the root of the class hierarchy.
33 * Every class has {@code Object} as a superclass. All objects,
34 * including arrays, implement the methods of this class.
37 * @see java.lang.Class
40 @JavaScriptPrototype(container = "Object.prototype", prototype = "new Object")
43 Class.registerNatives();
44 Class.registerToString();
48 * Returns the runtime class of this {@code Object}. The returned
49 * {@code Class} object is the object that is locked by {@code
50 * static synchronized} methods of the represented class.
52 * <p><b>The actual result type is {@code Class<? extends |X|>}
53 * where {@code |X|} is the erasure of the static type of the
54 * expression on which {@code getClass} is called.</b> For
55 * example, no cast is required in this code fragment:</p>
58 * {@code Number n = 0; }<br>
59 * {@code Class<? extends Number> c = n.getClass(); }
62 * @return The {@code Class} object that represents the runtime
63 * class of this object.
64 * @see Class Literals, section 15.8.2 of
65 * <cite>The Java™ Language Specification</cite>.
67 public final Class<?> getClass() {
68 Class<?> c = Class.classFor(this);
69 return c == null ? Object.class : c;
73 * Returns a hash code value for the object. This method is
74 * supported for the benefit of hash tables such as those provided by
75 * {@link java.util.HashMap}.
77 * The general contract of {@code hashCode} is:
79 * <li>Whenever it is invoked on the same object more than once during
80 * an execution of a Java application, the {@code hashCode} method
81 * must consistently return the same integer, provided no information
82 * used in {@code equals} comparisons on the object is modified.
83 * This integer need not remain consistent from one execution of an
84 * application to another execution of the same application.
85 * <li>If two objects are equal according to the {@code equals(Object)}
86 * method, then calling the {@code hashCode} method on each of
87 * the two objects must produce the same integer result.
88 * <li>It is <em>not</em> required that if two objects are unequal
89 * according to the {@link java.lang.Object#equals(java.lang.Object)}
90 * method, then calling the {@code hashCode} method on each of the
91 * two objects must produce distinct integer results. However, the
92 * programmer should be aware that producing distinct integer results
93 * for unequal objects may improve the performance of hash tables.
96 * As much as is reasonably practical, the hashCode method defined by
97 * class {@code Object} does return distinct integers for distinct
98 * objects. (This is typically implemented by converting the internal
99 * address of the object into an integer, but this implementation
100 * technique is not required by the
101 * Java<font size="-2"><sup>TM</sup></font> programming language.)
103 * @return a hash code value for this object.
104 * @see java.lang.Object#equals(java.lang.Object)
105 * @see java.lang.System#identityHashCode
107 public int hashCode() {
108 return Class.defaultHashCode(this);
112 * Indicates whether some other object is "equal to" this one.
114 * The {@code equals} method implements an equivalence relation
115 * on non-null object references:
117 * <li>It is <i>reflexive</i>: for any non-null reference value
118 * {@code x}, {@code x.equals(x)} should return
120 * <li>It is <i>symmetric</i>: for any non-null reference values
121 * {@code x} and {@code y}, {@code x.equals(y)}
122 * should return {@code true} if and only if
123 * {@code y.equals(x)} returns {@code true}.
124 * <li>It is <i>transitive</i>: for any non-null reference values
125 * {@code x}, {@code y}, and {@code z}, if
126 * {@code x.equals(y)} returns {@code true} and
127 * {@code y.equals(z)} returns {@code true}, then
128 * {@code x.equals(z)} should return {@code true}.
129 * <li>It is <i>consistent</i>: for any non-null reference values
130 * {@code x} and {@code y}, multiple invocations of
131 * {@code x.equals(y)} consistently return {@code true}
132 * or consistently return {@code false}, provided no
133 * information used in {@code equals} comparisons on the
134 * objects is modified.
135 * <li>For any non-null reference value {@code x},
136 * {@code x.equals(null)} should return {@code false}.
139 * The {@code equals} method for class {@code Object} implements
140 * the most discriminating possible equivalence relation on objects;
141 * that is, for any non-null reference values {@code x} and
142 * {@code y}, this method returns {@code true} if and only
143 * if {@code x} and {@code y} refer to the same object
144 * ({@code x == y} has the value {@code true}).
146 * Note that it is generally necessary to override the {@code hashCode}
147 * method whenever this method is overridden, so as to maintain the
148 * general contract for the {@code hashCode} method, which states
149 * that equal objects must have equal hash codes.
151 * @param obj the reference object with which to compare.
152 * @return {@code true} if this object is the same as the obj
153 * argument; {@code false} otherwise.
155 * @see java.util.HashMap
157 public boolean equals(Object obj) {
158 return (this == obj);
162 * Creates and returns a copy of this object. The precise meaning
163 * of "copy" may depend on the class of the object. The general
164 * intent is that, for any object {@code x}, the expression:
167 * x.clone() != x</pre></blockquote>
168 * will be true, and that the expression:
171 * x.clone().getClass() == x.getClass()</pre></blockquote>
172 * will be {@code true}, but these are not absolute requirements.
173 * While it is typically the case that:
176 * x.clone().equals(x)</pre></blockquote>
177 * will be {@code true}, this is not an absolute requirement.
179 * By convention, the returned object should be obtained by calling
180 * {@code super.clone}. If a class and all of its superclasses (except
181 * {@code Object}) obey this convention, it will be the case that
182 * {@code x.clone().getClass() == x.getClass()}.
184 * By convention, the object returned by this method should be independent
185 * of this object (which is being cloned). To achieve this independence,
186 * it may be necessary to modify one or more fields of the object returned
187 * by {@code super.clone} before returning it. Typically, this means
188 * copying any mutable objects that comprise the internal "deep structure"
189 * of the object being cloned and replacing the references to these
190 * objects with references to the copies. If a class contains only
191 * primitive fields or references to immutable objects, then it is usually
192 * the case that no fields in the object returned by {@code super.clone}
193 * need to be modified.
195 * The method {@code clone} for class {@code Object} performs a
196 * specific cloning operation. First, if the class of this object does
197 * not implement the interface {@code Cloneable}, then a
198 * {@code CloneNotSupportedException} is thrown. Note that all arrays
199 * are considered to implement the interface {@code Cloneable} and that
200 * the return type of the {@code clone} method of an array type {@code T[]}
201 * is {@code T[]} where T is any reference or primitive type.
202 * Otherwise, this method creates a new instance of the class of this
203 * object and initializes all its fields with exactly the contents of
204 * the corresponding fields of this object, as if by assignment; the
205 * contents of the fields are not themselves cloned. Thus, this method
206 * performs a "shallow copy" of this object, not a "deep copy" operation.
208 * The class {@code Object} does not itself implement the interface
209 * {@code Cloneable}, so calling the {@code clone} method on an object
210 * whose class is {@code Object} will result in throwing an
211 * exception at run time.
213 * @return a clone of this instance.
214 * @exception CloneNotSupportedException if the object's class does not
215 * support the {@code Cloneable} interface. Subclasses
216 * that override the {@code clone} method can also
217 * throw this exception to indicate that an instance cannot
219 * @see java.lang.Cloneable
221 protected Object clone() throws CloneNotSupportedException {
222 Object ret = Class.clone(this);
224 throw new CloneNotSupportedException(getClass().getName());
230 * Returns a string representation of the object. In general, the
231 * {@code toString} method returns a string that
232 * "textually represents" this object. The result should
233 * be a concise but informative representation that is easy for a
235 * It is recommended that all subclasses override this method.
237 * The {@code toString} method for class {@code Object}
238 * returns a string consisting of the name of the class of which the
239 * object is an instance, the at-sign character `{@code @}', and
240 * the unsigned hexadecimal representation of the hash code of the
241 * object. In other words, this method returns a string equal to the
245 * getClass().getName() + '@' + Integer.toHexString(hashCode())
246 * </pre></blockquote>
248 * @return a string representation of the object.
250 public String toString() {
251 return getClass().getName() + "@" + Integer.toHexString(hashCode());
255 * Wakes up a single thread that is waiting on this object's
256 * monitor. If any threads are waiting on this object, one of them
257 * is chosen to be awakened. The choice is arbitrary and occurs at
258 * the discretion of the implementation. A thread waits on an object's
259 * monitor by calling one of the {@code wait} methods.
261 * The awakened thread will not be able to proceed until the current
262 * thread relinquishes the lock on this object. The awakened thread will
263 * compete in the usual manner with any other threads that might be
264 * actively competing to synchronize on this object; for example, the
265 * awakened thread enjoys no reliable privilege or disadvantage in being
266 * the next thread to lock this object.
268 * This method should only be called by a thread that is the owner
269 * of this object's monitor. A thread becomes the owner of the
270 * object's monitor in one of three ways:
272 * <li>By executing a synchronized instance method of that object.
273 * <li>By executing the body of a {@code synchronized} statement
274 * that synchronizes on the object.
275 * <li>For objects of type {@code Class,} by executing a
276 * synchronized static method of that class.
279 * Only one thread at a time can own an object's monitor.
281 * @exception IllegalMonitorStateException if the current thread is not
282 * the owner of this object's monitor.
283 * @see java.lang.Object#notifyAll()
284 * @see java.lang.Object#wait()
286 public final void notify() {
290 * Wakes up all threads that are waiting on this object's monitor. A
291 * thread waits on an object's monitor by calling one of the
292 * {@code wait} methods.
294 * The awakened threads will not be able to proceed until the current
295 * thread relinquishes the lock on this object. The awakened threads
296 * will compete in the usual manner with any other threads that might
297 * be actively competing to synchronize on this object; for example,
298 * the awakened threads enjoy no reliable privilege or disadvantage in
299 * being the next thread to lock this object.
301 * This method should only be called by a thread that is the owner
302 * of this object's monitor. See the {@code notify} method for a
303 * description of the ways in which a thread can become the owner of
306 * @exception IllegalMonitorStateException if the current thread is not
307 * the owner of this object's monitor.
308 * @see java.lang.Object#notify()
309 * @see java.lang.Object#wait()
311 public final void notifyAll() {
315 * Causes the current thread to wait until either another thread invokes the
316 * {@link java.lang.Object#notify()} method or the
317 * {@link java.lang.Object#notifyAll()} method for this object, or a
318 * specified amount of time has elapsed.
320 * The current thread must own this object's monitor.
322 * This method causes the current thread (call it <var>T</var>) to
323 * place itself in the wait set for this object and then to relinquish
324 * any and all synchronization claims on this object. Thread <var>T</var>
325 * becomes disabled for thread scheduling purposes and lies dormant
326 * until one of four things happens:
328 * <li>Some other thread invokes the {@code notify} method for this
329 * object and thread <var>T</var> happens to be arbitrarily chosen as
330 * the thread to be awakened.
331 * <li>Some other thread invokes the {@code notifyAll} method for this
333 * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
334 * thread <var>T</var>.
335 * <li>The specified amount of real time has elapsed, more or less. If
336 * {@code timeout} is zero, however, then real time is not taken into
337 * consideration and the thread simply waits until notified.
339 * The thread <var>T</var> is then removed from the wait set for this
340 * object and re-enabled for thread scheduling. It then competes in the
341 * usual manner with other threads for the right to synchronize on the
342 * object; once it has gained control of the object, all its
343 * synchronization claims on the object are restored to the status quo
344 * ante - that is, to the situation as of the time that the {@code wait}
345 * method was invoked. Thread <var>T</var> then returns from the
346 * invocation of the {@code wait} method. Thus, on return from the
347 * {@code wait} method, the synchronization state of the object and of
348 * thread {@code T} is exactly as it was when the {@code wait} method
351 * A thread can also wake up without being notified, interrupted, or
352 * timing out, a so-called <i>spurious wakeup</i>. While this will rarely
353 * occur in practice, applications must guard against it by testing for
354 * the condition that should have caused the thread to be awakened, and
355 * continuing to wait if the condition is not satisfied. In other words,
356 * waits should always occur in loops, like this one:
358 * synchronized (obj) {
359 * while (<condition does not hold>)
361 * ... // Perform action appropriate to condition
364 * (For more information on this topic, see Section 3.2.3 in Doug Lea's
365 * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
366 * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
367 * Language Guide" (Addison-Wesley, 2001).
369 * <p>If the current thread is {@linkplain java.lang.Thread#interrupt()
370 * interrupted} by any thread before or while it is waiting, then an
371 * {@code InterruptedException} is thrown. This exception is not
372 * thrown until the lock status of this object has been restored as
376 * Note that the {@code wait} method, as it places the current thread
377 * into the wait set for this object, unlocks only this object; any
378 * other objects on which the current thread may be synchronized remain
379 * locked while the thread waits.
381 * This method should only be called by a thread that is the owner
382 * of this object's monitor. See the {@code notify} method for a
383 * description of the ways in which a thread can become the owner of
386 * @param timeout the maximum time to wait in milliseconds.
387 * @exception IllegalArgumentException if the value of timeout is
389 * @exception IllegalMonitorStateException if the current thread is not
390 * the owner of the object's monitor.
391 * @exception InterruptedException if any thread interrupted the
392 * current thread before or while the current thread
393 * was waiting for a notification. The <i>interrupted
394 * status</i> of the current thread is cleared when
395 * this exception is thrown.
396 * @see java.lang.Object#notify()
397 * @see java.lang.Object#notifyAll()
399 public final void wait(long timeout) throws InterruptedException {
400 throw new InterruptedException();
404 * Causes the current thread to wait until another thread invokes the
405 * {@link java.lang.Object#notify()} method or the
406 * {@link java.lang.Object#notifyAll()} method for this object, or
407 * some other thread interrupts the current thread, or a certain
408 * amount of real time has elapsed.
410 * This method is similar to the {@code wait} method of one
411 * argument, but it allows finer control over the amount of time to
412 * wait for a notification before giving up. The amount of real time,
413 * measured in nanoseconds, is given by:
416 * 1000000*timeout+nanos</pre></blockquote>
418 * In all other respects, this method does the same thing as the
419 * method {@link #wait(long)} of one argument. In particular,
420 * {@code wait(0, 0)} means the same thing as {@code wait(0)}.
422 * The current thread must own this object's monitor. The thread
423 * releases ownership of this monitor and waits until either of the
424 * following two conditions has occurred:
426 * <li>Another thread notifies threads waiting on this object's monitor
427 * to wake up either through a call to the {@code notify} method
428 * or the {@code notifyAll} method.
429 * <li>The timeout period, specified by {@code timeout}
430 * milliseconds plus {@code nanos} nanoseconds arguments, has
434 * The thread then waits until it can re-obtain ownership of the
435 * monitor and resumes execution.
437 * As in the one argument version, interrupts and spurious wakeups are
438 * possible, and this method should always be used in a loop:
440 * synchronized (obj) {
441 * while (<condition does not hold>)
442 * obj.wait(timeout, nanos);
443 * ... // Perform action appropriate to condition
446 * This method should only be called by a thread that is the owner
447 * of this object's monitor. See the {@code notify} method for a
448 * description of the ways in which a thread can become the owner of
451 * @param timeout the maximum time to wait in milliseconds.
452 * @param nanos additional time, in nanoseconds range
454 * @exception IllegalArgumentException if the value of timeout is
455 * negative or the value of nanos is
456 * not in the range 0-999999.
457 * @exception IllegalMonitorStateException if the current thread is not
458 * the owner of this object's monitor.
459 * @exception InterruptedException if any thread interrupted the
460 * current thread before or while the current thread
461 * was waiting for a notification. The <i>interrupted
462 * status</i> of the current thread is cleared when
463 * this exception is thrown.
465 public final void wait(long timeout, int nanos) throws InterruptedException {
466 throw new InterruptedException();
470 * Causes the current thread to wait until another thread invokes the
471 * {@link java.lang.Object#notify()} method or the
472 * {@link java.lang.Object#notifyAll()} method for this object.
473 * In other words, this method behaves exactly as if it simply
474 * performs the call {@code wait(0)}.
476 * The current thread must own this object's monitor. The thread
477 * releases ownership of this monitor and waits until another thread
478 * notifies threads waiting on this object's monitor to wake up
479 * either through a call to the {@code notify} method or the
480 * {@code notifyAll} method. The thread then waits until it can
481 * re-obtain ownership of the monitor and resumes execution.
483 * As in the one argument version, interrupts and spurious wakeups are
484 * possible, and this method should always be used in a loop:
486 * synchronized (obj) {
487 * while (<condition does not hold>)
489 * ... // Perform action appropriate to condition
492 * This method should only be called by a thread that is the owner
493 * of this object's monitor. See the {@code notify} method for a
494 * description of the ways in which a thread can become the owner of
497 * @exception IllegalMonitorStateException if the current thread is not
498 * the owner of the object's monitor.
499 * @exception InterruptedException if any thread interrupted the
500 * current thread before or while the current thread
501 * was waiting for a notification. The <i>interrupted
502 * status</i> of the current thread is cleared when
503 * this exception is thrown.
504 * @see java.lang.Object#notify()
505 * @see java.lang.Object#notifyAll()
507 public final void wait() throws InterruptedException {
508 throw new InterruptedException();
512 * Called by the garbage collector on an object when garbage collection
513 * determines that there are no more references to the object.
514 * A subclass overrides the {@code finalize} method to dispose of
515 * system resources or to perform other cleanup.
517 * The general contract of {@code finalize} is that it is invoked
518 * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
519 * machine has determined that there is no longer any
520 * means by which this object can be accessed by any thread that has
521 * not yet died, except as a result of an action taken by the
522 * finalization of some other object or class which is ready to be
523 * finalized. The {@code finalize} method may take any action, including
524 * making this object available again to other threads; the usual purpose
525 * of {@code finalize}, however, is to perform cleanup actions before
526 * the object is irrevocably discarded. For example, the finalize method
527 * for an object that represents an input/output connection might perform
528 * explicit I/O transactions to break the connection before the object is
529 * permanently discarded.
531 * The {@code finalize} method of class {@code Object} performs no
532 * special action; it simply returns normally. Subclasses of
533 * {@code Object} may override this definition.
535 * The Java programming language does not guarantee which thread will
536 * invoke the {@code finalize} method for any given object. It is
537 * guaranteed, however, that the thread that invokes finalize will not
538 * be holding any user-visible synchronization locks when finalize is
539 * invoked. If an uncaught exception is thrown by the finalize method,
540 * the exception is ignored and finalization of that object terminates.
542 * After the {@code finalize} method has been invoked for an object, no
543 * further action is taken until the Java virtual machine has again
544 * determined that there is no longer any means by which this object can
545 * be accessed by any thread that has not yet died, including possible
546 * actions by other objects or classes which are ready to be finalized,
547 * at which point the object may be discarded.
549 * The {@code finalize} method is never invoked more than once by a Java
550 * virtual machine for any given object.
552 * Any exception thrown by the {@code finalize} method causes
553 * the finalization of this object to be halted, but is otherwise
556 * @throws Throwable the {@code Exception} raised by this method
558 protected void finalize() throws Throwable { }