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