Permalink
Browse files

Store the JavaProxyClass cache in JavaSupport (refs #164)

This keeps the cache of proxies inside an instance of a Ruby runtime
versus hanging around in a static map in JavaProxyClassFactory.
  • Loading branch information...
1 parent 3d31a65 commit 7cae1101029d50af5fe4e3efc447a92b8b519153 @bbrowning committed May 15, 2012
@@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -49,6 +50,7 @@
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.Unrescuable;
import org.jruby.java.proxies.ProxyCache;
+import org.jruby.javasupport.proxy.JavaProxyClass;
import org.jruby.javasupport.util.ObjectProxyCache;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.WeakIdentityHashMap;
@@ -124,6 +126,9 @@ public IRubyObject allocateProxy(Object javaObject, RubyClass clazz) {
private final Map<String, JavaClass> nameClassMap = new HashMap<String, JavaClass>();
private final Map<Object, Object[]> javaObjectVariables = new WeakIdentityHashMap();
+
+ // A cache of all JavaProxyClass objects created for this runtime
+ private Map<Set<?>, JavaProxyClass> javaProxyClassCache = Collections.synchronizedMap(new HashMap<Set<?>, JavaProxyClass>());
public JavaSupport(Ruby ruby) {
this.runtime = ruby;
@@ -342,4 +347,8 @@ public RubyClass getJavaConstructorClass() {
return javaConstructorClass = getJavaModule().getClass("JavaConstructor");
}
+ public Map<Set<?>, JavaProxyClass> getJavaProxyClassCache() {
+ return this.javaProxyClassCache;
+ }
+
}
@@ -28,7 +28,6 @@
package org.jruby.javasupport.proxy;
-import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -39,15 +38,14 @@
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-import java.util.WeakHashMap;
import org.jruby.Ruby;
+import org.jruby.javasupport.JavaSupport;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
@@ -102,8 +100,6 @@
private static int counter;
- private static Map proxies = Collections.synchronizedMap(new WeakHashMap());
-
private static Method defineClass_method; // statically initialized below
private static synchronized int nextId() {
@@ -148,8 +144,9 @@ static JavaProxyClass newProxyClass(Ruby runtime, ClassLoader loader,
key.addAll(names);
}
- WeakReference<JavaProxyClass> proxyRef = (WeakReference<JavaProxyClass>) proxies.get(key);
- JavaProxyClass proxyClass = proxyRef == null ? null : proxyRef.get();
+ Map<Set<?>, JavaProxyClass> proxyCache =
+ runtime.getJavaSupport().getJavaProxyClassCache();
+ JavaProxyClass proxyClass = proxyCache.get(key);
if (proxyClass == null) {
if (targetClassName == null) {
@@ -177,7 +174,7 @@ static JavaProxyClass newProxyClass(Ruby runtime, ClassLoader loader,
proxyClass = generate(loader, targetClassName, superClass,
interfaces, methods, selfType);
- proxies.put(key, new WeakReference<JavaProxyClass>(proxyClass));
+ proxyCache.put(key, proxyClass);
}
return proxyClass;

0 comments on commit 7cae110

Please sign in to comment.