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