Skip to content

Commit 523993e

Browse files
committed
8362291: [macOS] Remove finalize method in CGraphicsEnvironment.java
Reviewed-by: bchristi, serb, kizune
1 parent b8da969 commit 523993e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/java.desktop/macosx/classes/sun/awt/CGraphicsEnvironment.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.util.ListIterator;
3838
import java.util.Map;
3939

40+
import sun.java2d.Disposer;
41+
import sun.java2d.DisposerRecord;
4042
import sun.java2d.SunGraphicsEnvironment;
4143

4244
/**
@@ -82,7 +84,7 @@ public static void init() { }
8284
/**
8385
* Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
8486
*/
85-
private native void deregisterDisplayReconfiguration(long context);
87+
private static native void deregisterDisplayReconfiguration(long context);
8688

8789
/** Available CoreGraphics displays. */
8890
private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);
@@ -93,6 +95,7 @@ public static void init() { }
9395

9496
/** Reference to the display reconfiguration callback context. */
9597
private final long displayReconfigContext;
98+
private final Object disposerReferent = new Object();
9699

97100
// list of invalidated graphics devices (those which were removed)
98101
private List<WeakReference<CGraphicsDevice>> oldDevices = new ArrayList<>();
@@ -114,6 +117,7 @@ public CGraphicsEnvironment() {
114117
if (displayReconfigContext == 0L) {
115118
throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
116119
}
120+
Disposer.addRecord(disposerReferent, new CGEDisposerRecord(displayReconfigContext));
117121
}
118122

119123
/**
@@ -139,12 +143,14 @@ void _displayReconfiguration(int displayId, boolean removed) {
139143
rebuildDevices();
140144
}
141145

142-
@Override
143-
@SuppressWarnings("removal")
144-
protected void finalize() throws Throwable {
145-
try {
146-
super.finalize();
147-
} finally {
146+
private static class CGEDisposerRecord implements DisposerRecord {
147+
private final long displayReconfigContext;
148+
149+
CGEDisposerRecord(long ptr) {
150+
displayReconfigContext = ptr;
151+
}
152+
153+
public void dispose() {
148154
deregisterDisplayReconfiguration(displayReconfigContext);
149155
}
150156
}

src/java.desktop/macosx/native/libawt_lwawt/awt/CGraphicsEnv.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
*/
163163
JNIEXPORT void JNICALL
164164
Java_sun_awt_CGraphicsEnvironment_deregisterDisplayReconfiguration
165-
(JNIEnv *env, jobject this, jlong p)
165+
(JNIEnv *env, jclass clazz, jlong p)
166166
{
167167
JNI_COCOA_ENTER(env);
168168

src/java.desktop/share/classes/sun/java2d/Disposer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void run() {
137137
obj = null;
138138
rec = null;
139139
clearDeferredRecords();
140-
} catch (Exception e) {
140+
} catch (Throwable t) {
141141
System.out.println("Exception while removing reference.");
142142
}
143143
}
@@ -157,7 +157,7 @@ public static interface PollDisposable {
157157
private static void safeDispose(DisposerRecord rec) {
158158
try {
159159
rec.dispose();
160-
} catch (final Exception e) {
160+
} catch (final Throwable t) {
161161
System.out.println("Exception while disposing deferred rec.");
162162
}
163163
}
@@ -212,7 +212,7 @@ public static void pollRemove() {
212212
deferredRecords.offerLast(rec);
213213
}
214214
}
215-
} catch (Exception e) {
215+
} catch (Throwable t) {
216216
System.out.println("Exception while removing reference.");
217217
} finally {
218218
pollingQueue = false;

0 commit comments

Comments
 (0)