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