8032397: Remove sun.misc.Ref

Reviewed-by: alanb
This commit is contained in:
Joe Darcy 2014-01-22 11:03:44 -08:00
parent fd09a81d8a
commit 6b965a6ae1
7 changed files with 86 additions and 159 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,12 +27,32 @@ package sun.applet;
import java.awt.Toolkit;
import java.awt.Image;
import java.lang.ref.SoftReference;
import sun.awt.image.URLImageSource;
import java.net.URL;
class AppletImageRef extends sun.misc.Ref {
class AppletImageRef {
private SoftReference<Image> soft = null;
URL url;
/**
* Returns a pointer to the object referenced by this Ref. If the object
* has been thrown away by the garbage collector, it will be
* reconstituted. This method does everything necessary to ensure that the garbage
* collector throws things away in Least Recently Used(LRU) order. Applications should
* never override this method. The get() method effectively caches calls to
* reconstitute().
*/
public synchronized Image get() {
Image t = check();
if (t == null) {
t = reconstitute();
setThing(t);
}
return t;
}
/**
* Create the Ref
*/
@ -40,14 +60,38 @@ class AppletImageRef extends sun.misc.Ref {
this.url = url;
}
public void flush() {
super.flush();
/**
* Flushes the cached object. Forces the next invocation of get() to
* invoke reconstitute().
*/
public synchronized void flush() {
SoftReference s = soft;
if (s != null) s.clear();
soft = null;
}
/**
* Sets the thing to the specified object.
* @param thing the specified object
*/
public synchronized void setThing(Object thing) {
flush();
soft = new SoftReference(thing);
}
/**
* Checks to see what object is being pointed at by this Ref and returns it.
*/
public synchronized Image check() {
SoftReference<Image> s = soft;
if (s == null) return null;
return s.get();
}
/**
* Reconsitute the image. Only called when the ref has been flushed.
*/
public Object reconstitute() {
public Image reconstitute() {
Image img = Toolkit.getDefaultToolkit().createImage(new URLImageSource(url));
return img;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 1998, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,21 +27,17 @@ package sun.applet;
import java.net.URL;
import java.awt.Image;
import sun.misc.Ref;
/**
* Part of this class still remains only to support legacy, 100%-impure
* applications such as HotJava 1.0.1.
*/
@Deprecated
public class AppletResourceLoader {
public static Image getImage(URL url) {
return AppletViewer.getCachedImage(url);
}
public static Ref getImageRef(URL url) {
return AppletViewer.getCachedImageRef(url);
}
public static void flushImages() {
AppletViewer.flushImageCache();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -35,7 +35,6 @@ import java.applet.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.SocketPermission;
import sun.misc.Ref;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.lang.reflect.InvocationTargetException;
@ -390,22 +389,18 @@ public class AppletViewer extends Frame implements AppletContext,
return getCachedImage(url);
}
/**
* Get an image.
*/
static Image getCachedImage(URL url) {
// System.getSecurityManager().checkConnection(url.getHost(), url.getPort());
return (Image)getCachedImageRef(url).get();
}
/**
* Get an image ref.
*/
static Ref getCachedImageRef(URL url) {
synchronized (imageRefs) {
AppletImageRef ref = (AppletImageRef)imageRefs.get(url);
if (ref == null) {
ref = new AppletImageRef(url);
imageRefs.put(url, ref);
}
return ref;
return ref.get();
}
}

View File

@ -25,6 +25,7 @@
package sun.misc;
import java.lang.ref.SoftReference;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.NoSuchElementException;
@ -32,12 +33,26 @@ import java.util.NoSuchElementException;
/**
* Caches the collision list.
*/
class CacheEntry extends Ref {
class CacheEntry {
int hash;
Object key;
CacheEntry next;
public Object reconstitute() {
return null;
SoftReference<Object> value;
public CacheEntry() {
value = null;
}
public CacheEntry(Object o) {
value = new SoftReference<>(o);
}
public Object get() {
return value.get();
}
public void setThing(Object thing) {
value = new SoftReference<>(thing);
}
}
@ -72,7 +87,6 @@ class CacheEntry extends Ref {
*
* @see java.lang.Object#hashCode
* @see java.lang.Object#equals
* @see sun.misc.Ref
* @deprecated Consider {@link java.util.LinkedHashMap} for LRU caches.
*/
@Deprecated
@ -192,7 +206,7 @@ public
int index = (hash & 0x7FFFFFFF) % tab.length;
for (CacheEntry e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return e.check();
return e.get();
}
}
return null;
@ -220,7 +234,7 @@ public
for (CacheEntry old = oldTable[i]; old != null;) {
CacheEntry e = old;
old = old.next;
if (e.check() != null) {
if (e.get() != null) {
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newTable[index];
newTable[index] = e;
@ -253,10 +267,10 @@ public
CacheEntry ne = null;
for (CacheEntry e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
Object old = e.check();
Object old = e.get();
e.setThing(value);
return old;
} else if (e.check() == null)
} else if (e.get() == null)
ne = e; /* reuse old flushed value */
}
@ -296,7 +310,7 @@ public
tab[index] = e.next;
}
count--;
return e.check();
return e.get();
}
}
return null;
@ -322,7 +336,7 @@ class CacheEnumerator implements Enumeration<Object> {
public boolean hasMoreElements() {
while (index >= 0) {
while (entry != null)
if (entry.check() != null)
if (entry.get() != null)
return true;
else
entry = entry.next;
@ -338,8 +352,8 @@ class CacheEnumerator implements Enumeration<Object> {
if (entry != null) {
CacheEntry e = entry;
entry = e.next;
if (e.check() != null)
return keys ? e.key : e.check();
if (e.get() != null)
return keys ? e.key : e.get();
}
}
throw new NoSuchElementException("CacheEnumerator");

View File

@ -1,122 +0,0 @@
/*
* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.misc;
import java.lang.ref.SoftReference;
/**
* A "Ref" is an indirect reference to an object that the garbage collector
* knows about. An application should override the reconstitute() method with one
* that will construct the object based on information in the Ref, often by
* reading from a file. The get() method retains a cache of the result of the last call to
* reconstitute() in the Ref. When space gets tight, the garbage collector
* will clear old Ref cache entries when there are no other pointers to the
* object. In normal usage, Ref will always be subclassed. The subclass will add the
* instance variables necessary for the reconstitute() method to work. It will also add a
* constructor to set them up, and write a version of reconstitute().
*
* @deprecated This class has been replaced by
* <code>java.util.SoftReference</code>.
*
* @see java.util.SoftReference
*
*/
@Deprecated
public abstract class Ref {
private SoftReference soft = null;
/**
* Returns a pointer to the object referenced by this Ref. If the object
* has been thrown away by the garbage collector, it will be
* reconstituted. This method does everything necessary to ensure that the garbage
* collector throws things away in Least Recently Used(LRU) order. Applications should
* never override this method. The get() method effectively caches calls to
* reconstitute().
*/
public synchronized Object get() {
Object t = check();
if (t == null) {
t = reconstitute();
setThing(t);
}
return t;
}
/**
* Returns a pointer to the object referenced by this Ref by
* reconstituting it from some external source (such as a file). This method should not
* bother with caching since the method get() will deal with that.
* <p>
* In normal usage, Ref will always be subclassed. The subclass will add
* the instance variables necessary for reconstitute() to work. It will
* also add a constructor to set them up, and write a version of
* reconstitute().
*/
public abstract Object reconstitute();
/**
* Flushes the cached object. Forces the next invocation of get() to
* invoke reconstitute().
*/
public synchronized void flush() {
SoftReference s = soft;
if (s != null) s.clear();
soft = null;
}
/**
* Sets the thing to the specified object.
* @param thing the specified object
*/
public synchronized void setThing(Object thing) {
flush();
soft = new SoftReference(thing);
}
/**
* Checks to see what object is being pointed at by this Ref and returns it.
*/
public synchronized Object check() {
SoftReference s = soft;
if (s == null) return null;
return s.get();
}
/**
* Constructs a new Ref.
*/
public Ref() { }
/**
* Constructs a new Ref that initially points to thing.
*/
public Ref(Object thing) {
setThing(thing);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -43,7 +43,7 @@ public class CheckPackageAccess {
* access to classes in the sun.* hierarchy, which is what is specified
* in the JDK's default java.security file.
*/
private final static String restrictedClassName = "sun.misc.Ref";
private final static String restrictedClassName = "sun.misc.Cache";
public static void main(String[] args) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -123,7 +123,7 @@ public class NotExtending implements Remote {
}
/**
* Force desparate garbage collection so that all sun.misc.Ref instances
* Force desperate garbage collection so that soft references
* will be cleared.
*
* This method is required with the JDK 1.1.x RMI runtime so that the