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