Skip to content

Commit

Permalink
Remove finalize()
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Mar 15, 2024
1 parent bf2bfc1 commit 825aae4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 68 deletions.
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();
}

/**
* 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;
}

/**
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
}
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...
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();
}
}

0 comments on commit 825aae4

Please sign in to comment.