From eceb3bbc80aae5d99155218f755725041edbb8ab Mon Sep 17 00:00:00 2001 From: Phil Race Date: Mon, 21 Jul 2025 21:03:17 +0000 Subject: [PATCH] 8362452: [macOS] Remove CPrinterJob.finalize() Reviewed-by: serb, psadhukhan, kizune --- .../classes/sun/lwawt/macosx/CPrinterJob.java | 62 ++++++++++++++----- .../native/libawt_lwawt/awt/CPrinterJob.m | 10 +-- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java index 25ebc82c5f7..7b0c51f078f 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java @@ -26,14 +26,31 @@ package sun.lwawt.macosx; -import java.awt.*; +import java.awt.Color; +import java.awt.EventQueue; +import java.awt.HeadlessException; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GraphicsEnvironment; +import java.awt.SecondaryLoop; +import java.awt.Toolkit; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; -import java.awt.print.*; +import java.awt.print.Pageable; +import java.awt.print.PageFormat; +import java.awt.print.Paper; +import java.awt.print.Printable; +import java.awt.print.PrinterAbortException; +import java.awt.print.PrinterException; +import java.awt.print.PrinterJob; import java.net.URI; import java.util.concurrent.atomic.AtomicReference; -import javax.print.*; +import javax.print.DocFlavor; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import javax.print.StreamPrintService; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.standard.Chromaticity; @@ -48,8 +65,16 @@ import javax.print.attribute.standard.PageRanges; import javax.print.attribute.standard.Sides; import javax.print.attribute.Attribute; -import sun.java2d.*; -import sun.print.*; +import sun.java2d.Disposer; +import sun.java2d.DisposerRecord; +import sun.java2d.SunGraphics2D; +import sun.java2d.SurfaceData; +import sun.print.CustomMediaTray; +import sun.print.CustomOutputBin; +import sun.print.GrayscaleProxyGraphics2D; +import sun.print.PeekGraphics; +import sun.print.RasterPrinterJob; +import sun.print.SunPageSelection; public final class CPrinterJob extends RasterPrinterJob { // NOTE: This uses RasterPrinterJob as a base, but it doesn't use @@ -82,7 +107,8 @@ public final class CPrinterJob extends RasterPrinterJob { // PageFormat data is passed in and set on the fNSPrintInfo on a per call // basis. private long fNSPrintInfo = -1; - private Object fNSPrintInfoLock = new Object(); + private final Object fNSPrintInfoLock = new Object(); + private final Object disposerReferent = new Object(); static { // AWT has to be initialized for the native code to function correctly. @@ -610,25 +636,29 @@ public final class CPrinterJob extends RasterPrinterJob { // The following methods are CPrinterJob specific. - @Override - @SuppressWarnings("removal") - protected void finalize() { - synchronized (fNSPrintInfoLock) { - if (fNSPrintInfo != -1) { - dispose(fNSPrintInfo); - } - fNSPrintInfo = -1; + static class NSPrintInfoDisposer implements DisposerRecord { + + private final long fNSPrintInfo; + + NSPrintInfoDisposer(long ptr) { + fNSPrintInfo = ptr; + } + + public void dispose() { + CPrinterJob.disposeNSPrintInfo(fNSPrintInfo); } } - private native long createNSPrintInfo(); - private native void dispose(long printInfo); + private static native long createNSPrintInfo(); + private static native void disposeNSPrintInfo(long printInfo); private long getNSPrintInfo() { // This is called from the native side. synchronized (fNSPrintInfoLock) { if (fNSPrintInfo == -1) { fNSPrintInfo = createNSPrintInfo(); + Disposer.addRecord(disposerReferent, + new NSPrintInfoDisposer(fNSPrintInfo)); } return fNSPrintInfo; } diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m index 9333aa8676b..9db38ee21ff 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m @@ -617,11 +617,11 @@ JNI_COCOA_EXIT(env); * Signature: ()J */ JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPrinterJob_createNSPrintInfo - (JNIEnv *env, jobject jthis) + (JNIEnv *env, jclass clazz) { jlong result = -1; JNI_COCOA_ENTER(env); - // This is used to create the NSPrintInfo for this PrinterJob. Thread + // This is used to create the NSPrintInfo for a PrinterJob. Thread // safety is assured by the java side of this call. NSPrintInfo* printInfo = createDefaultNSPrintInfo(env, NULL); @@ -634,11 +634,11 @@ JNI_COCOA_EXIT(env); /* * Class: sun_lwawt_macosx_CPrinterJob - * Method: dispose + * Method: disposeNSPrintInfo * Signature: (J)V */ -JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_dispose - (JNIEnv *env, jobject jthis, jlong nsPrintInfo) +JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterJob_disposeNSPrintInfo + (JNIEnv *env, jclass clazz, jlong nsPrintInfo) { JNI_COCOA_ENTER(env); if (nsPrintInfo != -1)