Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove finalize() #725

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions api/src/main/java/jakarta/mail/Folder.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
jmehrens marked this conversation as resolved.
Show resolved Hide resolved

/**
* Close this Folder and expunge deleted messages. <p>
Expand Down Expand Up @@ -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
Expand Down
14 changes: 1 addition & 13 deletions api/src/main/java/jakarta/mail/Service.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
60 changes: 16 additions & 44 deletions api/src/main/java/jakarta/mail/util/SharedFileInputStream.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Objects;

/**
* A <code>SharedFileInputStream</code> is a
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
jmehrens marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -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
jmehrens marked this conversation as resolved.
Show resolved Hide resolved
}
if (in == null)
throw new RuntimeException("Stream closed");
jmehrens marked this conversation as resolved.
Show resolved Hide resolved
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...
Expand All @@ -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();
}
}