1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1994, 2021, 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 import jdk.internal.vm.annotation.IntrinsicCandidate;
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  * @see     java.lang.Class
38  * @since   1.0
39  */
40 public class Object {
41 
42     // Android-removed: registerNatives() not used on Android
43     // private static native void registerNatives();
44     // static {
45     //     registerNatives();
46     // }
47 
48     // Android-added: Use Android specific fields for Class and monitor.
49     private transient Class<?> shadow$_klass_;
50     private transient int shadow$_monitor_;
51 
52 
53     /**
54      * Constructs a new object.
55      */
56     @IntrinsicCandidate
Object()57     public Object() {}
58 
59     /**
60      * Returns the runtime class of this {@code Object}. The returned
61      * {@code Class} object is the object that is locked by {@code
62      * static synchronized} methods of the represented class.
63      *
64      * <p><b>The actual result type is {@code Class<? extends |X|>}
65      * where {@code |X|} is the erasure of the static type of the
66      * expression on which {@code getClass} is called.</b> For
67      * example, no cast is required in this code fragment:</p>
68      *
69      * <p>
70      * {@code Number n = 0;                             }<br>
71      * {@code Class<? extends Number> c = n.getClass(); }
72      * </p>
73      *
74      * @return The {@code Class} object that represents the runtime
75      *         class of this object.
76      * @jls 15.8.2 Class Literals
77      */
78     @IntrinsicCandidate
79     // Android-changed: Use Android specific fields for Class and monitor.
80     // public final native Class<?> getClass();
getClass()81     public final Class<?> getClass() {
82       return shadow$_klass_;
83     }
84 
85     /**
86      * Returns a hash code value for the object. This method is
87      * supported for the benefit of hash tables such as those provided by
88      * {@link java.util.HashMap}.
89      * <p>
90      * The general contract of {@code hashCode} is:
91      * <ul>
92      * <li>Whenever it is invoked on the same object more than once during
93      *     an execution of a Java application, the {@code hashCode} method
94      *     must consistently return the same integer, provided no information
95      *     used in {@code equals} comparisons on the object is modified.
96      *     This integer need not remain consistent from one execution of an
97      *     application to another execution of the same application.
98      * <li>If two objects are equal according to the {@link
99      *     #equals(Object) equals} method, then calling the {@code
100      *     hashCode} method on each of the two objects must produce the
101      *     same integer result.
102      * <li>It is <em>not</em> required that if two objects are unequal
103      *     according to the {@link #equals(Object) equals} method, then
104      *     calling the {@code hashCode} method on each of the two objects
105      *     must produce distinct integer results.  However, the programmer
106      *     should be aware that producing distinct integer results for
107      *     unequal objects may improve the performance of hash tables.
108      * </ul>
109      *
110      * @implSpec
111      * As far as is reasonably practical, the {@code hashCode} method defined
112      * by class {@code Object} returns distinct integers for distinct objects.
113      *
114      * @return  a hash code value for this object.
115      * @see     java.lang.Object#equals(java.lang.Object)
116      * @see     java.lang.System#identityHashCode
117      */
118     @IntrinsicCandidate
119     // BEGIN Android-changed: Added a local helper for identityHashCode.
120     // public native int hashCode();
hashCode()121     public int hashCode() {
122         return identityHashCode(this);
123     }
124 
125     // Package-private to be used by j.l.System. We do the implementation here
126     // to avoid Object.hashCode doing a clinit check on j.l.System, and also
127     // to avoid leaking shadow$_monitor_ outside of this class.
identityHashCode(Object obj)128     /* package-private */ static int identityHashCode(Object obj) {
129         int lockWord = obj.shadow$_monitor_;
130         final int lockWordStateMask = 0xC0000000;  // Top 2 bits.
131         final int lockWordStateHash = 0x80000000;  // Top 2 bits are value 2 (kStateHash).
132         final int lockWordHashMask = 0x0FFFFFFF;  // Low 28 bits.
133         if ((lockWord & lockWordStateMask) == lockWordStateHash) {
134             return lockWord & lockWordHashMask;
135         }
136         return identityHashCodeNative(obj);
137     }
138 
139     /**
140      * Return the identity hash code when the information in the monitor field
141      * is not sufficient.
142      */
143     @FastNative
identityHashCodeNative(Object obj)144     private static native int identityHashCodeNative(Object obj);
145     // END Android-changed: Added a local helper for identityHashCode.
146 
147     /**
148      * Indicates whether some other object is "equal to" this one.
149      * <p>
150      * The {@code equals} method implements an equivalence relation
151      * on non-null object references:
152      * <ul>
153      * <li>It is <i>reflexive</i>: for any non-null reference value
154      *     {@code x}, {@code x.equals(x)} should return
155      *     {@code true}.
156      * <li>It is <i>symmetric</i>: for any non-null reference values
157      *     {@code x} and {@code y}, {@code x.equals(y)}
158      *     should return {@code true} if and only if
159      *     {@code y.equals(x)} returns {@code true}.
160      * <li>It is <i>transitive</i>: for any non-null reference values
161      *     {@code x}, {@code y}, and {@code z}, if
162      *     {@code x.equals(y)} returns {@code true} and
163      *     {@code y.equals(z)} returns {@code true}, then
164      *     {@code x.equals(z)} should return {@code true}.
165      * <li>It is <i>consistent</i>: for any non-null reference values
166      *     {@code x} and {@code y}, multiple invocations of
167      *     {@code x.equals(y)} consistently return {@code true}
168      *     or consistently return {@code false}, provided no
169      *     information used in {@code equals} comparisons on the
170      *     objects is modified.
171      * <li>For any non-null reference value {@code x},
172      *     {@code x.equals(null)} should return {@code false}.
173      * </ul>
174      *
175      * <p>
176      * An equivalence relation partitions the elements it operates on
177      * into <i>equivalence classes</i>; all the members of an
178      * equivalence class are equal to each other. Members of an
179      * equivalence class are substitutable for each other, at least
180      * for some purposes.
181      *
182      * @implSpec
183      * The {@code equals} method for class {@code Object} implements
184      * the most discriminating possible equivalence relation on objects;
185      * that is, for any non-null reference values {@code x} and
186      * {@code y}, this method returns {@code true} if and only
187      * if {@code x} and {@code y} refer to the same object
188      * ({@code x == y} has the value {@code true}).
189      *
190      * In other words, under the reference equality equivalence
191      * relation, each equivalence class only has a single element.
192      *
193      * @apiNote
194      * It is generally necessary to override the {@link #hashCode hashCode}
195      * method whenever this method is overridden, so as to maintain the
196      * general contract for the {@code hashCode} method, which states
197      * that equal objects must have equal hash codes.
198      *
199      * @param   obj   the reference object with which to compare.
200      * @return  {@code true} if this object is the same as the obj
201      *          argument; {@code false} otherwise.
202      * @see     #hashCode()
203      * @see     java.util.HashMap
204      */
equals(Object obj)205     public boolean equals(Object obj) {
206         return (this == obj);
207     }
208 
209     /**
210      * Creates and returns a copy of this object.  The precise meaning
211      * of "copy" may depend on the class of the object. The general
212      * intent is that, for any object {@code x}, the expression:
213      * <blockquote>
214      * <pre>
215      * x.clone() != x</pre></blockquote>
216      * will be true, and that the expression:
217      * <blockquote>
218      * <pre>
219      * x.clone().getClass() == x.getClass()</pre></blockquote>
220      * will be {@code true}, but these are not absolute requirements.
221      * While it is typically the case that:
222      * <blockquote>
223      * <pre>
224      * x.clone().equals(x)</pre></blockquote>
225      * will be {@code true}, this is not an absolute requirement.
226      * <p>
227      * By convention, the returned object should be obtained by calling
228      * {@code super.clone}.  If a class and all of its superclasses (except
229      * {@code Object}) obey this convention, it will be the case that
230      * {@code x.clone().getClass() == x.getClass()}.
231      * <p>
232      * By convention, the object returned by this method should be independent
233      * of this object (which is being cloned).  To achieve this independence,
234      * it may be necessary to modify one or more fields of the object returned
235      * by {@code super.clone} before returning it.  Typically, this means
236      * copying any mutable objects that comprise the internal "deep structure"
237      * of the object being cloned and replacing the references to these
238      * objects with references to the copies.  If a class contains only
239      * primitive fields or references to immutable objects, then it is usually
240      * the case that no fields in the object returned by {@code super.clone}
241      * need to be modified.
242      *
243      * @implSpec
244      * The method {@code clone} for class {@code Object} performs a
245      * specific cloning operation. First, if the class of this object does
246      * not implement the interface {@code Cloneable}, then a
247      * {@code CloneNotSupportedException} is thrown. Note that all arrays
248      * are considered to implement the interface {@code Cloneable} and that
249      * the return type of the {@code clone} method of an array type {@code T[]}
250      * is {@code T[]} where T is any reference or primitive type.
251      * Otherwise, this method creates a new instance of the class of this
252      * object and initializes all its fields with exactly the contents of
253      * the corresponding fields of this object, as if by assignment; the
254      * contents of the fields are not themselves cloned. Thus, this method
255      * performs a "shallow copy" of this object, not a "deep copy" operation.
256      * <p>
257      * The class {@code Object} does not itself implement the interface
258      * {@code Cloneable}, so calling the {@code clone} method on an object
259      * whose class is {@code Object} will result in throwing an
260      * exception at run time.
261      *
262      * @return     a clone of this instance.
263      * @throws  CloneNotSupportedException  if the object's class does not
264      *               support the {@code Cloneable} interface. Subclasses
265      *               that override the {@code clone} method can also
266      *               throw this exception to indicate that an instance cannot
267      *               be cloned.
268      * @see java.lang.Cloneable
269      */
270     @IntrinsicCandidate
271     // BEGIN Android-changed: Use native local helper for clone()
272     // Checks whether cloning is allowed before calling native local helper.
273     // protected native Object clone() throws CloneNotSupportedException;
clone()274     protected Object clone() throws CloneNotSupportedException {
275         if (!(this instanceof Cloneable)) {
276             throw new CloneNotSupportedException("Class " + getClass().getName() +
277                                                  " doesn't implement Cloneable");
278         }
279 
280         return internalClone();
281     }
282 
283     /*
284      * Native helper method for cloning.
285      */
286     @FastNative
internalClone()287     private native Object internalClone();
288     // END Android-changed: Use native local helper for clone()
289 
290     /**
291      * Returns a string representation of the object.
292      * @apiNote
293      * In general, the
294      * {@code toString} method returns a string that
295      * "textually represents" this object. The result should
296      * be a concise but informative representation that is easy for a
297      * person to read.
298      * It is recommended that all subclasses override this method.
299      * The string output is not necessarily stable over time or across
300      * JVM invocations.
301      * @implSpec
302      * The {@code toString} method for class {@code Object}
303      * returns a string consisting of the name of the class of which the
304      * object is an instance, the at-sign character `{@code @}', and
305      * the unsigned hexadecimal representation of the hash code of the
306      * object. In other words, this method returns a string equal to the
307      * value of:
308      * <blockquote>
309      * <pre>
310      * getClass().getName() + '@' + Integer.toHexString(hashCode())
311      * </pre></blockquote>
312      *
313      * @return  a string representation of the object.
314      */
toString()315     public String toString() {
316         return getClass().getName() + "@" + Integer.toHexString(hashCode());
317     }
318 
319     /**
320      * Wakes up a single thread that is waiting on this object's
321      * monitor. If any threads are waiting on this object, one of them
322      * is chosen to be awakened. The choice is arbitrary and occurs at
323      * the discretion of the implementation. A thread waits on an object's
324      * monitor by calling one of the {@code wait} methods.
325      * <p>
326      * The awakened thread will not be able to proceed until the current
327      * thread relinquishes the lock on this object. The awakened thread will
328      * compete in the usual manner with any other threads that might be
329      * actively competing to synchronize on this object; for example, the
330      * awakened thread enjoys no reliable privilege or disadvantage in being
331      * the next thread to lock this object.
332      * <p>
333      * This method should only be called by a thread that is the owner
334      * of this object's monitor. A thread becomes the owner of the
335      * object's monitor in one of three ways:
336      * <ul>
337      * <li>By executing a synchronized instance method of that object.
338      * <li>By executing the body of a {@code synchronized} statement
339      *     that synchronizes on the object.
340      * <li>For objects of type {@code Class,} by executing a
341      *     synchronized static method of that class.
342      * </ul>
343      * <p>
344      * Only one thread at a time can own an object's monitor.
345      *
346      * @throws  IllegalMonitorStateException  if the current thread is not
347      *               the owner of this object's monitor.
348      * @see        java.lang.Object#notifyAll()
349      * @see        java.lang.Object#wait()
350      */
351     @FastNative
352     @IntrinsicCandidate
notify()353     public final native void notify();
354 
355     /**
356      * Wakes up all threads that are waiting on this object's monitor. A
357      * thread waits on an object's monitor by calling one of the
358      * {@code wait} methods.
359      * <p>
360      * The awakened threads will not be able to proceed until the current
361      * thread relinquishes the lock on this object. The awakened threads
362      * will compete in the usual manner with any other threads that might
363      * be actively competing to synchronize on this object; for example,
364      * the awakened threads enjoy no reliable privilege or disadvantage in
365      * being the next thread to lock this object.
366      * <p>
367      * This method should only be called by a thread that is the owner
368      * of this object's monitor. See the {@code notify} method for a
369      * description of the ways in which a thread can become the owner of
370      * a monitor.
371      *
372      * @throws  IllegalMonitorStateException  if the current thread is not
373      *               the owner of this object's monitor.
374      * @see        java.lang.Object#notify()
375      * @see        java.lang.Object#wait()
376      */
377     @FastNative
378     @IntrinsicCandidate
notifyAll()379     public final native void notifyAll();
380 
381     /**
382      * Causes the current thread to wait until it is awakened, typically
383      * by being <em>notified</em> or <em>interrupted</em>, or until a
384      * certain amount of real time has elapsed.
385      * <p>
386      * In all respects, this method behaves as if {@code wait(timeoutMillis, 0)}
387      * had been called. See the specification of the {@link #wait(long, int)} method
388      * for details.
389      *
390      * @param  timeoutMillis the maximum time to wait, in milliseconds
391      * @throws IllegalArgumentException if {@code timeoutMillis} is negative
392      * @throws IllegalMonitorStateException if the current thread is not
393      *         the owner of the object's monitor
394      * @throws InterruptedException if any thread interrupted the current thread before or
395      *         while the current thread was waiting. The <em>interrupted status</em> of the
396      *         current thread is cleared when this exception is thrown.
397      * @see    #notify()
398      * @see    #notifyAll()
399      * @see    #wait()
400      * @see    #wait(long, int)
401      */
402     // Android-changed: Implement wait(long) non-natively.
403     // public final native void wait(long timeoutMillis) throws InterruptedException;
wait(long timeoutMillis)404     public final void wait(long timeoutMillis) throws InterruptedException {
405         wait(timeoutMillis, 0);
406     }
407 
408     /**
409      * Causes the current thread to wait until it is awakened, typically
410      * by being <em>notified</em> or <em>interrupted</em>, or until a
411      * certain amount of real time has elapsed.
412      * <p>
413      * The current thread must own this object's monitor lock. See the
414      * {@link #notify notify} method for a description of the ways in which
415      * a thread can become the owner of a monitor lock.
416      * <p>
417      * This method causes the current thread (referred to here as <var>T</var>) to
418      * place itself in the wait set for this object and then to relinquish any
419      * and all synchronization claims on this object. Note that only the locks
420      * on this object are relinquished; any other objects on which the current
421      * thread may be synchronized remain locked while the thread waits.
422      * <p>
423      * Thread <var>T</var> then becomes disabled for thread scheduling purposes
424      * and lies dormant until one of the following occurs:
425      * <ul>
426      * <li>Some other thread invokes the {@code notify} method for this
427      * object and thread <var>T</var> happens to be arbitrarily chosen as
428      * the thread to be awakened.
429      * <li>Some other thread invokes the {@code notifyAll} method for this
430      * object.
431      * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
432      * thread <var>T</var>.
433      * <li>The specified amount of real time has elapsed, more or less.
434      * The amount of real time, in nanoseconds, is given by the expression
435      * {@code 1000000 * timeoutMillis + nanos}. If {@code timeoutMillis} and {@code nanos}
436      * are both zero, then real time is not taken into consideration and the
437      * thread waits until awakened by one of the other causes.
438      * <li>Thread <var>T</var> is awakened spuriously. (See below.)
439      * </ul>
440      * <p>
441      * The thread <var>T</var> is then removed from the wait set for this
442      * object and re-enabled for thread scheduling. It competes in the
443      * usual manner with other threads for the right to synchronize on the
444      * object; once it has regained control of the object, all its
445      * synchronization claims on the object are restored to the status quo
446      * ante - that is, to the situation as of the time that the {@code wait}
447      * method was invoked. Thread <var>T</var> then returns from the
448      * invocation of the {@code wait} method. Thus, on return from the
449      * {@code wait} method, the synchronization state of the object and of
450      * thread {@code T} is exactly as it was when the {@code wait} method
451      * was invoked.
452      * <p>
453      * A thread can wake up without being notified, interrupted, or timing out, a
454      * so-called <em>spurious wakeup</em>.  While this will rarely occur in practice,
455      * applications must guard against it by testing for the condition that should
456      * have caused the thread to be awakened, and continuing to wait if the condition
457      * is not satisfied. See the example below.
458      * <p>
459      * For more information on this topic, see section 14.2,
460      * "Condition Queues," in Brian Goetz and others' <em>Java Concurrency
461      * in Practice</em> (Addison-Wesley, 2006) or Item 69 in Joshua
462      * Bloch's <em>Effective Java, Second Edition</em> (Addison-Wesley,
463      * 2008).
464      * <p>
465      * If the current thread is {@linkplain java.lang.Thread#interrupt() interrupted}
466      * by any thread before or while it is waiting, then an {@code InterruptedException}
467      * is thrown.  The <em>interrupted status</em> of the current thread is cleared when
468      * this exception is thrown. This exception is not thrown until the lock status of
469      * this object has been restored as described above.
470      *
471      * @apiNote
472      * The recommended approach to waiting is to check the condition being awaited in
473      * a {@code while} loop around the call to {@code wait}, as shown in the example
474      * below. Among other things, this approach avoids problems that can be caused
475      * by spurious wakeups.
476      *
477      * <pre>{@code
478      *     synchronized (obj) {
479      *         while (<condition does not hold> and <timeout not exceeded>) {
480      *             long timeoutMillis = ... ; // recompute timeout values
481      *             int nanos = ... ;
482      *             obj.wait(timeoutMillis, nanos);
483      *         }
484      *         ... // Perform action appropriate to condition or timeout
485      *     }
486      * }</pre>
487      *
488      * @param  timeoutMillis the maximum time to wait, in milliseconds
489      * @param  nanos   additional time, in nanoseconds, in the range 0-999999 inclusive
490      * @throws IllegalArgumentException if {@code timeoutMillis} is negative,
491      *         or if the value of {@code nanos} is out of range
492      * @throws IllegalMonitorStateException if the current thread is not
493      *         the owner of the object's monitor
494      * @throws InterruptedException if any thread interrupted the current thread before or
495      *         while the current thread was waiting. The <em>interrupted status</em> of the
496      *         current thread is cleared when this exception is thrown.
497      * @see    #notify()
498      * @see    #notifyAll()
499      * @see    #wait()
500      * @see    #wait(long)
501      */
502     // Android-changed: Implement wait(long, int) natively.
503     /*
504     public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
505         if (timeoutMillis < 0) {
506             throw new IllegalArgumentException("timeoutMillis value is negative");
507         }
508 
509         if (nanos < 0 || nanos > 999999) {
510             throw new IllegalArgumentException(
511                                 "nanosecond timeout value out of range");
512         }
513 
514         if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
515             timeoutMillis++;
516         }
517 
518         wait(timeoutMillis);
519     }
520     */
521     @FastNative
wait(long timeoutMillis, int nanos)522     public final native void wait(long timeoutMillis, int nanos) throws InterruptedException;
523 
524     /**
525      * Causes the current thread to wait until it is awakened, typically
526      * by being <em>notified</em> or <em>interrupted</em>.
527      * <p>
528      * In all respects, this method behaves as if {@code wait(0L, 0)}
529      * had been called. See the specification of the {@link #wait(long, int)} method
530      * for details.
531      *
532      * @throws IllegalMonitorStateException if the current thread is not
533      *         the owner of the object's monitor
534      * @throws InterruptedException if any thread interrupted the current thread before or
535      *         while the current thread was waiting. The <em>interrupted status</em> of the
536      *         current thread is cleared when this exception is thrown.
537      * @see    #notify()
538      * @see    #notifyAll()
539      * @see    #wait(long)
540      * @see    #wait(long, int)
541      */
wait()542     public final void wait() throws InterruptedException {
543         wait(0);
544     }
545 
546     /**
547      * Called by the garbage collector on an object when garbage collection
548      * determines that there are no more references to the object.
549      * A subclass overrides the {@code finalize} method to dispose of
550      * system resources or to perform other cleanup.
551      * <p>
552      * The general contract of {@code finalize} is that it is invoked
553      * if and when the Java virtual
554      * machine has determined that there is no longer any
555      * means by which this object can be accessed by any thread that has
556      * not yet died, except as a result of an action taken by the
557      * finalization of some other object or class which is ready to be
558      * finalized. The {@code finalize} method may take any action, including
559      * making this object available again to other threads; the usual purpose
560      * of {@code finalize}, however, is to perform cleanup actions before
561      * the object is irrevocably discarded. For example, the finalize method
562      * for an object that represents an input/output connection might perform
563      * explicit I/O transactions to break the connection before the object is
564      * permanently discarded.
565      * <p>
566      * The {@code finalize} method of class {@code Object} performs no
567      * special action; it simply returns normally. Subclasses of
568      * {@code Object} may override this definition.
569      * <p>
570      * The Java programming language does not guarantee which thread will
571      * invoke the {@code finalize} method for any given object. It is
572      * guaranteed, however, that the thread that invokes finalize will not
573      * be holding any user-visible synchronization locks when finalize is
574      * invoked. If an uncaught exception is thrown by the finalize method,
575      * the exception is ignored and finalization of that object terminates.
576      * <p>
577      * After the {@code finalize} method has been invoked for an object, no
578      * further action is taken until the Java virtual machine has again
579      * determined that there is no longer any means by which this object can
580      * be accessed by any thread that has not yet died, including possible
581      * actions by other objects or classes which are ready to be finalized,
582      * at which point the object may be discarded.
583      * <p>
584      * The {@code finalize} method is never invoked more than once by a Java
585      * virtual machine for any given object.
586      * <p>
587      * Any exception thrown by the {@code finalize} method causes
588      * the finalization of this object to be halted, but is otherwise
589      * ignored.
590      *
591      * @apiNote
592      * Classes that embed non-heap resources have many options
593      * for cleanup of those resources. The class must ensure that the
594      * lifetime of each instance is longer than that of any resource it embeds.
595      * {@link java.lang.ref.Reference#reachabilityFence} can be used to ensure that
596      * objects remain reachable while resources embedded in the object are in use.
597      * <p>
598      * A subclass should avoid overriding the {@code finalize} method
599      * unless the subclass embeds non-heap resources that must be cleaned up
600      * before the instance is collected.
601      * Finalizer invocations are not automatically chained, unlike constructors.
602      * If a subclass overrides {@code finalize} it must invoke the superclass
603      * finalizer explicitly.
604      * To guard against exceptions prematurely terminating the finalize chain,
605      * the subclass should use a {@code try-finally} block to ensure
606      * {@code super.finalize()} is always invoked. For example,
607      * <pre>{@code      @Override
608      *     protected void finalize() throws Throwable {
609      *         try {
610      *             ... // cleanup subclass state
611      *         } finally {
612      *             super.finalize();
613      *         }
614      *     }
615      * }</pre>
616      *
617      * Deprecation: The finalization mechanism is inherently problematic.
618      * Finalization can lead to performance issues, deadlocks, and hangs.
619      * Errors in finalizers can lead to resource leaks; there is no way to cancel
620      * finalization if it is no longer necessary; and no ordering is specified
621      * among calls to {@code finalize} methods of different objects.
622      * Furthermore, there are no guarantees regarding the timing of finalization.
623      * The {@code finalize} method might be called on a finalizable object
624      * only after an indefinite delay, if at all.
625      *
626      * Classes whose instances hold non-heap resources should provide a method
627      * to enable explicit release of those resources, and they should also
628      * implement {@link AutoCloseable} if appropriate.
629      * The {@link java.lang.ref.Cleaner} and {@link java.lang.ref.PhantomReference}
630      * provide more flexible and efficient ways to release resources when an object
631      * becomes unreachable.
632      *
633      * @throws Throwable the {@code Exception} raised by this method
634      * @see java.lang.ref.WeakReference
635      * @see java.lang.ref.PhantomReference
636      * @jls 12.6 Finalization of Class Instances
637      */
638     // Android-changed: Avoid deprecating finalize() causing deprecation of the overridden methods.
639     // @Deprecated(since="9")
finalize()640     protected void finalize() throws Throwable { }
641 }
642