diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizable.java b/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizable.java index d8d13d47669..b515721e925 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizable.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizable.java @@ -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 Finalizer to perform finalization of an object. * Each object that requires finalization at VM shutdown time should implement this - * interface and activate a Finalizer hook. + * interface and call the registerCleanup to activate a Finalizer hook. * * @see Finalizer */ public interface Finalizable { /** - * This method will be invoked by Finalizer when virtual mashine + * This method will be implemented by FinalizableObject and is called in finalizeAtExit. + * + * @see Finalizer + */ + public void cleanup(); + + /** + * This method will be invoked by Finalizer 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()); + } } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/FinalizableObject.java b/test/hotspot/jtreg/vmTestbase/nsk/share/FinalizableObject.java index 6a5b6457fb6..7ffee4a43fe 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/FinalizableObject.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/FinalizableObject.java @@ -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 - * Finalizable and invokes standard finalize() method - * as finalization. + * This class is a simple example of finalizable object, that + * implements interface Finalizable. * * @see Finalizable * @see Finalizer */ public class FinalizableObject implements Finalizable { - /** - * This method will be invoked by Finalizer when virtual mashine - * shuts down. - * For FinalizableObject this method just invoke - * finalize(). - * - * @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() {} } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java b/test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java index f6a0b087334..6ff5c9a39a5 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/LocalProcess.java @@ -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 close(). + * This method is called at finalization and calls kill(). * - * @throws Throwable if any throwable exception is thrown during finalization */ - protected void finalize() throws Throwable { + @Override + public void cleanup() { kill(); - super.finalize(); } } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/Log.java b/test/hotspot/jtreg/vmTestbase/nsk/share/Log.java index e171f6daa82..6bdc5266fc4 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/Log.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/Log.java @@ -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 stream 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(); } /** diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/MainWrapper.java b/test/hotspot/jtreg/vmTestbase/nsk/share/MainWrapper.java index 6e91dd9bcbf..c24ca5e813e 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/MainWrapper.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/MainWrapper.java @@ -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); diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/Binder.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/Binder.java index 59e57eff965..46a115bbb4c 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/Binder.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/Binder.java @@ -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; } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/Binder.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/Binder.java index 2424a9a56ed..73c9654f161 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/Binder.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jdwp/Binder.java @@ -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(); diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/BindServer.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/BindServer.java index 6ad2150acef..2fc76a991d5 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/BindServer.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/BindServer.java @@ -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 BindServer. * 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 BindServer object at program exit - * by invoking method finalize(). + * by invoking method cleanup(). * - * @see #finalize() */ public void finalizeAtExit() throws Throwable { - finalize(); + cleanup(); logger.trace(TRACE_LEVEL_THREADS, "BindServer: finalization at exit completed"); } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java index 61354b2e6e1..0be4658d824 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeBinder.java @@ -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 * argumentHandler; 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 close(). * - * @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 finalize(). - * - * @throws Throwable if any throwable exception is thrown during finalization - */ - public void finalizeAtExit() throws Throwable { - finalize(); } /** diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeProcess.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeProcess.java index 40261018a12..94fafdead27 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeProcess.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/DebugeeProcess.java @@ -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(); } } diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/SocketIOPipe.java b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/SocketIOPipe.java index 57fbb7a6915..2cc7f0435cd 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/SocketIOPipe.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/SocketIOPipe.java @@ -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 IOPipe 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