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 all commits
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
22 changes: 10 additions & 12 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 @@ -643,14 +643,21 @@ public abstract boolean delete(boolean recurse)
*
* This implementation calls <code>close(true)</code>.
*
* @throws IllegalStateException if this folder is not opened
* @throws MessagingException for other failures
* @see jakarta.mail.event.ConnectionEvent
* @since JavaMail 1.6
*/
@Override
public void close() throws MessagingException {
close(true);
try {
close(true);
} catch (IllegalStateException ise) {
if (isOpen()) {
throw ise;
}
} finally {
q.terminateQueue();
}
}

/**
Expand Down Expand Up @@ -1641,15 +1648,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
77 changes: 35 additions & 42 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 Down Expand Up @@ -75,9 +75,9 @@ 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;
RandomAccessFile in;

SharedFile(String file) throws IOException {
this.in = new RandomAccessFile(file, "r");
Expand All @@ -92,19 +92,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 @@ -265,10 +257,10 @@ private int read1(byte[] b, int off, int len) throws IOException {
int avail = count - pos;
if (avail <= 0) {
if (false) {
/* If the requested length is at least as large as the buffer, and
if there is no mark/reset activity, do not bother to copy the
bytes into the local buffer. In this way buffered streams will
cascade harmlessly. */
/* If the requested length is at least as large as the buffer, and
if there is no mark/reset activity, do not bother to copy the
bytes into the local buffer. In this way buffered streams will
cascade harmlessly. */
if (len >= buf.length && markpos < 0) {
// XXX - seek, update bufpos - how?
return in.read(b, off, len);
Expand Down Expand Up @@ -337,10 +329,10 @@ public synchronized long skip(long n) throws IOException {

if (avail <= 0) {
// If no mark position set then don't keep in buffer
/*
/*
if (markpos <0)
return in.skip(n);
*/
*/

// Fill in buffer to save bytes for reset
fill();
Expand Down Expand Up @@ -441,7 +433,17 @@ public void close() throws IOException {
sf = null;
in = null;
buf = null;
Objects.requireNonNull(this); //TODO: replace with Reference.reachabilityFence
/*
* This avoids that 'new SharedFileInputStream(file)'
* is GCed meanwhile #newStream is invoked, for example:
* new SharedFileInputStream(file).newStream(0, -1)
*
* That could be an issue if a subclass of this one invokes #close
* from #finalize (not a good practice anyway).
*/
Objects.requireNonNull(this);
// TODO Replace it by the next
// Reference.reachabilityFence(this);
}
}

Expand All @@ -454,7 +456,7 @@ public void close() throws IOException {
@Override
public long getPosition() {
//System.out.println("getPosition: start " + start + " pos " + pos
// + " bufpos " + bufpos + " = " + (bufpos + pos - start));
// + " bufpos " + bufpos + " = " + (bufpos + pos - start));
if (in == null)
throw new RuntimeException("Stream closed");
return bufpos + pos - start;
Expand All @@ -481,7 +483,7 @@ public synchronized InputStream newStream(long start, long end) {
throw new IllegalArgumentException("start < 0");
if (end == -1)
end = datalen;

return new SharedFileInputStream(sf,
this.start + start, end - start, bufsize);
} finally {
Expand All @@ -492,27 +494,18 @@ public synchronized InputStream newStream(long start, long end) {
// for testing...
/*
public static void main(String[] argv) throws Exception {
SharedFileInputStream is = new SharedFileInputStream(argv[0]);
java.util.Random r = new java.util.Random();
int b;
while ((b = is.read()) >= 0) {
System.out.write(b);
if (r.nextDouble() < 0.3) {
InputStream is2 = is.newStream(is.getPosition(), -1);
int b2;
while ((b2 = is2.read()) >= 0)
;
}
}
SharedFileInputStream is = new SharedFileInputStream(argv[0]);
java.util.Random r = new java.util.Random();
int b;
while ((b = is.read()) >= 0) {
System.out.write(b);
if (r.nextDouble() < 0.3) {
InputStream is2 = is.newStream(is.getPosition(), -1);
int b2;
while ((b2 = is2.read()) >= 0)
;
}
}
}
*/

/**
* Force this stream to close.
*/
@Override
protected synchronized void finalize() throws Throwable {
super.finalize();
close();
}
}
170 changes: 170 additions & 0 deletions api/src/test/java/jakarta/mail/FolderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright (c) 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package jakarta.mail;

import java.util.Properties;

import org.junit.Test;

public class FolderTest {

@Test
public void closeIdempotent() throws MessagingException {
CustomFolder folder = new CustomFolder(new DummyStore(Session.getDefaultInstance(new Properties()), null));
folder.close();
folder.close();
}

@Test
public void closeFalse() throws MessagingException {
try (Folder folder = new CustomFolder(new DummyStore(Session.getDefaultInstance(new Properties()), null))) {
folder.close(false);
}
}

private static class CustomFolder extends Folder {

private boolean closed;

protected CustomFolder(Store store) {
super(store);
}

@Override
public String getName() {
return null;
}

@Override
public String getFullName() {
return null;
}

@Override
public Folder getParent() throws MessagingException {
return null;
}

@Override
public boolean exists() throws MessagingException {
return false;
}

@Override
public Folder[] list(String pattern) throws MessagingException {
return null;
}

@Override
public char getSeparator() throws MessagingException {
return 0;
}

@Override
public int getType() throws MessagingException {
return 0;
}

@Override
public boolean create(int type) throws MessagingException {
return false;
}

@Override
public boolean hasNewMessages() throws MessagingException {
return false;
}

@Override
public Folder getFolder(String name) throws MessagingException {
return null;
}

@Override
public boolean delete(boolean recurse) throws MessagingException {
return false;
}

@Override
public boolean renameTo(Folder f) throws MessagingException {
return false;
}

@Override
public void open(int mode) throws MessagingException {
}

@Override
public void close(boolean expunge) throws MessagingException {
if (closed) {
throw new IllegalStateException("Already closed");
}
closed = true;
}

@Override
public boolean isOpen() {
return !closed;
}

@Override
public Flags getPermanentFlags() {
return null;
}

@Override
public int getMessageCount() throws MessagingException {
return 0;
}

@Override
public Message getMessage(int msgnum) throws MessagingException {
return null;
}

@Override
public void appendMessages(Message[] msgs) throws MessagingException {
}

@Override
public Message[] expunge() throws MessagingException {
return null;
}
}

private static class DummyStore extends Store {

protected DummyStore(Session session, URLName urlname) {
super(session, urlname);
}

@Override
public Folder getDefaultFolder() throws MessagingException {
return null;
}

@Override
public Folder getFolder(String name) throws MessagingException {
return null;
}

@Override
public Folder getFolder(URLName url) throws MessagingException {
return null;
}
}
}
Loading