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;
31 * Class {@code Object} is the root of the class hierarchy.
32 * Every class has {@code Object} as a superclass. All objects,
33 * including arrays, implement the methods of this class.
36 * @see java.lang.Class
41 @JavaScriptBody(args = {}, body = "")
42 private static native void registerNatives();
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 @JavaScriptBody(args="self", body="return self.constructor.$class;")
68 public final native Class<?> getClass();
71 * Returns a hash code value for the object. This method is
72 * supported for the benefit of hash tables such as those provided by
73 * {@link java.util.HashMap}.
75 * The general contract of {@code hashCode} is:
77 * <li>Whenever it is invoked on the same object more than once during
78 * an execution of a Java application, the {@code hashCode} method
79 * must consistently return the same integer, provided no information
80 * used in {@code equals} comparisons on the object is modified.
81 * This integer need not remain consistent from one execution of an
82 * application to another execution of the same application.
83 * <li>If two objects are equal according to the {@code equals(Object)}
84 * method, then calling the {@code hashCode} method on each of
85 * the two objects must produce the same integer result.
86 * <li>It is <em>not</em> required that if two objects are unequal
87 * according to the {@link java.lang.Object#equals(java.lang.Object)}
88 * method, then calling the {@code hashCode} method on each of the
89 * two objects must produce distinct integer results. However, the
90 * programmer should be aware that producing distinct integer results
91 * for unequal objects may improve the performance of hash tables.
94 * As much as is reasonably practical, the hashCode method defined by
95 * class {@code Object} does return distinct integers for distinct
96 * objects. (This is typically implemented by converting the internal
97 * address of the object into an integer, but this implementation
98 * technique is not required by the
99 * Java<font size="-2"><sup>TM</sup></font> programming language.)
101 * @return a hash code value for this object.
102 * @see java.lang.Object#equals(java.lang.Object)
103 * @see java.lang.System#identityHashCode
105 public native int hashCode();
108 * Indicates whether some other object is "equal to" this one.
110 * The {@code equals} method implements an equivalence relation
111 * on non-null object references:
113 * <li>It is <i>reflexive</i>: for any non-null reference value
114 * {@code x}, {@code x.equals(x)} should return
116 * <li>It is <i>symmetric</i>: for any non-null reference values
117 * {@code x} and {@code y}, {@code x.equals(y)}
118 * should return {@code true} if and only if
119 * {@code y.equals(x)} returns {@code true}.
120 * <li>It is <i>transitive</i>: for any non-null reference values
121 * {@code x}, {@code y}, and {@code z}, if
122 * {@code x.equals(y)} returns {@code true} and
123 * {@code y.equals(z)} returns {@code true}, then
124 * {@code x.equals(z)} should return {@code true}.
125 * <li>It is <i>consistent</i>: for any non-null reference values
126 * {@code x} and {@code y}, multiple invocations of
127 * {@code x.equals(y)} consistently return {@code true}
128 * or consistently return {@code false}, provided no
129 * information used in {@code equals} comparisons on the
130 * objects is modified.
131 * <li>For any non-null reference value {@code x},
132 * {@code x.equals(null)} should return {@code false}.
135 * The {@code equals} method for class {@code Object} implements
136 * the most discriminating possible equivalence relation on objects;
137 * that is, for any non-null reference values {@code x} and
138 * {@code y}, this method returns {@code true} if and only
139 * if {@code x} and {@code y} refer to the same object
140 * ({@code x == y} has the value {@code true}).
142 * Note that it is generally necessary to override the {@code hashCode}
143 * method whenever this method is overridden, so as to maintain the
144 * general contract for the {@code hashCode} method, which states
145 * that equal objects must have equal hash codes.
147 * @param obj the reference object with which to compare.
148 * @return {@code true} if this object is the same as the obj
149 * argument; {@code false} otherwise.
151 * @see java.util.HashMap
153 public boolean equals(Object obj) {
154 return (this == obj);
158 * Creates and returns a copy of this object. The precise meaning
159 * of "copy" may depend on the class of the object. The general
160 * intent is that, for any object {@code x}, the expression:
163 * x.clone() != x</pre></blockquote>
164 * will be true, and that the expression:
167 * x.clone().getClass() == x.getClass()</pre></blockquote>
168 * will be {@code true}, but these are not absolute requirements.
169 * While it is typically the case that:
172 * x.clone().equals(x)</pre></blockquote>
173 * will be {@code true}, this is not an absolute requirement.
175 * By convention, the returned object should be obtained by calling
176 * {@code super.clone}. If a class and all of its superclasses (except
177 * {@code Object}) obey this convention, it will be the case that
178 * {@code x.clone().getClass() == x.getClass()}.
180 * By convention, the object returned by this method should be independent
181 * of this object (which is being cloned). To achieve this independence,
182 * it may be necessary to modify one or more fields of the object returned
183 * by {@code super.clone} before returning it. Typically, this means
184 * copying any mutable objects that comprise the internal "deep structure"
185 * of the object being cloned and replacing the references to these
186 * objects with references to the copies. If a class contains only
187 * primitive fields or references to immutable objects, then it is usually
188 * the case that no fields in the object returned by {@code super.clone}
189 * need to be modified.
191 * The method {@code clone} for class {@code Object} performs a
192 * specific cloning operation. First, if the class of this object does
193 * not implement the interface {@code Cloneable}, then a
194 * {@code CloneNotSupportedException} is thrown. Note that all arrays
195 * are considered to implement the interface {@code Cloneable} and that
196 * the return type of the {@code clone} method of an array type {@code T[]}
197 * is {@code T[]} where T is any reference or primitive type.
198 * Otherwise, this method creates a new instance of the class of this
199 * object and initializes all its fields with exactly the contents of
200 * the corresponding fields of this object, as if by assignment; the
201 * contents of the fields are not themselves cloned. Thus, this method
202 * performs a "shallow copy" of this object, not a "deep copy" operation.
204 * The class {@code Object} does not itself implement the interface
205 * {@code Cloneable}, so calling the {@code clone} method on an object
206 * whose class is {@code Object} will result in throwing an
207 * exception at run time.
209 * @return a clone of this instance.
210 * @exception CloneNotSupportedException if the object's class does not
211 * support the {@code Cloneable} interface. Subclasses
212 * that override the {@code clone} method can also
213 * throw this exception to indicate that an instance cannot
215 * @see java.lang.Cloneable
217 protected native Object clone() throws CloneNotSupportedException;
220 * Returns a string representation of the object. In general, the
221 * {@code toString} method returns a string that
222 * "textually represents" this object. The result should
223 * be a concise but informative representation that is easy for a
225 * It is recommended that all subclasses override this method.
227 * The {@code toString} method for class {@code Object}
228 * returns a string consisting of the name of the class of which the
229 * object is an instance, the at-sign character `{@code @}', and
230 * the unsigned hexadecimal representation of the hash code of the
231 * object. In other words, this method returns a string equal to the
235 * getClass().getName() + '@' + Integer.toHexString(hashCode())
236 * </pre></blockquote>
238 * @return a string representation of the object.
240 public String toString() {
241 return getClass().getName() + "@" + Integer.toHexString(hashCode());
245 * Wakes up a single thread that is waiting on this object's
246 * monitor. If any threads are waiting on this object, one of them
247 * is chosen to be awakened. The choice is arbitrary and occurs at
248 * the discretion of the implementation. A thread waits on an object's
249 * monitor by calling one of the {@code wait} methods.
251 * The awakened thread will not be able to proceed until the current
252 * thread relinquishes the lock on this object. The awakened thread will
253 * compete in the usual manner with any other threads that might be
254 * actively competing to synchronize on this object; for example, the
255 * awakened thread enjoys no reliable privilege or disadvantage in being
256 * the next thread to lock this object.
258 * This method should only be called by a thread that is the owner
259 * of this object's monitor. A thread becomes the owner of the
260 * object's monitor in one of three ways:
262 * <li>By executing a synchronized instance method of that object.
263 * <li>By executing the body of a {@code synchronized} statement
264 * that synchronizes on the object.
265 * <li>For objects of type {@code Class,} by executing a
266 * synchronized static method of that class.
269 * Only one thread at a time can own an object's monitor.
271 * @exception IllegalMonitorStateException if the current thread is not
272 * the owner of this object's monitor.
273 * @see java.lang.Object#notifyAll()
274 * @see java.lang.Object#wait()
276 public final native void notify();
279 * Wakes up all threads that are waiting on this object's monitor. A
280 * thread waits on an object's monitor by calling one of the
281 * {@code wait} methods.
283 * The awakened threads will not be able to proceed until the current
284 * thread relinquishes the lock on this object. The awakened threads
285 * will compete in the usual manner with any other threads that might
286 * be actively competing to synchronize on this object; for example,
287 * the awakened threads enjoy no reliable privilege or disadvantage in
288 * being the next thread to lock this object.
290 * This method should only be called by a thread that is the owner
291 * of this object's monitor. See the {@code notify} method for a
292 * description of the ways in which a thread can become the owner of
295 * @exception IllegalMonitorStateException if the current thread is not
296 * the owner of this object's monitor.
297 * @see java.lang.Object#notify()
298 * @see java.lang.Object#wait()
300 public final native void notifyAll();
303 * Causes the current thread to wait until either another thread invokes the
304 * {@link java.lang.Object#notify()} method or the
305 * {@link java.lang.Object#notifyAll()} method for this object, or a
306 * specified amount of time has elapsed.
308 * The current thread must own this object's monitor.
310 * This method causes the current thread (call it <var>T</var>) to
311 * place itself in the wait set for this object and then to relinquish
312 * any and all synchronization claims on this object. Thread <var>T</var>
313 * becomes disabled for thread scheduling purposes and lies dormant
314 * until one of four things happens:
316 * <li>Some other thread invokes the {@code notify} method for this
317 * object and thread <var>T</var> happens to be arbitrarily chosen as
318 * the thread to be awakened.
319 * <li>Some other thread invokes the {@code notifyAll} method for this
321 * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
322 * thread <var>T</var>.
323 * <li>The specified amount of real time has elapsed, more or less. If
324 * {@code timeout} is zero, however, then real time is not taken into
325 * consideration and the thread simply waits until notified.
327 * The thread <var>T</var> is then removed from the wait set for this
328 * object and re-enabled for thread scheduling. It then competes in the
329 * usual manner with other threads for the right to synchronize on the
330 * object; once it has gained control of the object, all its
331 * synchronization claims on the object are restored to the status quo
332 * ante - that is, to the situation as of the time that the {@code wait}
333 * method was invoked. Thread <var>T</var> then returns from the
334 * invocation of the {@code wait} method. Thus, on return from the
335 * {@code wait} method, the synchronization state of the object and of
336 * thread {@code T} is exactly as it was when the {@code wait} method
339 * A thread can also wake up without being notified, interrupted, or
340 * timing out, a so-called <i>spurious wakeup</i>. While this will rarely
341 * occur in practice, applications must guard against it by testing for
342 * the condition that should have caused the thread to be awakened, and
343 * continuing to wait if the condition is not satisfied. In other words,
344 * waits should always occur in loops, like this one:
346 * synchronized (obj) {
347 * while (<condition does not hold>)
349 * ... // Perform action appropriate to condition
352 * (For more information on this topic, see Section 3.2.3 in Doug Lea's
353 * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
354 * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
355 * Language Guide" (Addison-Wesley, 2001).
357 * <p>If the current thread is {@linkplain java.lang.Thread#interrupt()
358 * interrupted} by any thread before or while it is waiting, then an
359 * {@code InterruptedException} is thrown. This exception is not
360 * thrown until the lock status of this object has been restored as
364 * Note that the {@code wait} method, as it places the current thread
365 * into the wait set for this object, unlocks only this object; any
366 * other objects on which the current thread may be synchronized remain
367 * locked while the thread waits.
369 * This method should only be called by a thread that is the owner
370 * of this object's monitor. See the {@code notify} method for a
371 * description of the ways in which a thread can become the owner of
374 * @param timeout the maximum time to wait in milliseconds.
375 * @exception IllegalArgumentException if the value of timeout is
377 * @exception IllegalMonitorStateException if the current thread is not
378 * the owner of the object's monitor.
379 * @exception InterruptedException if any thread interrupted the
380 * current thread before or while the current thread
381 * was waiting for a notification. The <i>interrupted
382 * status</i> of the current thread is cleared when
383 * this exception is thrown.
384 * @see java.lang.Object#notify()
385 * @see java.lang.Object#notifyAll()
387 public final native void wait(long timeout) throws InterruptedException;
390 * Causes the current thread to wait until another thread invokes the
391 * {@link java.lang.Object#notify()} method or the
392 * {@link java.lang.Object#notifyAll()} method for this object, or
393 * some other thread interrupts the current thread, or a certain
394 * amount of real time has elapsed.
396 * This method is similar to the {@code wait} method of one
397 * argument, but it allows finer control over the amount of time to
398 * wait for a notification before giving up. The amount of real time,
399 * measured in nanoseconds, is given by:
402 * 1000000*timeout+nanos</pre></blockquote>
404 * In all other respects, this method does the same thing as the
405 * method {@link #wait(long)} of one argument. In particular,
406 * {@code wait(0, 0)} means the same thing as {@code wait(0)}.
408 * The current thread must own this object's monitor. The thread
409 * releases ownership of this monitor and waits until either of the
410 * following two conditions has occurred:
412 * <li>Another thread notifies threads waiting on this object's monitor
413 * to wake up either through a call to the {@code notify} method
414 * or the {@code notifyAll} method.
415 * <li>The timeout period, specified by {@code timeout}
416 * milliseconds plus {@code nanos} nanoseconds arguments, has
420 * The thread then waits until it can re-obtain ownership of the
421 * monitor and resumes execution.
423 * As in the one argument version, interrupts and spurious wakeups are
424 * possible, and this method should always be used in a loop:
426 * synchronized (obj) {
427 * while (<condition does not hold>)
428 * obj.wait(timeout, nanos);
429 * ... // Perform action appropriate to condition
432 * This method should only be called by a thread that is the owner
433 * of this object's monitor. See the {@code notify} method for a
434 * description of the ways in which a thread can become the owner of
437 * @param timeout the maximum time to wait in milliseconds.
438 * @param nanos additional time, in nanoseconds range
440 * @exception IllegalArgumentException if the value of timeout is
441 * negative or the value of nanos is
442 * not in the range 0-999999.
443 * @exception IllegalMonitorStateException if the current thread is not
444 * the owner of this object's monitor.
445 * @exception InterruptedException if any thread interrupted the
446 * current thread before or while the current thread
447 * was waiting for a notification. The <i>interrupted
448 * status</i> of the current thread is cleared when
449 * this exception is thrown.
451 public final void wait(long timeout, int nanos) throws InterruptedException {
453 throw new IllegalArgumentException("timeout value is negative");
456 if (nanos < 0 || nanos > 999999) {
457 throw new IllegalArgumentException(
458 "nanosecond timeout value out of range");
461 if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
469 * Causes the current thread to wait until another thread invokes the
470 * {@link java.lang.Object#notify()} method or the
471 * {@link java.lang.Object#notifyAll()} method for this object.
472 * In other words, this method behaves exactly as if it simply
473 * performs the call {@code wait(0)}.
475 * The current thread must own this object's monitor. The thread
476 * releases ownership of this monitor and waits until another thread
477 * notifies threads waiting on this object's monitor to wake up
478 * either through a call to the {@code notify} method or the
479 * {@code notifyAll} method. The thread then waits until it can
480 * re-obtain ownership of the monitor and resumes execution.
482 * As in the one argument version, interrupts and spurious wakeups are
483 * possible, and this method should always be used in a loop:
485 * synchronized (obj) {
486 * while (<condition does not hold>)
488 * ... // Perform action appropriate to condition
491 * This method should only be called by a thread that is the owner
492 * of this object's monitor. See the {@code notify} method for a
493 * description of the ways in which a thread can become the owner of
496 * @exception IllegalMonitorStateException if the current thread is not
497 * the owner of the object's monitor.
498 * @exception InterruptedException if any thread interrupted the
499 * current thread before or while the current thread
500 * was waiting for a notification. The <i>interrupted
501 * status</i> of the current thread is cleared when
502 * this exception is thrown.
503 * @see java.lang.Object#notify()
504 * @see java.lang.Object#notifyAll()
506 public final void wait() throws InterruptedException {
511 * Called by the garbage collector on an object when garbage collection
512 * determines that there are no more references to the object.
513 * A subclass overrides the {@code finalize} method to dispose of
514 * system resources or to perform other cleanup.
516 * The general contract of {@code finalize} is that it is invoked
517 * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
518 * machine has determined that there is no longer any
519 * means by which this object can be accessed by any thread that has
520 * not yet died, except as a result of an action taken by the
521 * finalization of some other object or class which is ready to be
522 * finalized. The {@code finalize} method may take any action, including
523 * making this object available again to other threads; the usual purpose
524 * of {@code finalize}, however, is to perform cleanup actions before
525 * the object is irrevocably discarded. For example, the finalize method
526 * for an object that represents an input/output connection might perform
527 * explicit I/O transactions to break the connection before the object is
528 * permanently discarded.
530 * The {@code finalize} method of class {@code Object} performs no
531 * special action; it simply returns normally. Subclasses of
532 * {@code Object} may override this definition.
534 * The Java programming language does not guarantee which thread will
535 * invoke the {@code finalize} method for any given object. It is
536 * guaranteed, however, that the thread that invokes finalize will not
537 * be holding any user-visible synchronization locks when finalize is
538 * invoked. If an uncaught exception is thrown by the finalize method,
539 * the exception is ignored and finalization of that object terminates.
541 * After the {@code finalize} method has been invoked for an object, no
542 * further action is taken until the Java virtual machine has again
543 * determined that there is no longer any means by which this object can
544 * be accessed by any thread that has not yet died, including possible
545 * actions by other objects or classes which are ready to be finalized,
546 * at which point the object may be discarded.
548 * The {@code finalize} method is never invoked more than once by a Java
549 * virtual machine for any given object.
551 * Any exception thrown by the {@code finalize} method causes
552 * the finalization of this object to be halted, but is otherwise
555 * @throws Throwable the {@code Exception} raised by this method
557 protected void finalize() throws Throwable { }