8305083: Remove finalize() from test/hotspot/jtreg/vmTestbase/nsk/share/ and /jpda that are used in serviceability/dcmd/framework tests

Reviewed-by: coleenp, dholmes
This commit is contained in:
Afshin Zafari 2023-05-12 07:32:46 +00:00
parent 1ce1611ead
commit e1e758a7b4
11 changed files with 83 additions and 87 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,22 +22,44 @@
*/
package nsk.share;
import java.lang.ref.Cleaner;
/**
* Finalizable interface allows <tt>Finalizer</tt> to perform finalization of an object.
* Each object that requires finalization at VM shutdown time should implement this
* interface and activate a <tt>Finalizer</tt> hook.
* interface and call the <tt>registerCleanup</tt> to activate a <tt>Finalizer</tt> hook.
*
* @see Finalizer
*/
public interface Finalizable {
/**
* This method will be invoked by <tt>Finalizer</tt> when virtual mashine
* This method will be implemented by FinalizableObject and is called in <tt>finalizeAtExit</tt>.
*
* @see Finalizer
*/
public void cleanup();
/**
* This method will be invoked by <tt>Finalizer</tt> when virtual machine
* shuts down.
*
* @throws Throwable if any throwable exception thrown during finalization
*/
public void finalizeAtExit() throws Throwable;
default public void finalizeAtExit() throws Throwable {
cleanup();
}
/**
* This method will register a cleanup method and create an instance of Finalizer
* to register the object for finalization at VM exit.
*
* @see Finalizer
*/
default public void registerCleanup() {
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
Cleaner.create().register(this, () -> cleanup());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,28 +23,18 @@
package nsk.share;
/**
* This class is an simple exalmple of finalizable object, that implements interface
* <code>Finalizable</code> and invokes standard <code>finalize()</code> method
* as finalization.
* This class is a simple example of finalizable object, that
* implements interface <code>Finalizable</code>.
*
* @see Finalizable
* @see Finalizer
*/
public class FinalizableObject implements Finalizable {
/**
* This method will be invoked by <tt>Finalizer</tt> when virtual mashine
* shuts down.
* For <code>FinalizableObject</code> this method just invoke
* <code>finalize()</code>.
*
* @throws Throwable if any throwable exception thrown during finalization
*
* @see Object#finalize()
* @see Finalizer
* Subclasses should override this method to provide the specific
* cleanup actions that they need.
*/
public void finalizeAtExit() throws Throwable {
finalize();
}
public void cleanup() {}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -60,8 +60,7 @@ public class LocalProcess extends FinalizableObject {
process = Runtime.getRuntime().exec(args);
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
registerCleanup();
}
public void launch (String cmdLine) throws IOException {
@ -69,8 +68,7 @@ public class LocalProcess extends FinalizableObject {
process = Runtime.getRuntime().exec(cmdLine);
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
registerCleanup();
}
/** Return exit status. */
@ -166,12 +164,11 @@ public class LocalProcess extends FinalizableObject {
}
/**
* Finalize mirror by invoking <code>close()</code>.
* This method is called at finalization and calls <code>kill()</code>.
*
* @throws Throwable if any throwable exception is thrown during finalization
*/
protected void finalize() throws Throwable {
@Override
public void cleanup() {
kill();
super.finalize();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -189,13 +189,12 @@ public class Log extends FinalizableObject {
@Deprecated
protected Log() {
// install finalizer to print errors summary at exit
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
registerCleanup();
// Don't log exceptions from this method. It would just add unnecessary logs.
loggedExceptions.add("nsk.share.jdi.SerialExecutionDebugger.executeTests");
}
/**
* Incarnate new Log for the given <code>stream</code> and
* for non-verbose mode.
@ -470,7 +469,7 @@ public class Log extends FinalizableObject {
*/
@Deprecated
protected synchronized void logTo(PrintStream stream) {
finalize(); // flush older log stream
cleanup(); // flush older log stream
out = stream;
verbose = true;
}
@ -605,8 +604,13 @@ public class Log extends FinalizableObject {
/**
* Print errors summary if mode is verbose, flush and cancel output stream.
*
* This is replacement of the finalize() method and is called when this
* Log instance becomes unreachable.
*
*/
protected void finalize() {
@Override
public void cleanup() {
if (verbose() && isErrorsSummaryEnabled()) {
printErrorsSummary();
}
@ -619,7 +623,7 @@ public class Log extends FinalizableObject {
* Perform finalization at the exit.
*/
public void finalizeAtExit() {
finalize();
cleanup();
}
/**

View File

@ -43,8 +43,8 @@ public final class MainWrapper {
// It is needed to register finalizer thread in default thread group
// So FinalizerThread thread can't be in virtual threads group
Finalizer finalizer = new Finalizer(new FinalizableObject());
finalizer.activate();
FinalizableObject finalizableObject = new FinalizableObject();
finalizableObject.registerCleanup();
// Some tests use this property to understand if virtual threads are used
System.setProperty("main.wrapper", wrapperName);

View File

@ -129,9 +129,7 @@ public class Binder extends DebugeeBinder {
public Debugee makeLocalDebugee(Process process) {
LocalLaunchedDebugee debugee = new LocalLaunchedDebugee(process, this);
Finalizer finalizer = new Finalizer(debugee);
finalizer.activate();
debugee.registerCleanup();
return debugee;
}
@ -942,8 +940,7 @@ public class Binder extends DebugeeBinder {
RemoteLaunchedDebugee debugee = new RemoteLaunchedDebugee(this);
Finalizer finalizer = new Finalizer(debugee);
finalizer.activate();
debugee.registerCleanup();
return debugee;
}
@ -956,8 +953,7 @@ public class Binder extends DebugeeBinder {
ManualLaunchedDebugee debugee = new ManualLaunchedDebugee(this);
debugee.launchDebugee(cmd);
Finalizer finalizer = new Finalizer(debugee);
finalizer.activate();
debugee.registerCleanup();
return debugee;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -106,8 +106,7 @@ final public class Binder extends DebugeeBinder {
debugee.redirectOutput(log);
}
Finalizer finalizer = new Finalizer(debugee);
finalizer.activate();
debugee.registerCleanup();
Transport transport = debugee.connect();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -119,7 +119,6 @@ public class BindServer implements Finalizable {
public static int run(String argv[], PrintStream out) {
return new BindServer().runIt(argv, out);
}
/**
* Perform execution of <code>BindServer</code>.
* This method handles command line arguments, starts seperate
@ -152,8 +151,7 @@ public class BindServer implements Finalizable {
log.enableVerboseOnError(false);
logger = new Log.Logger(log, "");
Finalizer bindFinalizer = new Finalizer(this);
bindFinalizer.activate();
registerCleanup();
logger.trace(TRACE_LEVEL_THREADS, "BindServer: starting main thread");
@ -217,7 +215,7 @@ public class BindServer implements Finalizable {
logger.trace(TRACE_LEVEL_THREADS, "BindServer: exiting main thread");
try {
finalize();
cleanup();
} catch (Throwable e) {
e.printStackTrace(log.getOutStream());
logger.complain("Caught exception while finalization of BindServer:\n\t" + e);
@ -408,19 +406,18 @@ public class BindServer implements Finalizable {
*
* @see #close()
*/
protected void finalize() throws Throwable {
@Override
public void cleanup() {
close();
super.finalize();
}
/**
* Make finalization of <code>BindServer</code> object at program exit
* by invoking method <code>finalize()</code>.
* by invoking method <code>cleanup()</code>.
*
* @see #finalize()
*/
public void finalizeAtExit() throws Throwable {
finalize();
cleanup();
logger.trace(TRACE_LEVEL_THREADS, "BindServer: finalization at exit completed");
}

View File

@ -26,6 +26,7 @@ package nsk.share.jpda;
import nsk.share.*;
import java.io.*;
import java.lang.ref.Cleaner;
import java.net.*;
import java.util.*;
@ -109,7 +110,6 @@ public class DebugeeBinder extends Log.Logger implements Finalizable {
private ServerSocket pipeServerSocket = null;
// -------------------------------------------------- //
/**
* Incarnate new Binder obeying the given
* <code>argumentHandler</code>; and assign the given
@ -118,8 +118,8 @@ public class DebugeeBinder extends Log.Logger implements Finalizable {
public DebugeeBinder (DebugeeArgumentHandler argumentHandler, Log log) {
super(log, LOG_PREFIX);
this.argumentHandler = argumentHandler;
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
registerCleanup();
}
/**
@ -551,20 +551,9 @@ public class DebugeeBinder extends Log.Logger implements Finalizable {
/**
* Finalize binder by invoking <code>close()</code>.
*
* @throws Throwable if any throwable exception is thrown during finalization
*/
protected void finalize() throws Throwable {
public void cleanup() {
close();
super.finalize();
}
/**
* Finalize binder at exit by invoking <code>finalize()</code>.
*
* @throws Throwable if any throwable exception is thrown during finalization
*/
public void finalizeAtExit() throws Throwable {
finalize();
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -84,6 +84,9 @@ abstract public class DebugeeProcess extends FinalizableObject {
protected DebugeeProcess (DebugeeBinder binder) {
this.binder = binder;
this.log = binder.getLog();
// Register the cleanup() method to be called when this instance becomes unreachable.
registerCleanup();
}
/**
@ -460,8 +463,7 @@ abstract public class DebugeeProcess extends FinalizableObject {
*
* @throws Throwable if any throwable exception is thrown during finalization
*/
protected void finalize() throws Throwable {
public void cleanup() {
close();
super.finalize();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -82,7 +82,6 @@ public class SocketIOPipe extends Log.Logger implements Finalizable {
protected ServerSocket serverSocket;
protected String name;
/**
* Make general <code>IOPipe</code> object with specified parameters.
*/
@ -93,6 +92,8 @@ public class SocketIOPipe extends Log.Logger implements Finalizable {
this.timeout = timeout;
this.listening = listening;
this.name = name;
registerCleanup();
}
/**
@ -104,6 +105,8 @@ public class SocketIOPipe extends Log.Logger implements Finalizable {
this.port = port;
this.timeout = timeout;
this.listening = listening;
registerCleanup();
}
/**
@ -308,18 +311,15 @@ public class SocketIOPipe extends Log.Logger implements Finalizable {
/**
* Perform finalization of the object by invoking close().
*
* This is replacement of finalize() method and is called
* when this instance becomes unreachable.
*
*/
protected void finalize() throws Throwable {
public void cleanup() {
close();
super.finalize();
}
/**
* Perform finalization of the object at exit by invoking finalize().
*/
public void finalizeAtExit() throws Throwable {
finalize();
}
/**
* Field 'pipeCounter' and method 'getNextPipeNumber' are used to construct unique names for SocketIOPipes