Skip to content
Permalink
Browse files
8276447: Deprecate finalization-related methods for removal
Reviewed-by: rriggs, alanb, lancea, darcy, mchung, serb, smarks, prr
  • Loading branch information
Brent Christian committed Dec 8, 2021
1 parent 3c2951f commit ec7cb6d5d305003429b51384ed72973767c70124
Show file tree
Hide file tree
Showing 62 changed files with 156 additions and 140 deletions.
@@ -275,8 +275,13 @@ public static <T extends Enum<T>> T valueOf(Class<T> enumClass,

/**
* enum classes cannot have finalize methods.
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@SuppressWarnings("deprecation")
@Deprecated(since="18", forRemoval=true)
@SuppressWarnings("removal")
protected final void finalize() { }

/**
@@ -478,6 +478,12 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
* A subclass overrides the {@code finalize} method to dispose of
* system resources or to perform other cleanup.
* <p>
* <b>When running in a Java virtual machine in which finalization has been
* disabled or removed, the garbage collector will never call
* {@code finalize()}. In a Java virtual machine in which finalization is
* enabled, the garbage collector might call {@code finalize} only after an
* indefinite delay.</b>
* <p>
* The general contract of {@code finalize} is that it is invoked
* if and when the Java virtual
* machine has determined that there is no longer any
@@ -543,27 +549,29 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
* }
* }</pre>
*
* @deprecated The finalization mechanism is inherently problematic.
* Finalization can lead to performance issues, deadlocks, and hangs.
* Errors in finalizers can lead to resource leaks; there is no way to cancel
* finalization if it is no longer necessary; and no ordering is specified
* among calls to {@code finalize} methods of different objects.
* Furthermore, there are no guarantees regarding the timing of finalization.
* The {@code finalize} method might be called on a finalizable object
* only after an indefinite delay, if at all.
*
* Classes whose instances hold non-heap resources should provide a method
* to enable explicit release of those resources, and they should also
* implement {@link AutoCloseable} if appropriate.
* The {@link java.lang.ref.Cleaner} and {@link java.lang.ref.PhantomReference}
* provide more flexible and efficient ways to release resources when an object
* becomes unreachable.
* @deprecated Finalization is deprecated and subject to removal in a future
* release. The use of finalization can lead to problems with security,
* performance, and reliability.
* See <a href="https://openjdk.java.net/jeps/421">JEP 421</a> for
* discussion and alternatives.
* <p>
* Subclasses that override {@code finalize} to perform cleanup should use
* alternative cleanup mechanisms and remove the {@code finalize} method.
* Use {@link java.lang.ref.Cleaner} and
* {@link java.lang.ref.PhantomReference} as safer ways to release resources
* when an object becomes unreachable. Alternatively, add a {@code close}
* method to explicitly release resources, and implement
* {@code AutoCloseable} to enable use of the {@code try}-with-resources
* statement.
* <p>
* This method will remain in place until finalizers have been removed from
* most existing code.
*
* @throws Throwable the {@code Exception} raised by this method
* @see java.lang.ref.WeakReference
* @see java.lang.ref.PhantomReference
* @jls 12.6 Finalization of Class Instances
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
protected void finalize() throws Throwable { }
}
@@ -707,8 +707,17 @@ public Process exec(String[] cmdarray, String[] envp, File dir)
* The method {@link System#runFinalization()} is the conventional
* and convenient means of invoking this method.
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
* <p>
* When running in a JVM in which finalization has been disabled or removed,
* no objects will be pending finalization, so this method does nothing.
*
* @see java.lang.Object#finalize()
* @jls 12.6 Finalization of Class Instances
*/
@Deprecated(since="18", forRemoval=true)
public void runFinalization() {
SharedSecrets.getJavaLangRefAccess().runFinalization();
}
@@ -1922,8 +1922,18 @@ public static void gc() {
* Runtime.getRuntime().runFinalization()
* </pre></blockquote>
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
* <p>
* When running in a JVM in which finalization has been disabled or removed,
* no objects will be pending finalization, so this method does nothing.
*
* @see java.lang.Runtime#runFinalization()
* @jls 12.6 Finalization of Class Instances
*/
@Deprecated(since="18", forRemoval=true)
@SuppressWarnings("removal")
public static void runFinalization() {
Runtime.getRuntime().runFinalization();
}
@@ -2321,7 +2331,7 @@ public void registerShutdownHook(int slot, boolean registerShutdownInProgress, R
public Thread newThreadWithAcc(Runnable target, @SuppressWarnings("removal") AccessControlContext acc) {
return new Thread(target, acc);
}
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
public void invokeFinalize(Object o) throws Throwable {
o.finalize();
}
@@ -791,7 +791,7 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
FinalizableDelegatedExecutorService(ExecutorService executor) {
super(executor);
}
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() {
super.shutdown();
}
@@ -1477,8 +1477,13 @@ public boolean awaitTermination(long timeout, TimeUnit unit)
* @implNote Previous versions of this class had a finalize method
* that shut down this executor, but in this version, finalize
* does nothing.
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() {}

/**
@@ -242,7 +242,7 @@ public final boolean isLockHeldByCurrentThread() {
return readLock.isHeldByCurrentThread();
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
try {
close();
@@ -270,7 +270,7 @@ public boolean isOutputShutdown() {
* the penalty of prematurly killing SSL sessions.
*/
@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected final void finalize() throws Throwable {
try {
close();
@@ -114,7 +114,7 @@ public JRSUIControl(final boolean flipped){
changes.putAll(other.changes);
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected synchronized void finalize() throws Throwable {
if (cfDictionaryPtr == 0) return;
disposeCFDictionary(cfDictionaryPtr);
@@ -144,7 +144,7 @@ void _displayReconfiguration(int displayId, boolean removed) {
}

@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
try {
super.finalize();
@@ -247,7 +247,7 @@ public CompositeFont getCompositeFont2D() {
return compFont;
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected synchronized void finalize() {
if (nativeFontPtr != 0) {
disposeNativeFont(nativeFontPtr);
@@ -125,7 +125,7 @@ public long getNativeStrikePtr() {
return nativeStrikePtr;
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected synchronized void finalize() throws Throwable {
if (nativeStrikePtr != 0) {
disposeNativeStrikePtr(nativeStrikePtr);
@@ -163,7 +163,7 @@ final long executeGet(final CFNativeActionGet action) {
}

@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected final void finalize() throws Throwable {
dispose();
}
@@ -598,7 +598,7 @@ private boolean jobSetup(Pageable doc, boolean allowPrintToFile) {
// The following methods are CPrinterJob specific.

@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() {
synchronized (fNSPrintInfoLock) {
if (fNSPrintInfo != -1) {
@@ -72,7 +72,7 @@ public void seek(long pos) throws IOException {
streamPos = pos;
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
@@ -147,7 +147,7 @@ void finish() throws IOException {
}

@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
@@ -284,7 +284,7 @@ void finish() throws IOException {
}

@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
@@ -435,7 +435,7 @@ public final int encode(byte[] b, int off,
return compDataLength;
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
super.finalize();
if(JPEGWriter != null) {
@@ -139,7 +139,7 @@ public void decodeRaw(byte[] b,
JPEGReader.read(0, JPEGParam);
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
super.finalize();
JPEGReader.dispose();
@@ -610,7 +610,7 @@ public void decodeRaw(byte[] b,
JPEGReader.read(0, JPEGParam);
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
super.finalize();
JPEGReader.dispose();
@@ -60,7 +60,7 @@ public StreamFinalizer(ImageInputStream stream) {
this.stream = stream;
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
try {
stream.close();
@@ -425,7 +425,7 @@ protected Transmitter createTransmitter() throws MidiUnavailableException {
* close this device if discarded by the garbage collector.
*/
@Override
@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
protected final void finalize() {
close();
}
@@ -283,6 +283,7 @@ public String toString() {
}

@Override
@SuppressWarnings("removal")
protected void finalize() {

if (clip != null) {
@@ -1162,17 +1162,14 @@ public abstract boolean drawImage(Image img,
/**
* Disposes of this graphics context once it is no longer referenced.
*
* @deprecated The {@code finalize} method has been deprecated.
* Subclasses that override {@code finalize} in order to perform cleanup
* should be modified to use alternative cleanup mechanisms and
* to remove the overriding {@code finalize} method.
* When overriding the {@code finalize} method, its implementation must explicitly
* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
* See the specification for {@link Object#finalize()} for further
* information about migration options.
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*
* @see #dispose
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
public void finalize() {
dispose();
}
@@ -85,17 +85,14 @@ protected PrintJob() {}
/**
* Ends this print job once it is no longer referenced.
*
* @deprecated The {@code finalize} method has been deprecated.
* Subclasses that override {@code finalize} in order to perform cleanup
* should be modified to use alternative cleanup mechanisms and
* to remove the overriding {@code finalize} method.
* When overriding the {@code finalize} method, its implementation must explicitly
* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
* See the specification for {@link Object#finalize()} for further
* information about migration options.
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*
* @see #end
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
public void finalize() {
end();
}
@@ -680,16 +680,12 @@ public void deregisterAll() {
* @exception Throwable if an error occurs during superclass
* finalization.
*
* @deprecated The {@code finalize} method has been deprecated.
* Subclasses that override {@code finalize} in order to perform cleanup
* should be modified to use alternative cleanup mechanisms and
* to remove the overriding {@code finalize} method.
* When overriding the {@code finalize} method, its implementation must explicitly
* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
* See the specification for {@link Object#finalize()} for further
* information about migration options.
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
public void finalize() throws Throwable {
deregisterAll();
super.finalize();
@@ -842,7 +838,7 @@ public synchronized void clear() {
accMap.clear();
}

@SuppressWarnings("deprecation")
@SuppressWarnings("removal")
public synchronized void finalize() {
clear();
}
@@ -261,16 +261,12 @@ public void close() throws IOException {
/**
* {@inheritDoc}
*
* @deprecated The {@code finalize} method has been deprecated.
* Subclasses that override {@code finalize} in order to perform cleanup
* should be modified to use alternative cleanup mechanisms and
* to remove the overriding {@code finalize} method.
* When overriding the {@code finalize} method, its implementation must explicitly
* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
* See the specification for {@link Object#finalize()} for further
* information about migration options.
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9")
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer: for performance reasons we instead use the
// Disposer mechanism for ensuring that the underlying

1 comment on commit ec7cb6d

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on ec7cb6d Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.