Avoid overriding finalize()
Is your feature request related to a problem? Please describe.
The current implementation to make Resource.setNonDisposehandler(Consumer) work, uses finalize() under the hood:
https://github.com/eclipse-platform/eclipse.platform.swt/blob/4d6832927eb8874dccb0c59eac5a761ce9ea025f/bundles/org.eclipse.swt/Eclipse%20SWT/common/org/eclipse/swt/graphics/Resource.java#L74-L81
This method is deprecated for removal since Java 18 as described in JEP 421.
Describe the solution you'd like
We should rewrite this functionality using PhantomReferences and a daemon thread polling from the shared ReferenceQueue.
Describe alternatives you've considered
The cleaner API might also work:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ref/Cleaner.html
I already made some PO to use Cleaner API instead:
- https://github.com/eclipse-platform/eclipse.platform.swt/pull/1054
this has the advantage that one still can use dispose() to clear early, wait for the GC (and probably log), support clearing of Weak/SoftReference and so on.
here is a working commit that was rejected: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/185578/24/bundles/org.eclipse.swt/Eclipse+SWT/common/org/eclipse/swt/graphics/Resource.java#b74
Is there any reason why we can't let the Cleaner do the disposal, effectively making dispose() a best practice instead of a necessity?
Aa cleaner depends on GC, which has literally no guaranties to run. Resources are OS objects which need to be released otherwise the OS runs in out of resource; and- a cleaner does not run in swt thread.
My proposed solution: #1702