1 /*
  2  * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package java.lang;
 25 
 26 import jdk.internal.misc.Blocker;
 27 import nsk.jvmti.scenarios.bcinstr.BI04.bi04t002a;
 28 
 29 /**
 30  * Class <code>Object</code> is the root of the class hierarchy.
 31  * Every class has <code>Object</code> as a superclass. All objects,
 32  * including arrays, implement the methods of this class.
 33  *
 34  * @author  unascribed
 35  * @version 1.67, 02/03/04
 36  * @see     java.lang.Class
 37  * @since   JDK1.0
 38  */
 39 public class Object {
 40 
 41     /**
 42      * Returns the runtime class of an object. That <tt>Class</tt>
 43      * object is the object that is locked by <tt>static synchronized</tt>
 44      * methods of the represented class.
 45      *
 46      * @return The <code>java.lang.Class</code> object that represents
 47      *         the runtime class of the object.  The result is of type
 48      *         {@code Class<? extends X>} where X is the
 49      *         static type of the expression on which
 50      *         <code>getClass</code> is called.
 51      */
 52     public final native Class<? extends Object> getClass();
 53 
 54     /**
 55      * Returns a hash code value for the object. This method is
 56      * supported for the benefit of hashtables such as those provided by
 57      * <code>java.util.Hashtable</code>.
 58      * <p>
 59      * The general contract of <code>hashCode</code> is:
 60      * <ul>
 61      * <li>Whenever it is invoked on the same object more than once during
 62      *     an execution of a Java application, the <tt>hashCode</tt> method
 63      *     must consistently return the same integer, provided no information
 64      *     used in <tt>equals</tt> comparisons on the object is modified.
 65      *     This integer need not remain consistent from one execution of an
 66      *     application to another execution of the same application.
 67      * <li>If two objects are equal according to the <tt>equals(Object)</tt>
 68      *     method, then calling the <code>hashCode</code> method on each of
 69      *     the two objects must produce the same integer result.
 70      * <li>It is <em>not</em> required that if two objects are unequal
 71      *     according to the {@link java.lang.Object#equals(java.lang.Object)}
 72      *     method, then calling the <tt>hashCode</tt> method on each of the
 73      *     two objects must produce distinct integer results.  However, the
 74      *     programmer should be aware that producing distinct integer results
 75      *     for unequal objects may improve the performance of hashtables.
 76      * </ul>
 77      * <p>
 78      * As much as is reasonably practical, the hashCode method defined by
 79      * class <tt>Object</tt> does return distinct integers for distinct
 80      * objects. (This is typically implemented by converting the internal
 81      * address of the object into an integer, but this implementation
 82      * technique is not required by the
 83      * Java<font size="-2"><sup>TM</sup></font> programming language.)
 84      *
 85      * @return  a hash code value for this object.
 86      * @see     java.lang.Object#equals(java.lang.Object)
 87      * @see     java.util.Hashtable
 88      */
 89     public native int hashCode();
 90 
 91     /**
 92      * Indicates whether some other object is "equal to" this one.
 93      * <p>
 94      * The <code>equals</code> method implements an equivalence relation
 95      * on non-null object references:
 96      * <ul>
 97      * <li>It is <i>reflexive</i>: for any non-null reference value
 98      *     <code>x</code>, <code>x.equals(x)</code> should return
 99      *     <code>true</code>.
100      * <li>It is <i>symmetric</i>: for any non-null reference values
101      *     <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
102      *     should return <code>true</code> if and only if
103      *     <code>y.equals(x)</code> returns <code>true</code>.
104      * <li>It is <i>transitive</i>: for any non-null reference values
105      *     <code>x</code>, <code>y</code>, and <code>z</code>, if
106      *     <code>x.equals(y)</code> returns <code>true</code> and
107      *     <code>y.equals(z)</code> returns <code>true</code>, then
108      *     <code>x.equals(z)</code> should return <code>true</code>.
109      * <li>It is <i>consistent</i>: for any non-null reference values
110      *     <code>x</code> and <code>y</code>, multiple invocations of
111      *     <tt>x.equals(y)</tt> consistently return <code>true</code>
112      *     or consistently return <code>false</code>, provided no
113      *     information used in <code>equals</code> comparisons on the
114      *     objects is modified.
115      * <li>For any non-null reference value <code>x</code>,
116      *     <code>x.equals(null)</code> should return <code>false</code>.
117      * </ul>
118      * <p>
119      * The <tt>equals</tt> method for class <code>Object</code> implements
120      * the most discriminating possible equivalence relation on objects;
121      * that is, for any non-null reference values <code>x</code> and
122      * <code>y</code>, this method returns <code>true</code> if and only
123      * if <code>x</code> and <code>y</code> refer to the same object
124      * (<code>x == y</code> has the value <code>true</code>).
125      * <p>
126      * Note that it is generally necessary to override the <tt>hashCode</tt>
127      * method whenever this method is overridden, so as to maintain the
128      * general contract for the <tt>hashCode</tt> method, which states
129      * that equal objects must have equal hash codes.
130      *
131      * @param   obj   the reference object with which to compare.
132      * @return  <code>true</code> if this object is the same as the obj
133      *          argument; <code>false</code> otherwise.
134      * @see     #hashCode()
135      * @see     java.util.Hashtable
136      */
137     public boolean equals(Object obj) {
138         bi04t002a.instrInvoke(bi04t002a.INSTR_EQUALS);
139         return (this == obj);
140     }
141 
142     /**
143      * Creates and returns a copy of this object.  The precise meaning
144      * of "copy" may depend on the class of the object. The general
145      * intent is that, for any object <tt>x</tt>, the expression:
146      * <blockquote>
147      * <pre>
148      * x.clone() != x</pre></blockquote>
149      * will be true, and that the expression:
150      * <blockquote>
151      * <pre>
152      * x.clone().getClass() == x.getClass()</pre></blockquote>
153      * will be <tt>true</tt>, but these are not absolute requirements.
154      * While it is typically the case that:
155      * <blockquote>
156      * <pre>
157      * x.clone().equals(x)</pre></blockquote>
158      * will be <tt>true</tt>, this is not an absolute requirement.
159      * <p>
160      * By convention, the returned object should be obtained by calling
161      * <tt>super.clone</tt>.  If a class and all of its superclasses (except
162      * <tt>Object</tt>) obey this convention, it will be the case that
163      * <tt>x.clone().getClass() == x.getClass()</tt>.
164      * <p>
165      * By convention, the object returned by this method should be independent
166      * of this object (which is being cloned).  To achieve this independence,
167      * it may be necessary to modify one or more fields of the object returned
168      * by <tt>super.clone</tt> before returning it.  Typically, this means
169      * copying any mutable objects that comprise the internal "deep structure"
170      * of the object being cloned and replacing the references to these
171      * objects with references to the copies.  If a class contains only
172      * primitive fields or references to immutable objects, then it is usually
173      * the case that no fields in the object returned by <tt>super.clone</tt>
174      * need to be modified.
175      * <p>
176      * The method <tt>clone</tt> for class <tt>Object</tt> performs a
177      * specific cloning operation. First, if the class of this object does
178      * not implement the interface <tt>Cloneable</tt>, then a
179      * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays
180      * are considered to implement the interface <tt>Cloneable</tt>.
181      * Otherwise, this method creates a new instance of the class of this
182      * object and initializes all its fields with exactly the contents of
183      * the corresponding fields of this object, as if by assignment; the
184      * contents of the fields are not themselves cloned. Thus, this method
185      * performs a "shallow copy" of this object, not a "deep copy" operation.
186      * <p>
187      * The class <tt>Object</tt> does not itself implement the interface
188      * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object
189      * whose class is <tt>Object</tt> will result in throwing an
190      * exception at run time.
191      *
192      * @return     a clone of this instance.
193      * @exception  CloneNotSupportedException  if the object's class does not
194      *               support the <code>Cloneable</code> interface. Subclasses
195      *               that override the <code>clone</code> method can also
196      *               throw this exception to indicate that an instance cannot
197      *               be cloned.
198      * @see java.lang.Cloneable
199      */
200     protected native Object clone() throws CloneNotSupportedException;
201 
202     /**
203      * Returns a string representation of the object. In general, the
204      * <code>toString</code> method returns a string that
205      * "textually represents" this object. The result should
206      * be a concise but informative representation that is easy for a
207      * person to read.
208      * It is recommended that all subclasses override this method.
209      * <p>
210      * The <code>toString</code> method for class <code>Object</code>
211      * returns a string consisting of the name of the class of which the
212      * object is an instance, the at-sign character `<code>@</code>', and
213      * the unsigned hexadecimal representation of the hash code of the
214      * object. In other words, this method returns a string equal to the
215      * value of:
216      * <blockquote>
217      * <pre>
218      * getClass().getName() + '@' + Integer.toHexString(hashCode())
219      * </pre></blockquote>
220      *
221      * @return  a string representation of the object.
222      */
223     public String toString() {
224         bi04t002a.instrInvoke(bi04t002a.INSTR_TOSTRING);
225         return getClass().getName() + "@" + Integer.toHexString(hashCode());
226     }
227 
228     /**
229      * Wakes up a single thread that is waiting on this object's
230      * monitor. If any threads are waiting on this object, one of them
231      * is chosen to be awakened. The choice is arbitrary and occurs at
232      * the discretion of the implementation. A thread waits on an object's
233      * monitor by calling one of the <code>wait</code> methods.
234      * <p>
235      * The awakened thread will not be able to proceed until the current
236      * thread relinquishes the lock on this object. The awakened thread will
237      * compete in the usual manner with any other threads that might be
238      * actively competing to synchronize on this object; for example, the
239      * awakened thread enjoys no reliable privilege or disadvantage in being
240      * the next thread to lock this object.
241      * <p>
242      * This method should only be called by a thread that is the owner
243      * of this object's monitor. A thread becomes the owner of the
244      * object's monitor in one of three ways:
245      * <ul>
246      * <li>By executing a synchronized instance method of that object.
247      * <li>By executing the body of a <code>synchronized</code> statement
248      *     that synchronizes on the object.
249      * <li>For objects of type <code>Class,</code> by executing a
250      *     synchronized static method of that class.
251      * </ul>
252      * <p>
253      * Only one thread at a time can own an object's monitor.
254      *
255      * @exception  IllegalMonitorStateException  if the current thread is not
256      *               the owner of this object's monitor.
257      * @see        java.lang.Object#notifyAll()
258      * @see        java.lang.Object#wait()
259      */
260     public final native void notify();
261 
262     /**
263      * Wakes up all threads that are waiting on this object's monitor. A
264      * thread waits on an object's monitor by calling one of the
265      * <code>wait</code> methods.
266      * <p>
267      * The awakened threads will not be able to proceed until the current
268      * thread relinquishes the lock on this object. The awakened threads
269      * will compete in the usual manner with any other threads that might
270      * be actively competing to synchronize on this object; for example,
271      * the awakened threads enjoy no reliable privilege or disadvantage in
272      * being the next thread to lock this object.
273      * <p>
274      * This method should only be called by a thread that is the owner
275      * of this object's monitor. See the <code>notify</code> method for a
276      * description of the ways in which a thread can become the owner of
277      * a monitor.
278      *
279      * @exception  IllegalMonitorStateException  if the current thread is not
280      *               the owner of this object's monitor.
281      * @see        java.lang.Object#notify()
282      * @see        java.lang.Object#wait()
283      */
284     public final native void notifyAll();
285 
286     /**
287      * Causes current thread to wait until either another thread invokes the
288      * {@link java.lang.Object#notify()} method or the
289      * {@link java.lang.Object#notifyAll()} method for this object, or a
290      * specified amount of time has elapsed.
291      * <p>
292      * The current thread must own this object's monitor.
293      * <p>
294      * This method causes the current thread (call it <var>T</var>) to
295      * place itself in the wait set for this object and then to relinquish
296      * any and all synchronization claims on this object. Thread <var>T</var>
297      * becomes disabled for thread scheduling purposes and lies dormant
298      * until one of four things happens:
299      * <ul>
300      * <li>Some other thread invokes the <tt>notify</tt> method for this
301      * object and thread <var>T</var> happens to be arbitrarily chosen as
302      * the thread to be awakened.
303      * <li>Some other thread invokes the <tt>notifyAll</tt> method for this
304      * object.
305      * <li>Some other thread {@link java.lang.Thread#interrupt() interrupts}
306      * thread <var>T</var>.
307      * <li>The specified amount of real time has elapsed, more or less.  If
308      * <tt>timeout</tt> is zero, however, then real time is not taken into
309      * consideration and the thread simply waits until notified.
310      * </ul>
311      * The thread <var>T</var> is then removed from the wait set for this
312      * object and re-enabled for thread scheduling. It then competes in the
313      * usual manner with other threads for the right to synchronize on the
314      * object; once it has gained control of the object, all its
315      * synchronization claims on the object are restored to the status quo
316      * ante - that is, to the situation as of the time that the <tt>wait</tt>
317      * method was invoked. Thread <var>T</var> then returns from the
318      * invocation of the <tt>wait</tt> method. Thus, on return from the
319      * <tt>wait</tt> method, the synchronization state of the object and of
320      * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method
321      * was invoked.
322      * <p>
323      * A thread can also wake up without being notified, interrupted, or
324      * timing out, a so-called <i>spurious wakeup</i>.  While this will rarely
325      * occur in practice, applications must guard against it by testing for
326      * the condition that should have caused the thread to be awakened, and
327      * continuing to wait if the condition is not satisfied.  In other words,
328      * waits should always occur in loops, like this one:
329      * <pre>
330      *     synchronized (obj) {
331      *         while (&lt;condition does not hold&gt;)
332      *             obj.wait(timeout);
333      *         ... // Perform action appropriate to condition
334      *     }
335      * </pre>
336      * (For more information on this topic, see Section 3.2.3 in Doug Lea's
337      * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
338      * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
339      * Language Guide" (Addison-Wesley, 2001).
340      * <p>
341      * If the current thread is
342      * {@link java.lang.Thread#interrupt() interrupted} by another thread
343      * while it is waiting, then an <tt>InterruptedException</tt> is thrown.
344      * This exception is not thrown until the lock status of this object has
345      * been restored as described above.
346      * <p>
347      * Note that the <tt>wait</tt> method, as it places the current thread
348      * into the wait set for this object, unlocks only this object; any
349      * other objects on which the current thread may be synchronized remain
350      * locked while the thread waits.
351      * <p>
352      * This method should only be called by a thread that is the owner
353      * of this object's monitor. See the <code>notify</code> method for a
354      * description of the ways in which a thread can become the owner of
355      * a monitor.
356      *
357      * @param      timeout   the maximum time to wait in milliseconds.
358      * @exception  IllegalArgumentException      if the value of timeout is
359      *               negative.
360      * @exception  IllegalMonitorStateException  if the current thread is not
361      *               the owner of the object's monitor.
362      * @exception  InterruptedException if another thread interrupted the
363      *             current thread before or while the current thread
364      *             was waiting for a notification.  The <i>interrupted
365      *             status</i> of the current thread is cleared when
366      *             this exception is thrown.
367      * @see        java.lang.Object#notify()
368      * @see        java.lang.Object#notifyAll()
369      */
370     public final void wait(long timeoutMillis) throws InterruptedException {
371         long comp = Blocker.begin();
372         try {
373             wait0(timeoutMillis);
374         } catch (InterruptedException e) {
375             Thread thread = Thread.currentThread();
376             if (thread.isVirtual())
377                 thread.getAndClearInterrupt();
378             throw e;
379         } finally {
380             Blocker.end(comp);
381         }
382     }
383 
384     /**
385      * Causes current thread to wait until another thread invokes the
386      * {@link java.lang.Object#notify()} method or the
387      * {@link java.lang.Object#notifyAll()} method for this object, or
388      * some other thread interrupts the current thread, or a certain
389      * amount of real time has elapsed.
390      * <p>
391      * This method is similar to the <code>wait</code> method of one
392      * argument, but it allows finer control over the amount of time to
393      * wait for a notification before giving up. The amount of real time,
394      * measured in nanoseconds, is given by:
395      * <blockquote>
396      * <pre>
397      * 1000000*timeout+nanos</pre></blockquote>
398      * <p>
399      * In all other respects, this method does the same thing as the
400      * method {@link #wait(long)} of one argument. In particular,
401      * <tt>wait(0, 0)</tt> means the same thing as <tt>wait(0)</tt>.
402      * <p>
403      * The current thread must own this object's monitor. The thread
404      * releases ownership of this monitor and waits until either of the
405      * following two conditions has occurred:
406      * <ul>
407      * <li>Another thread notifies threads waiting on this object's monitor
408      *     to wake up either through a call to the <code>notify</code> method
409      *     or the <code>notifyAll</code> method.
410      * <li>The timeout period, specified by <code>timeout</code>
411      *     milliseconds plus <code>nanos</code> nanoseconds arguments, has
412      *     elapsed.
413      * </ul>
414      * <p>
415      * The thread then waits until it can re-obtain ownership of the
416      * monitor and resumes execution.
417      * <p>
418      * As in the one argument version, interrupts and spurious wakeups are
419      * possible, and this method should always be used in a loop:
420      * <pre>
421      *     synchronized (obj) {
422      *         while (&lt;condition does not hold&gt;)
423      *             obj.wait(timeout, nanos);
424      *         ... // Perform action appropriate to condition
425      *     }
426      * </pre>
427      * This method should only be called by a thread that is the owner
428      * of this object's monitor. See the <code>notify</code> method for a
429      * description of the ways in which a thread can become the owner of
430      * a monitor.
431      *
432      * @param      timeout   the maximum time to wait in milliseconds.
433      * @param      nanos      additional time, in nanoseconds range
434      *                       0-999999.
435      * @exception  IllegalArgumentException      if the value of timeout is
436      *                      negative or the value of nanos is
437      *                      not in the range 0-999999.
438      * @exception  IllegalMonitorStateException  if the current thread is not
439      *               the owner of this object's monitor.
440      * @exception  InterruptedException if another thread interrupted the
441      *             current thread before or while the current thread
442      *             was waiting for a notification.  The <i>interrupted
443      *             status</i> of the current thread is cleared when
444      *             this exception is thrown.
445      */
446     public final void wait(long timeout, int nanos) throws InterruptedException {
447 
448         bi04t002a.instrInvoke(bi04t002a.INSTR_WAIT_JI);
449 
450         if (timeout < 0) {
451             throw new IllegalArgumentException("timeout value is negative");
452         }
453 
454         if (nanos < 0 || nanos > 999999) {
455             throw new IllegalArgumentException(
456                                 "nanosecond timeout value out of range");
457         }
458 
459             if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
460                 timeout++;
461             }
462 
463             wait(timeout);
464     }
465 
466     // final modifier so method not in vtable
467     private final native void wait0(long timeoutMillis) throws InterruptedException;
468 
469     /**
470      * Causes 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 <tt>wait(0)</tt>.
475      * <p>
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</code> method or the
480      * <code>notifyAll</code> method. The thread then waits until it can
481      * re-obtain ownership of the monitor and resumes execution.
482      * <p>
483      * As in the one argument version, interrupts and spurious wakeups are
484      * possible, and this method should always be used in a loop:
485      * <pre>
486      *     synchronized (obj) {
487      *         while (&lt;condition does not hold&gt;)
488      *             obj.wait();
489      *         ... // Perform action appropriate to condition
490      *     }
491      * </pre>
492      * This method should only be called by a thread that is the owner
493      * of this object's monitor. See the <code>notify</code> method for a
494      * description of the ways in which a thread can become the owner of
495      * a monitor.
496      *
497      * @exception  IllegalMonitorStateException  if the current thread is not
498      *               the owner of the object's monitor.
499      * @exception  InterruptedException if another 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()
506      */
507     public final void wait() throws InterruptedException {
508         bi04t002a.instrInvoke(bi04t002a.INSTR_WAIT);
509         wait(0);
510     }
511 
512     /**
513      * Called by the garbage collector on an object when garbage collection
514      * determines that there are no more references to the object.
515      * A subclass overrides the <code>finalize</code> method to dispose of
516      * system resources or to perform other cleanup.
517      * <p>
518      * The general contract of <tt>finalize</tt> is that it is invoked
519      * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
520      * machine has determined that there is no longer any
521      * means by which this object can be accessed by any thread that has
522      * not yet died, except as a result of an action taken by the
523      * finalization of some other object or class which is ready to be
524      * finalized. The <tt>finalize</tt> method may take any action, including
525      * making this object available again to other threads; the usual purpose
526      * of <tt>finalize</tt>, however, is to perform cleanup actions before
527      * the object is irrevocably discarded. For example, the finalize method
528      * for an object that represents an input/output connection might perform
529      * explicit I/O transactions to break the connection before the object is
530      * permanently discarded.
531      * <p>
532      * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
533      * special action; it simply returns normally. Subclasses of
534      * <tt>Object</tt> may override this definition.
535      * <p>
536      * The Java programming language does not guarantee which thread will
537      * invoke the <tt>finalize</tt> method for any given object. It is
538      * guaranteed, however, that the thread that invokes finalize will not
539      * be holding any user-visible synchronization locks when finalize is
540      * invoked. If an uncaught exception is thrown by the finalize method,
541      * the exception is ignored and finalization of that object terminates.
542      * <p>
543      * After the <tt>finalize</tt> method has been invoked for an object, no
544      * further action is taken until the Java virtual machine has again
545      * determined that there is no longer any means by which this object can
546      * be accessed by any thread that has not yet died, including possible
547      * actions by other objects or classes which are ready to be finalized,
548      * at which point the object may be discarded.
549      * <p>
550      * The <tt>finalize</tt> method is never invoked more than once by a Java
551      * virtual machine for any given object.
552      * <p>
553      * Any exception thrown by the <code>finalize</code> method causes
554      * the finalization of this object to be halted, but is otherwise
555      * ignored.
556      *
557      * @throws Throwable the <code>Exception</code> raised by this method
558      */
559     protected void finalize() throws Throwable { }
560 
561 }