From 825aae4e9478cde8388911948b66a6acfeb13413 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Fri, 15 Mar 2024 15:26:15 +0100 Subject: [PATCH] Remove finalize() Signed-off-by: Jorge Bescos Gascon --- api/src/main/java/jakarta/mail/Folder.java | 17 ++---- api/src/main/java/jakarta/mail/Service.java | 14 +---- .../mail/util/SharedFileInputStream.java | 60 +++++-------------- 3 files changed, 23 insertions(+), 68 deletions(-) diff --git a/api/src/main/java/jakarta/mail/Folder.java b/api/src/main/java/jakarta/mail/Folder.java index 95a7784b..16c34754 100644 --- a/api/src/main/java/jakarta/mail/Folder.java +++ b/api/src/main/java/jakarta/mail/Folder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -623,12 +623,16 @@ public abstract boolean delete(boolean recurse) * if this method terminates abnormally by throwing a * MessagingException. * + * Implementations of this method must also invoke super.close(boolean expunge) + * * @param expunge expunges all deleted messages if this flag is true * @throws IllegalStateException if this folder is not opened * @throws MessagingException for other failures * @see jakarta.mail.event.ConnectionEvent */ - public abstract void close(boolean expunge) throws MessagingException; + public void close(boolean expunge) throws MessagingException { + q.terminateQueue(); + } /** * Close this Folder and expunge deleted messages.

@@ -1641,15 +1645,6 @@ private void queueEvent(MailEvent event, q.enqueue(event, v); } - @Override - protected void finalize() throws Throwable { - try { - q.terminateQueue(); - } finally { - super.finalize(); - } - } - /** * override the default toString(), it will return the String * from Folder.getFullName() or if that is null, it will use diff --git a/api/src/main/java/jakarta/mail/Service.java b/api/src/main/java/jakarta/mail/Service.java index 6d8af32a..be380d5f 100644 --- a/api/src/main/java/jakarta/mail/Service.java +++ b/api/src/main/java/jakarta/mail/Service.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -632,18 +632,6 @@ protected void queueEvent(MailEvent event, q.enqueue(event, v); } - /** - * Stop the event dispatcher thread so the queue can be garbage collected. - */ - @Override - protected void finalize() throws Throwable { - try { - q.terminateQueue(); - } finally { - super.finalize(); - } - } - /** * Package private method to allow Folder to get the Session for a Store. */ diff --git a/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java b/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java index e9c01589..bd4d9981 100644 --- a/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java +++ b/api/src/main/java/jakarta/mail/util/SharedFileInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; -import java.util.Objects; /** * A SharedFileInputStream is a @@ -75,7 +74,7 @@ public class SharedFileInputStream extends BufferedInputStream * to a particular file so it can be closed when the * last reference is gone. */ - static class SharedFile { + static class SharedFile implements AutoCloseable { private int cnt; private RandomAccessFile in; @@ -92,19 +91,11 @@ public synchronized RandomAccessFile open() { return in; } + @Override public synchronized void close() throws IOException { if (cnt > 0 && --cnt <= 0) in.close(); } - - @Override - protected synchronized void finalize() throws Throwable { - try { - in.close(); - } finally { - super.finalize(); - } - } } private SharedFile sf; @@ -433,16 +424,10 @@ public boolean markSupported() { */ @Override public void close() throws IOException { - if (in == null) - return; - try { - sf.close(); - } finally { - sf = null; - in = null; - buf = null; - Objects.requireNonNull(this); //TODO: replace with Reference.reachabilityFence - } + sf.close(); + sf = null; + in = null; + buf = null; } /** @@ -474,19 +459,15 @@ public long getPosition() { */ @Override public synchronized InputStream newStream(long start, long end) { - try { - if (in == null) - throw new RuntimeException("Stream closed"); - if (start < 0) - throw new IllegalArgumentException("start < 0"); - if (end == -1) - end = datalen; - - return new SharedFileInputStream(sf, - this.start + start, end - start, bufsize); - } finally { - Objects.requireNonNull(this); //TODO: replace with Reference.reachabilityFence - } + if (in == null) + throw new RuntimeException("Stream closed"); + if (start < 0) + throw new IllegalArgumentException("start < 0"); + if (end == -1) + end = datalen; + + return new SharedFileInputStream(sf, + this.start + start, end - start, bufsize); } // for testing... @@ -506,13 +487,4 @@ public static void main(String[] argv) throws Exception { } } */ - - /** - * Force this stream to close. - */ - @Override - protected synchronized void finalize() throws Throwable { - super.finalize(); - close(); - } }