Class CleanerUtil


  • public class CleanerUtil
    extends Object
    This class collects all the cleaner actions executed in various parts of the code.

    These actions replace the use of finalizers, which are deprecated in Java 9 and later, and should be avoided. These actions are triggered by their respective objects when those objects become phantom reachable.

    In the "unclosed*" methods below, the object should have been closed (implements AutoCloseable). We could possibly consolidate these into a single method which only warns, and doesn't try to clean up. We could also delete them entirely, since it is the caller's responsibility to close AutoCloseable resources, not the object's own responsibility to detect that it wasn't closed.

    • Field Detail

      • CLEANER

        public static final Cleaner CLEANER
    • Constructor Detail

      • CleanerUtil

        public CleanerUtil()
    • Method Detail

      • unclosed

        public static Cleaner.Cleanable unclosed​(AutoCloseable obj,
                                                 Class<?> objClass,
                                                 AtomicBoolean closed,
                                                 org.slf4j.Logger log,
                                                 AutoCloseable closeable)
        Register an action to warn about caller failing to close an AutoCloseable object.

        This task will register a generic action to:

        1. check that the monitored object wasn't closed,
        2. log a warning that the monitored object was not closed,
        3. attempt to close a resource within the object, and
        4. log an error if the resource cannot be closed for any reason
        Parameters:
        obj - the object to monitor for becoming phantom-reachable without having been closed
        objClass - the class whose simple name will be used in the log message for o (usually an interface name, rather than the actual impl name of the object)
        closed - a flag to check whether o has already been closed
        log - the logger to use when emitting error/warn messages
        closeable - the resource within o to close when o is cleaned; must not contain a reference to the monitoredObject or it won't become phantom-reachable and will never be cleaned
        Returns:
        the registered Cleaner.Cleanable from Cleaner.register(Object, Runnable)