Skip to content

Commit 982aa3f

Browse files
committed
8336654: [lworld] Tests depending on sun.awt.AppContext can fail when run with migrated classes
Reviewed-by: serb, azvegint
1 parent 1342db0 commit 982aa3f

File tree

4 files changed

+32
-142
lines changed

4 files changed

+32
-142
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaUtils.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import javax.swing.border.Border;
3636
import javax.swing.plaf.UIResource;
3737

38-
import sun.awt.AppContext;
39-
4038
import sun.lwawt.macosx.CPlatformWindow;
4139
import sun.swing.SwingUtilities2;
4240

@@ -150,13 +148,34 @@ T get() {
150148
protected abstract T create();
151149
}
152150

151+
abstract static class LazySingleton<T> {
152+
T instance;
153+
154+
final T get() {
155+
if (instance == null) {
156+
instance = getInstance();
157+
}
158+
return instance;
159+
}
160+
161+
abstract T getInstance();
162+
}
163+
153164
abstract static class RecyclableSingleton<T> {
154-
final T get() {
155-
return AppContext.getSoftReferenceValue(this, () -> getInstance());
156-
}
157165

158-
void reset() {
159-
AppContext.getAppContext().remove(this);
166+
SoftReference<T> ref;
167+
168+
final T get() {
169+
T instance;
170+
if (ref != null) {
171+
instance = ref.get();
172+
if (instance != null) {
173+
return instance;
174+
}
175+
}
176+
instance = getInstance();
177+
ref = new SoftReference<>(instance);
178+
return instance;
160179
}
161180

162181
abstract T getInstance();
@@ -197,11 +216,11 @@ V get(final K key) {
197216
protected abstract V getInstance(K key);
198217
}
199218

200-
private static final RecyclableSingleton<Boolean> enableAnimations = new RecyclableSingleton<Boolean>() {
219+
private static final LazySingleton<Boolean> enableAnimations = new LazySingleton<Boolean>() {
201220
@Override
202221
protected Boolean getInstance() {
203-
final String sizeProperty = System.getProperty(ANIMATIONS_PROPERTY);
204-
return !"false".equals(sizeProperty); // should be true by default
222+
final String animationsProperty = System.getProperty(ANIMATIONS_PROPERTY);
223+
return !"false".equals(animationsProperty); // should be true by default
205224
}
206225
};
207226
private static boolean animationsEnabled() {

src/java.desktop/share/classes/sun/awt/AppContext.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.concurrent.locks.Lock;
4848
import java.util.concurrent.locks.ReentrantLock;
4949
import java.util.concurrent.atomic.AtomicInteger;
50-
import java.util.function.Supplier;
5150

5251
/**
5352
* The AppContext is a table referenced by ThreadGroup which stores
@@ -735,24 +734,6 @@ public synchronized PropertyChangeListener[] getPropertyChangeListeners(
735734
}
736735
return changeSupport.getPropertyChangeListeners(propertyName);
737736
}
738-
739-
public static <T> T getSoftReferenceValue(Object key,
740-
Supplier<T> supplier) {
741-
742-
final AppContext appContext = AppContext.getAppContext();
743-
@SuppressWarnings("unchecked")
744-
SoftReference<T> ref = (SoftReference<T>) appContext.get(key);
745-
if (ref != null) {
746-
final T object = ref.get();
747-
if (object != null) {
748-
return object;
749-
}
750-
}
751-
final T object = supplier.get();
752-
ref = new SoftReference<>(object);
753-
appContext.put(key, ref);
754-
return object;
755-
}
756737
}
757738

758739
final class MostRecentKeyValue {

src/java.desktop/share/classes/sun/awt/image/ImageCache.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
import java.lang.ref.*;
3030
import java.util.*;
3131
import java.util.concurrent.locks.*;
32-
import sun.awt.AppContext;
3332

3433
/**
3534
* ImageCache - A fixed pixel count sized cache of Images keyed by arbitrary
3635
* set of arguments. All images are held with SoftReferences so they will be
3736
* dropped by the GC if heap memory gets tight. When our size hits max pixel
3837
* count least recently requested images are removed first.
3938
*
40-
* The ImageCache must be used from the thread with an AppContext only.
41-
*
4239
*/
4340
public final class ImageCache {
4441

@@ -56,9 +53,10 @@ public final class ImageCache {
5653
// Reference queue for tracking lost softreferences to images in the cache
5754
private final ReferenceQueue<Image> referenceQueue = new ReferenceQueue<>();
5855

56+
private static final ImageCache instance = new ImageCache();
57+
5958
public static ImageCache getInstance() {
60-
return AppContext.getSoftReferenceValue(ImageCache.class,
61-
() -> new ImageCache());
59+
return instance;
6260
}
6361

6462
ImageCache(final int maxPixelCount) {

test/jdk/javax/swing/Security/6657138/bug6657138.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)