Object.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* java.lang.Object - The universal superclass in Java
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2007
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.lang;
  33. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  34. * "The Java Language Specification", ISBN 0-201-63451-1
  35. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  36. * plus gcj compiler sources (to determine object layout)
  37. * Status: Complete to version 1.1
  38. */
  39. /**
  40. * Object is the ultimate superclass of every class
  41. * (excepting interfaces). When you define a class that
  42. * does not extend any other class, it implicitly extends
  43. * java.lang.Object. Also, an anonymous class based on
  44. * an interface will extend Object.
  45. *
  46. * <p>It provides general-purpose methods that every single
  47. * Object, regardless of race, sex or creed, implements.
  48. * All of the public methods may be invoked on arrays or
  49. * interfaces. The protected methods <code>clone</code>
  50. * and <code>finalize</code> are not accessible on arrays
  51. * or interfaces, but all array types have a public version
  52. * of <code>clone</code> which is accessible.
  53. *
  54. * @author John Keiser
  55. * @author Eric Blake (ebb9@email.byu.edu)
  56. * @author Tom Tromey (tromey@cygnus.com)
  57. */
  58. public class Object
  59. {
  60. /**
  61. * Called on an object by the Virtual Machine at most once,
  62. * at some point after the Object is determined unreachable
  63. * but before it is destroyed. You would think that this
  64. * means it eventually is called on every Object, but this is
  65. * not necessarily the case. If execution terminates
  66. * abnormally, garbage collection does not always happen.
  67. * Thus you cannot rely on this method to always work.
  68. * For finer control over garbage collection, use references
  69. * from the {@link java.lang.ref} package.
  70. *
  71. * <p>Virtual Machines are free to not call this method if
  72. * they can determine that it does nothing important; for
  73. * example, if your class extends Object and overrides
  74. * finalize to do simply <code>super.finalize()</code>.
  75. *
  76. * <p>finalize() will be called by a {@link Thread} that has no
  77. * locks on any Objects, and may be called concurrently.
  78. * There are no guarantees on the order in which multiple
  79. * objects are finalized. This means that finalize() is
  80. * usually unsuited for performing actions that must be
  81. * thread-safe, and that your implementation must be
  82. * use defensive programming if it is to always work.
  83. *
  84. * <p>If an Exception is thrown from finalize() during garbage
  85. * collection, it will be patently ignored and the Object will
  86. * still be destroyed.
  87. *
  88. * <p>It is allowed, although not typical, for user code to call
  89. * finalize() directly. User invocation does not affect whether
  90. * automatic invocation will occur. It is also permitted,
  91. * although not recommended, for a finalize() method to "revive"
  92. * an object by making it reachable from normal code again.
  93. *
  94. * <p>Unlike constructors, finalize() does not get called
  95. * for an object's superclass unless the implementation
  96. * specifically calls <code>super.finalize()</code>.
  97. *
  98. * <p>The default implementation does nothing.
  99. *
  100. * @throws Throwable permits a subclass to throw anything in an
  101. * overridden version; but the default throws nothing
  102. * @see System#gc()
  103. * @see System#runFinalizersOnExit(boolean)
  104. * @see java.lang.ref
  105. */
  106. // This must come first. See _JvObjectPrefix in Object.h.
  107. protected void finalize () throws Throwable
  108. {
  109. }
  110. /**
  111. * Returns the runtime {@link Class} of this Object.
  112. *
  113. * <p>The class object can also be obtained without a runtime
  114. * instance by using the class literal, as in:
  115. * <code>Foo.class</code>. Notice that the class literal
  116. * also works on primitive types, making it useful for
  117. * reflection purposes.
  118. *
  119. * @return the class of this Object
  120. */
  121. public final native Class<? extends Object> getClass();
  122. /**
  123. * Get a value that represents this Object, as uniquely as
  124. * possible within the confines of an int.
  125. *
  126. * <p>There are some requirements on this method which
  127. * subclasses must follow:<br>
  128. *
  129. * <ul>
  130. * <li>Semantic equality implies identical hashcodes. In other
  131. * words, if <code>a.equals(b)</code> is true, then
  132. * <code>a.hashCode() == b.hashCode()</code> must be as well.
  133. * However, the reverse is not necessarily true, and two
  134. * objects may have the same hashcode without being equal.</li>
  135. * <li>It must be consistent. Whichever value o.hashCode()
  136. * returns on the first invocation must be the value
  137. * returned on all later invocations as long as the object
  138. * exists. Notice, however, that the result of hashCode may
  139. * change between separate executions of a Virtual Machine,
  140. * because it is not invoked on the same object.</li>
  141. * </ul>
  142. *
  143. * <p>Notice that since <code>hashCode</code> is used in
  144. * {@link java.util.Hashtable} and other hashing classes,
  145. * a poor implementation will degrade the performance of hashing
  146. * (so don't blindly implement it as returning a constant!). Also,
  147. * if calculating the hash is time-consuming, a class may consider
  148. * caching the results.
  149. *
  150. * <p>The default implementation returns
  151. * <code>System.identityHashCode(this)</code>
  152. *
  153. * @return the hash code for this Object
  154. * @see #equals(Object)
  155. * @see System#identityHashCode(Object)
  156. */
  157. public native int hashCode();
  158. /**
  159. * Wakes up one of the {@link Thread}s that has called
  160. * <code>wait</code> on this Object. Only the owner
  161. * of a lock on this Object may call this method. This lock
  162. * is obtained by a <code>synchronized</code> method or statement.
  163. *
  164. * <p>The Thread to wake up is chosen arbitrarily. The
  165. * awakened thread is not guaranteed to be the next thread
  166. * to actually obtain the lock on this object.
  167. *
  168. * <p>This thread still holds a lock on the object, so it is
  169. * typical to release the lock by exiting the synchronized
  170. * code, calling wait(), or calling {@link Thread#sleep()}, so
  171. * that the newly awakened thread can actually resume. The
  172. * awakened thread will most likely be awakened with an
  173. * {@link InterruptedException}, but that is not guaranteed.
  174. *
  175. * @throws IllegalMonitorStateException if this Thread
  176. * does not own the lock on the Object
  177. * @see #notifyAll()
  178. * @see #wait()
  179. * @see #wait(long)
  180. * @see #wait(long, int)
  181. * @see Thread
  182. */
  183. public final native void notify();
  184. /**
  185. * Wakes up all of the {@link Thread}s that have called
  186. * <code>wait</code> on this Object. Only the owner
  187. * of a lock on this Object may call this method. This lock
  188. * is obtained by a <code>synchronized</code> method or statement.
  189. *
  190. * <p>There are no guarantees as to which thread will next
  191. * obtain the lock on the object.
  192. *
  193. * <p>This thread still holds a lock on the object, so it is
  194. * typical to release the lock by exiting the synchronized
  195. * code, calling wait(), or calling {@link Thread#sleep()}, so
  196. * that one of the newly awakened threads can actually resume.
  197. * The resuming thread will most likely be awakened with an
  198. * {@link InterruptedException}, but that is not guaranteed.
  199. *
  200. * @throws IllegalMonitorStateException if this Thread
  201. * does not own the lock on the Object
  202. * @see #notify()
  203. * @see #wait()
  204. * @see #wait(long)
  205. * @see #wait(long, int)
  206. * @see Thread
  207. */
  208. public final native void notifyAll();
  209. /**
  210. * Waits a specified amount of time (or indefinitely if
  211. * the time specified is 0) for someone to call notify()
  212. * or notifyAll() on this Object, waking up this Thread.
  213. *
  214. * <p>The Thread that calls wait must have a lock on this Object,
  215. * obtained by a <code>synchronized</code> method or statement.
  216. * After calling wait, the thread loses the lock on this
  217. * object until the method completes (abruptly or normally),
  218. * at which time it regains the lock. All locks held on
  219. * other objects remain in force, even though the thread is
  220. * inactive. Therefore, caution must be used to avoid deadlock.
  221. *
  222. * <p>Usually, this call will complete normally if the time
  223. * expires, or abruptly with {@link InterruptedException}
  224. * if another thread called notify, but neither result
  225. * is guaranteed.
  226. *
  227. * <p>The waiting period is nowhere near as precise as
  228. * nanoseconds; considering that even wait(int) is inaccurate,
  229. * how much can you expect? But on supporting
  230. * implementations, this offers somewhat more granularity
  231. * than milliseconds.
  232. *
  233. * @param ms the number of milliseconds to wait (1,000
  234. * milliseconds = 1 second)
  235. * @param ns the number of nanoseconds to wait over and
  236. * above ms (1,000,000 nanoseconds = 1 millisecond)
  237. * @throws IllegalArgumentException if ms &lt; 0 or ns is not
  238. * in the range 0 to 999,999
  239. * @throws IllegalMonitorStateException if this Thread
  240. * does not own a lock on this Object
  241. * @throws InterruptedException if some other Thread
  242. * interrupts this Thread
  243. * @see #notify()
  244. * @see #notifyAll()
  245. * @see #wait()
  246. * @see #wait(long)
  247. * @see Thread
  248. */
  249. public final native void wait(long timeout, int nanos)
  250. throws InterruptedException;
  251. /**
  252. * Determine whether this Object is semantically equal
  253. * to another Object.
  254. *
  255. * <p>There are some fairly strict requirements on this
  256. * method which subclasses must follow:<br>
  257. * <ul>
  258. * <li>It must be transitive. If <code>a.equals(b)</code> and
  259. * <code>b.equals(c)</code>, then <code>a.equals(c)</code>
  260. * must be true as well.</li>
  261. * <li>It must be symmetric. <code>a.equals(b)</code> and
  262. * <code>b.equals(a)</code> must have the same value.</li>
  263. * <li>It must be reflexive. <code>a.equals(a)</code> must
  264. * always be true.</li>
  265. * <li>It must be consistent. Whichever value a.equals(b)
  266. * returns on the first invocation must be the value
  267. * returned on all later invocations.</li>
  268. * <li><code>a.equals(null)</code> must be false.</li>
  269. * <li>It must be consistent with hashCode(). That is,
  270. * <code>a.equals(b)</code> must imply
  271. * <code>a.hashCode() == b.hashCode()</code>.
  272. * The reverse is not true; two objects that are not
  273. * equal may have the same hashcode, but that has
  274. * the potential to harm hashing performance.</li>
  275. * </ul>
  276. *
  277. * <p>This is typically overridden to throw a {@link ClassCastException}
  278. * if the argument is not comparable to the class performing
  279. * the comparison, but that is not a requirement. It is legal
  280. * for <code>a.equals(b)</code> to be true even though
  281. * <code>a.getClass() != b.getClass()</code>. Also, it
  282. * is typical to never cause a {@link NullPointerException}.
  283. *
  284. * <p>In general, the Collections API ({@link java.util}) use the
  285. * <code>equals</code> method rather than the <code>==</code>
  286. * operator to compare objects. However, {@link java.util.IdentityHashMap}
  287. * is an exception to this rule, for its own good reasons.
  288. *
  289. * <p>The default implementation returns <code>this == o</code>.
  290. *
  291. * @param obj the Object to compare to
  292. * @return whether this Object is semantically equal to another
  293. * @see #hashCode()
  294. */
  295. public boolean equals(Object obj)
  296. {
  297. return this == obj;
  298. }
  299. /**
  300. * The basic constructor. Object is special, because it has no
  301. * superclass, so there is no call to super().
  302. *
  303. * @throws OutOfMemoryError Technically, this constructor never
  304. * throws an OutOfMemoryError, because the memory has
  305. * already been allocated by this point. But as all
  306. * instance creation expressions eventually trace back
  307. * to this constructor, and creating an object allocates
  308. * memory, we list that possibility here.
  309. */
  310. public Object()
  311. {
  312. }
  313. /**
  314. * Convert this Object to a human-readable String.
  315. * There are no limits placed on how long this String
  316. * should be or what it should contain. We suggest you
  317. * make it as intuitive as possible to be able to place
  318. * it into {@link java.io.PrintStream#println() System.out.println()}
  319. * and such.
  320. *
  321. * <p>It is typical, but not required, to ensure that this method
  322. * never completes abruptly with a {@link RuntimeException}.
  323. *
  324. * <p>This method will be called when performing string
  325. * concatenation with this object. If the result is
  326. * <code>null</code>, string concatenation will instead
  327. * use <code>"null"</code>.
  328. *
  329. * <p>The default implementation returns
  330. * <code>getClass().getName() + "@" +
  331. * Integer.toHexString(hashCode())</code>.
  332. *
  333. * @return the String representing this Object, which may be null
  334. * @throws OutOfMemoryError The default implementation creates a new
  335. * String object, therefore it must allocate memory
  336. * @see #getClass()
  337. * @see #hashCode()
  338. * @see Class#getName()
  339. * @see Integer#toHexString(int)
  340. */
  341. public String toString()
  342. {
  343. return getClass().getName() + '@' + Integer.toHexString(hashCode());
  344. }
  345. /**
  346. * Waits indefinitely for notify() or notifyAll() to be
  347. * called on the Object in question. Implementation is
  348. * identical to wait(0).
  349. *
  350. * <p>The Thread that calls wait must have a lock on this Object,
  351. * obtained by a <code>synchronized</code> method or statement.
  352. * After calling wait, the thread loses the lock on this
  353. * object until the method completes (abruptly or normally),
  354. * at which time it regains the lock. All locks held on
  355. * other objects remain in force, even though the thread is
  356. * inactive. Therefore, caution must be used to avoid deadlock.
  357. *
  358. * <p>While it is typical that this method will complete abruptly
  359. * with an {@link InterruptedException}, it is not guaranteed. So,
  360. * it is typical to call wait inside an infinite loop:<br>
  361. *
  362. * <pre>
  363. * try
  364. * {
  365. * while (true)
  366. * lock.wait();
  367. * }
  368. * catch (InterruptedException e)
  369. * {
  370. * }
  371. * </pre>
  372. *
  373. * @throws IllegalMonitorStateException if this Thread
  374. * does not own a lock on this Object
  375. * @throws InterruptedException if some other Thread
  376. * interrupts this Thread
  377. * @see #notify()
  378. * @see #notifyAll()
  379. * @see #wait(long)
  380. * @see #wait(long, int)
  381. * @see Thread
  382. */
  383. public final void wait() throws InterruptedException
  384. {
  385. wait(0, 0);
  386. }
  387. /**
  388. * Waits a specified amount of time (or indefinitely if
  389. * the time specified is 0) for someone to call notify()
  390. * or notifyAll() on this Object, waking up this Thread.
  391. *
  392. * <p>The Thread that calls wait must have a lock on this Object,
  393. * obtained by a <code>synchronized</code> method or statement.
  394. * After calling wait, the thread loses the lock on this
  395. * object until the method completes (abruptly or normally),
  396. * at which time it regains the lock. All locks held on
  397. * other objects remain in force, even though the thread is
  398. * inactive. Therefore, caution must be used to avoid deadlock.
  399. *
  400. * <p>Usually, this call will complete normally if the time
  401. * expires, or abruptly with {@link InterruptedException}
  402. * if another thread called notify, but neither result
  403. * is guaranteed.
  404. *
  405. * <p>The waiting period is only *roughly* the amount of time
  406. * you requested. It cannot be exact because of the overhead
  407. * of the call itself. Most Virtual Machiness treat the
  408. * argument as a lower limit on the time spent waiting, but
  409. * even that is not guaranteed. Besides, some other thread
  410. * may hold the lock on the object when the time expires, so
  411. * the current thread may still have to wait to reobtain the
  412. * lock.
  413. *
  414. * @param timeout the minimum number of milliseconds to wait (1000
  415. * milliseconds = 1 second), or 0 for an indefinite wait
  416. * @throws IllegalArgumentException if ms &lt; 0
  417. * @throws IllegalMonitorStateException if this Thread
  418. * does not own a lock on this Object
  419. * @throws InterruptedException if some other Thread
  420. * interrupts this Thread
  421. * @see #notify()
  422. * @see #notifyAll()
  423. * @see #wait()
  424. * @see #wait(long, int)
  425. * @see Thread
  426. */
  427. public final void wait(long timeout) throws InterruptedException
  428. {
  429. wait(timeout, 0);
  430. }
  431. /**
  432. * This method may be called to create a new copy of the
  433. * Object. The typical behavior is as follows:<br>
  434. * <ul>
  435. * <li><code>o == o.clone()</code> is false</li>
  436. * <li><code>o.getClass() == o.clone().getClass()</code>
  437. * is true</li>
  438. * <li><code>o.equals(o)</code> is true</li>
  439. * </ul>
  440. *
  441. * <p>However, these are not strict requirements, and may
  442. * be violated if necessary. Of the three requirements, the
  443. * last is the most commonly violated, particularly if the
  444. * subclass does not override {@link #equals(Object)}.
  445. *
  446. * <p>If the Object you call clone() on does not implement
  447. * {@link Cloneable} (which is a placeholder interface), then
  448. * a CloneNotSupportedException is thrown. Notice that
  449. * Object does not implement Cloneable; this method exists
  450. * as a convenience for subclasses that do.
  451. *
  452. * <p>Object's implementation of clone allocates space for the
  453. * new Object using the correct class, without calling any
  454. * constructors, and then fills in all of the new field values
  455. * with the old field values. Thus, it is a shallow copy.
  456. * However, subclasses are permitted to make a deep copy.
  457. *
  458. * <p>All array types implement Cloneable, and override
  459. * this method as follows (it should never fail):<br>
  460. * <pre>
  461. * public Object clone()
  462. * {
  463. * try
  464. * {
  465. * super.clone();
  466. * }
  467. * catch (CloneNotSupportedException e)
  468. * {
  469. * throw new InternalError(e.getMessage());
  470. * }
  471. * }
  472. * </pre>
  473. *
  474. * @return a copy of the Object
  475. * @throws CloneNotSupportedException If this Object does not
  476. * implement Cloneable
  477. * @throws OutOfMemoryError Since cloning involves memory allocation,
  478. * even though it may bypass constructors, you might run
  479. * out of memory
  480. * @see Cloneable
  481. */
  482. protected native Object clone() throws CloneNotSupportedException;
  483. // This initializes the sync_info member. It is here for
  484. // completeness (some day we'll be able to auto-generate Object.h).
  485. private final native void sync_init();
  486. // If we fail to find a method at class loading time we put the
  487. // vtable index of this method in its place: any attempt to call
  488. // that method will result in an error.
  489. void throwNoSuchMethodError()
  490. {
  491. throw new NoSuchMethodError("in " + getClass());
  492. }
  493. // Note that we don't mention the sync_info field here. If we do,
  494. // jc1 will not work correctly.
  495. }