rt/emul/mini/src/main/java/java/lang/Object.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Feb 2013 17:24:50 +0100
changeset 779 01f3c1314ca3
parent 772 d382dacfd73f
child 780 bf066a5ca2f7
permissions -rw-r--r--
Don't call Array.get(null). Just link against that class.
     1 /*
     2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import java.lang.reflect.Array;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
    31 
    32 /**
    33  * Class {@code Object} is the root of the class hierarchy.
    34  * Every class has {@code Object} as a superclass. All objects,
    35  * including arrays, implement the methods of this class.
    36  *
    37  * @author  unascribed
    38  * @see     java.lang.Class
    39  * @since   JDK1.0
    40  */
    41 @JavaScriptPrototype(container = "Object.prototype", prototype = "new Object")
    42 public class Object {
    43 
    44     private static void registerNatives() {
    45         boolean assertsOn = false;
    46         assert assertsOn = false;
    47         if (assertsOn) try {
    48             Array.get(null, 0);
    49         } catch (Throwable ex) {
    50             // ignore
    51         }
    52     }
    53     static {
    54         registerNatives();
    55     }
    56 
    57     /**
    58      * Returns the runtime class of this {@code Object}. The returned
    59      * {@code Class} object is the object that is locked by {@code
    60      * static synchronized} methods of the represented class.
    61      *
    62      * <p><b>The actual result type is {@code Class<? extends |X|>}
    63      * where {@code |X|} is the erasure of the static type of the
    64      * expression on which {@code getClass} is called.</b> For
    65      * example, no cast is required in this code fragment:</p>
    66      *
    67      * <p>
    68      * {@code Number n = 0;                             }<br>
    69      * {@code Class<? extends Number> c = n.getClass(); }
    70      * </p>
    71      *
    72      * @return The {@code Class} object that represents the runtime
    73      *         class of this object.
    74      * @see    Class Literals, section 15.8.2 of
    75      *         <cite>The Java&trade; Language Specification</cite>.
    76      */
    77     @JavaScriptBody(args={}, body="return this.constructor.$class;")
    78     public final native Class<?> getClass();
    79 
    80     /**
    81      * Returns a hash code value for the object. This method is
    82      * supported for the benefit of hash tables such as those provided by
    83      * {@link java.util.HashMap}.
    84      * <p>
    85      * The general contract of {@code hashCode} is:
    86      * <ul>
    87      * <li>Whenever it is invoked on the same object more than once during
    88      *     an execution of a Java application, the {@code hashCode} method
    89      *     must consistently return the same integer, provided no information
    90      *     used in {@code equals} comparisons on the object is modified.
    91      *     This integer need not remain consistent from one execution of an
    92      *     application to another execution of the same application.
    93      * <li>If two objects are equal according to the {@code equals(Object)}
    94      *     method, then calling the {@code hashCode} method on each of
    95      *     the two objects must produce the same integer result.
    96      * <li>It is <em>not</em> required that if two objects are unequal
    97      *     according to the {@link java.lang.Object#equals(java.lang.Object)}
    98      *     method, then calling the {@code hashCode} method on each of the
    99      *     two objects must produce distinct integer results.  However, the
   100      *     programmer should be aware that producing distinct integer results
   101      *     for unequal objects may improve the performance of hash tables.
   102      * </ul>
   103      * <p>
   104      * As much as is reasonably practical, the hashCode method defined by
   105      * class {@code Object} does return distinct integers for distinct
   106      * objects. (This is typically implemented by converting the internal
   107      * address of the object into an integer, but this implementation
   108      * technique is not required by the
   109      * Java<font size="-2"><sup>TM</sup></font> programming language.)
   110      *
   111      * @return  a hash code value for this object.
   112      * @see     java.lang.Object#equals(java.lang.Object)
   113      * @see     java.lang.System#identityHashCode
   114      */
   115     @JavaScriptBody(args = {}, body = 
   116         "if (this.$hashCode) return this.$hashCode;\n"
   117         + "var h = this.computeHashCode__I();\n"
   118         + "return this.$hashCode = h & h;"
   119     )
   120     public native int hashCode();
   121 
   122     @JavaScriptBody(args = {}, body = "return Math.random() * Math.pow(2, 32);")
   123     native int computeHashCode();
   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      */
   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      * @exception  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      */
   235     protected Object clone() throws CloneNotSupportedException {
   236         Object ret = clone(this);
   237         if (ret == null) {
   238             throw new CloneNotSupportedException(getClass().getName());
   239         }
   240         return ret;
   241     }
   242 
   243     @JavaScriptBody(args = "self", body = 
   244           "\nif (!self.$instOf_java_lang_Cloneable) {"
   245         + "\n  return null;"
   246         + "\n} else {"
   247         + "\n  var clone = self.constructor(true);"
   248         + "\n  var props = Object.getOwnPropertyNames(self);"
   249         + "\n  for (var i = 0; i < props.length; i++) {"
   250         + "\n    var p = props[i];"
   251         + "\n    clone[p] = self[p];"
   252         + "\n  };"
   253         + "\n  return clone;"
   254         + "\n}"
   255     )
   256     private static native Object clone(Object self) throws CloneNotSupportedException;
   257 
   258     /**
   259      * Returns a string representation of the object. In general, the
   260      * {@code toString} method returns a string that
   261      * "textually represents" this object. The result should
   262      * be a concise but informative representation that is easy for a
   263      * person to read.
   264      * It is recommended that all subclasses override this method.
   265      * <p>
   266      * The {@code toString} method for class {@code Object}
   267      * returns a string consisting of the name of the class of which the
   268      * object is an instance, the at-sign character `{@code @}', and
   269      * the unsigned hexadecimal representation of the hash code of the
   270      * object. In other words, this method returns a string equal to the
   271      * value of:
   272      * <blockquote>
   273      * <pre>
   274      * getClass().getName() + '@' + Integer.toHexString(hashCode())
   275      * </pre></blockquote>
   276      *
   277      * @return  a string representation of the object.
   278      */
   279     public String toString() {
   280         return getClass().getName() + "@" + Integer.toHexString(hashCode());
   281     }
   282 
   283     /**
   284      * Wakes up a single thread that is waiting on this object's
   285      * monitor. If any threads are waiting on this object, one of them
   286      * is chosen to be awakened. The choice is arbitrary and occurs at
   287      * the discretion of the implementation. A thread waits on an object's
   288      * monitor by calling one of the {@code wait} methods.
   289      * <p>
   290      * The awakened thread will not be able to proceed until the current
   291      * thread relinquishes the lock on this object. The awakened thread will
   292      * compete in the usual manner with any other threads that might be
   293      * actively competing to synchronize on this object; for example, the
   294      * awakened thread enjoys no reliable privilege or disadvantage in being
   295      * the next thread to lock this object.
   296      * <p>
   297      * This method should only be called by a thread that is the owner
   298      * of this object's monitor. A thread becomes the owner of the
   299      * object's monitor in one of three ways:
   300      * <ul>
   301      * <li>By executing a synchronized instance method of that object.
   302      * <li>By executing the body of a {@code synchronized} statement
   303      *     that synchronizes on the object.
   304      * <li>For objects of type {@code Class,} by executing a
   305      *     synchronized static method of that class.
   306      * </ul>
   307      * <p>
   308      * Only one thread at a time can own an object's monitor.
   309      *
   310      * @exception  IllegalMonitorStateException  if the current thread is not
   311      *               the owner of this object's monitor.
   312      * @see        java.lang.Object#notifyAll()
   313      * @see        java.lang.Object#wait()
   314      */
   315     public final native void notify();
   316 
   317     /**
   318      * Wakes up all threads that are waiting on this object's monitor. A
   319      * thread waits on an object's monitor by calling one of the
   320      * {@code wait} methods.
   321      * <p>
   322      * The awakened threads will not be able to proceed until the current
   323      * thread relinquishes the lock on this object. The awakened threads
   324      * will compete in the usual manner with any other threads that might
   325      * be actively competing to synchronize on this object; for example,
   326      * the awakened threads enjoy no reliable privilege or disadvantage in
   327      * being the next thread to lock this object.
   328      * <p>
   329      * This method should only be called by a thread that is the owner
   330      * of this object's monitor. See the {@code notify} method for a
   331      * description of the ways in which a thread can become the owner of
   332      * a monitor.
   333      *
   334      * @exception  IllegalMonitorStateException  if the current thread is not
   335      *               the owner of this object's monitor.
   336      * @see        java.lang.Object#notify()
   337      * @see        java.lang.Object#wait()
   338      */
   339     public final native void notifyAll();
   340 
   341     /**
   342      * Causes the current thread to wait until either another thread invokes the
   343      * {@link java.lang.Object#notify()} method or the
   344      * {@link java.lang.Object#notifyAll()} method for this object, or a
   345      * specified amount of time has elapsed.
   346      * <p>
   347      * The current thread must own this object's monitor.
   348      * <p>
   349      * This method causes the current thread (call it <var>T</var>) to
   350      * place itself in the wait set for this object and then to relinquish
   351      * any and all synchronization claims on this object. Thread <var>T</var>
   352      * becomes disabled for thread scheduling purposes and lies dormant
   353      * until one of four things happens:
   354      * <ul>
   355      * <li>Some other thread invokes the {@code notify} method for this
   356      * object and thread <var>T</var> happens to be arbitrarily chosen as
   357      * the thread to be awakened.
   358      * <li>Some other thread invokes the {@code notifyAll} method for this
   359      * object.
   360      * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
   361      * thread <var>T</var>.
   362      * <li>The specified amount of real time has elapsed, more or less.  If
   363      * {@code timeout} is zero, however, then real time is not taken into
   364      * consideration and the thread simply waits until notified.
   365      * </ul>
   366      * The thread <var>T</var> is then removed from the wait set for this
   367      * object and re-enabled for thread scheduling. It then competes in the
   368      * usual manner with other threads for the right to synchronize on the
   369      * object; once it has gained control of the object, all its
   370      * synchronization claims on the object are restored to the status quo
   371      * ante - that is, to the situation as of the time that the {@code wait}
   372      * method was invoked. Thread <var>T</var> then returns from the
   373      * invocation of the {@code wait} method. Thus, on return from the
   374      * {@code wait} method, the synchronization state of the object and of
   375      * thread {@code T} is exactly as it was when the {@code wait} method
   376      * was invoked.
   377      * <p>
   378      * A thread can also wake up without being notified, interrupted, or
   379      * timing out, a so-called <i>spurious wakeup</i>.  While this will rarely
   380      * occur in practice, applications must guard against it by testing for
   381      * the condition that should have caused the thread to be awakened, and
   382      * continuing to wait if the condition is not satisfied.  In other words,
   383      * waits should always occur in loops, like this one:
   384      * <pre>
   385      *     synchronized (obj) {
   386      *         while (&lt;condition does not hold&gt;)
   387      *             obj.wait(timeout);
   388      *         ... // Perform action appropriate to condition
   389      *     }
   390      * </pre>
   391      * (For more information on this topic, see Section 3.2.3 in Doug Lea's
   392      * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
   393      * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
   394      * Language Guide" (Addison-Wesley, 2001).
   395      *
   396      * <p>If the current thread is {@linkplain java.lang.Thread#interrupt()
   397      * interrupted} by any thread before or while it is waiting, then an
   398      * {@code InterruptedException} is thrown.  This exception is not
   399      * thrown until the lock status of this object has been restored as
   400      * described above.
   401      *
   402      * <p>
   403      * Note that the {@code wait} method, as it places the current thread
   404      * into the wait set for this object, unlocks only this object; any
   405      * other objects on which the current thread may be synchronized remain
   406      * locked while the thread waits.
   407      * <p>
   408      * This method should only be called by a thread that is the owner
   409      * of this object's monitor. See the {@code notify} method for a
   410      * description of the ways in which a thread can become the owner of
   411      * a monitor.
   412      *
   413      * @param      timeout   the maximum time to wait in milliseconds.
   414      * @exception  IllegalArgumentException      if the value of timeout is
   415      *               negative.
   416      * @exception  IllegalMonitorStateException  if the current thread is not
   417      *               the owner of the object's monitor.
   418      * @exception  InterruptedException if any thread interrupted the
   419      *             current thread before or while the current thread
   420      *             was waiting for a notification.  The <i>interrupted
   421      *             status</i> of the current thread is cleared when
   422      *             this exception is thrown.
   423      * @see        java.lang.Object#notify()
   424      * @see        java.lang.Object#notifyAll()
   425      */
   426     public final native void wait(long timeout) throws InterruptedException;
   427 
   428     /**
   429      * Causes the current thread to wait until another thread invokes the
   430      * {@link java.lang.Object#notify()} method or the
   431      * {@link java.lang.Object#notifyAll()} method for this object, or
   432      * some other thread interrupts the current thread, or a certain
   433      * amount of real time has elapsed.
   434      * <p>
   435      * This method is similar to the {@code wait} method of one
   436      * argument, but it allows finer control over the amount of time to
   437      * wait for a notification before giving up. The amount of real time,
   438      * measured in nanoseconds, is given by:
   439      * <blockquote>
   440      * <pre>
   441      * 1000000*timeout+nanos</pre></blockquote>
   442      * <p>
   443      * In all other respects, this method does the same thing as the
   444      * method {@link #wait(long)} of one argument. In particular,
   445      * {@code wait(0, 0)} means the same thing as {@code wait(0)}.
   446      * <p>
   447      * The current thread must own this object's monitor. The thread
   448      * releases ownership of this monitor and waits until either of the
   449      * following two conditions has occurred:
   450      * <ul>
   451      * <li>Another thread notifies threads waiting on this object's monitor
   452      *     to wake up either through a call to the {@code notify} method
   453      *     or the {@code notifyAll} method.
   454      * <li>The timeout period, specified by {@code timeout}
   455      *     milliseconds plus {@code nanos} nanoseconds arguments, has
   456      *     elapsed.
   457      * </ul>
   458      * <p>
   459      * The thread then waits until it can re-obtain ownership of the
   460      * monitor and resumes execution.
   461      * <p>
   462      * As in the one argument version, interrupts and spurious wakeups are
   463      * possible, and this method should always be used in a loop:
   464      * <pre>
   465      *     synchronized (obj) {
   466      *         while (&lt;condition does not hold&gt;)
   467      *             obj.wait(timeout, nanos);
   468      *         ... // Perform action appropriate to condition
   469      *     }
   470      * </pre>
   471      * This method should only be called by a thread that is the owner
   472      * of this object's monitor. See the {@code notify} method for a
   473      * description of the ways in which a thread can become the owner of
   474      * a monitor.
   475      *
   476      * @param      timeout   the maximum time to wait in milliseconds.
   477      * @param      nanos      additional time, in nanoseconds range
   478      *                       0-999999.
   479      * @exception  IllegalArgumentException      if the value of timeout is
   480      *                      negative or the value of nanos is
   481      *                      not in the range 0-999999.
   482      * @exception  IllegalMonitorStateException  if the current thread is not
   483      *               the owner of this object's monitor.
   484      * @exception  InterruptedException if any thread interrupted the
   485      *             current thread before or while the current thread
   486      *             was waiting for a notification.  The <i>interrupted
   487      *             status</i> of the current thread is cleared when
   488      *             this exception is thrown.
   489      */
   490     public final void wait(long timeout, int nanos) throws InterruptedException {
   491         if (timeout < 0) {
   492             throw new IllegalArgumentException("timeout value is negative");
   493         }
   494 
   495         if (nanos < 0 || nanos > 999999) {
   496             throw new IllegalArgumentException(
   497                                 "nanosecond timeout value out of range");
   498         }
   499 
   500         if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
   501             timeout++;
   502         }
   503 
   504         wait(timeout);
   505     }
   506 
   507     /**
   508      * Causes the current thread to wait until another thread invokes the
   509      * {@link java.lang.Object#notify()} method or the
   510      * {@link java.lang.Object#notifyAll()} method for this object.
   511      * In other words, this method behaves exactly as if it simply
   512      * performs the call {@code wait(0)}.
   513      * <p>
   514      * The current thread must own this object's monitor. The thread
   515      * releases ownership of this monitor and waits until another thread
   516      * notifies threads waiting on this object's monitor to wake up
   517      * either through a call to the {@code notify} method or the
   518      * {@code notifyAll} method. The thread then waits until it can
   519      * re-obtain ownership of the monitor and resumes execution.
   520      * <p>
   521      * As in the one argument version, interrupts and spurious wakeups are
   522      * possible, and this method should always be used in a loop:
   523      * <pre>
   524      *     synchronized (obj) {
   525      *         while (&lt;condition does not hold&gt;)
   526      *             obj.wait();
   527      *         ... // Perform action appropriate to condition
   528      *     }
   529      * </pre>
   530      * This method should only be called by a thread that is the owner
   531      * of this object's monitor. See the {@code notify} method for a
   532      * description of the ways in which a thread can become the owner of
   533      * a monitor.
   534      *
   535      * @exception  IllegalMonitorStateException  if the current thread is not
   536      *               the owner of the object's monitor.
   537      * @exception  InterruptedException if any thread interrupted the
   538      *             current thread before or while the current thread
   539      *             was waiting for a notification.  The <i>interrupted
   540      *             status</i> of the current thread is cleared when
   541      *             this exception is thrown.
   542      * @see        java.lang.Object#notify()
   543      * @see        java.lang.Object#notifyAll()
   544      */
   545     public final void wait() throws InterruptedException {
   546         wait(0);
   547     }
   548 
   549     /**
   550      * Called by the garbage collector on an object when garbage collection
   551      * determines that there are no more references to the object.
   552      * A subclass overrides the {@code finalize} method to dispose of
   553      * system resources or to perform other cleanup.
   554      * <p>
   555      * The general contract of {@code finalize} is that it is invoked
   556      * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
   557      * machine has determined that there is no longer any
   558      * means by which this object can be accessed by any thread that has
   559      * not yet died, except as a result of an action taken by the
   560      * finalization of some other object or class which is ready to be
   561      * finalized. The {@code finalize} method may take any action, including
   562      * making this object available again to other threads; the usual purpose
   563      * of {@code finalize}, however, is to perform cleanup actions before
   564      * the object is irrevocably discarded. For example, the finalize method
   565      * for an object that represents an input/output connection might perform
   566      * explicit I/O transactions to break the connection before the object is
   567      * permanently discarded.
   568      * <p>
   569      * The {@code finalize} method of class {@code Object} performs no
   570      * special action; it simply returns normally. Subclasses of
   571      * {@code Object} may override this definition.
   572      * <p>
   573      * The Java programming language does not guarantee which thread will
   574      * invoke the {@code finalize} method for any given object. It is
   575      * guaranteed, however, that the thread that invokes finalize will not
   576      * be holding any user-visible synchronization locks when finalize is
   577      * invoked. If an uncaught exception is thrown by the finalize method,
   578      * the exception is ignored and finalization of that object terminates.
   579      * <p>
   580      * After the {@code finalize} method has been invoked for an object, no
   581      * further action is taken until the Java virtual machine has again
   582      * determined that there is no longer any means by which this object can
   583      * be accessed by any thread that has not yet died, including possible
   584      * actions by other objects or classes which are ready to be finalized,
   585      * at which point the object may be discarded.
   586      * <p>
   587      * The {@code finalize} method is never invoked more than once by a Java
   588      * virtual machine for any given object.
   589      * <p>
   590      * Any exception thrown by the {@code finalize} method causes
   591      * the finalization of this object to be halted, but is otherwise
   592      * ignored.
   593      *
   594      * @throws Throwable the {@code Exception} raised by this method
   595      */
   596     protected void finalize() throws Throwable { }
   597 }