Revision: 1c9b92a5d869
Author: Sam Berlin <[email protected]>
Date: Sat Jan 21 09:55:00 2012
Log: Fix issue 643 using a slightly modified version of the patch
provided by
Stuart.
http://code.google.com/p/google-guice/source/detail?r=1c9b92a5d869
Modified:
/core/src/com/google/inject/internal/ProxyFactory.java
/core/test/com/google/inject/MethodInterceptionTest.java
=======================================
--- /core/src/com/google/inject/internal/ProxyFactory.java Thu Jul 7
17:34:16 2011
+++ /core/src/com/google/inject/internal/ProxyFactory.java Sat Jan 21
09:55:00 2012
@@ -24,6 +24,7 @@
import com.google.common.collect.Maps;
import com.google.inject.spi.InjectionPoint;
+import net.sf.cglib.core.MethodWrapper;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
@@ -169,7 +170,7 @@
// to this injector. Otherwise, the proxies for each injector will
waste PermGen memory
try {
Enhancer enhancer = BytecodeGen.newEnhancer(declaringClass,
visibility);
- enhancer.setCallbackFilter(new IndicesCallbackFilter(declaringClass,
methods));
+ enhancer.setCallbackFilter(new IndicesCallbackFilter(methods));
enhancer.setCallbackTypes(callbackTypes);
return new ProxyConstructor<T>(enhancer, injectionPoint, callbacks,
interceptors);
} catch (Throwable e) {
@@ -198,35 +199,35 @@
}
/**
- * A callback filter that maps methods to unique IDs. We define equals
and hashCode using the
- * declaring class so that enhanced classes can be shared between
injectors.
+ * A callback filter that maps methods to unique IDs. We define equals
and
+ * hashCode without using any state related to the injector so that
enhanced
+ * classes intercepting the same methods can be shared between injectors
(and
+ * child injectors, etc).
*/
private static class IndicesCallbackFilter implements CallbackFilter {
- final Class<?> declaringClass;
- final Map<Method, Integer> indices;
-
- IndicesCallbackFilter(Class<?> declaringClass, List<Method> methods) {
- this.declaringClass = declaringClass;
- final Map<Method, Integer> indices = Maps.newHashMap();
+ final Map<Object, Integer> indices;
+ final int hashCode;
+
+ IndicesCallbackFilter(List<Method> methods) {
+ final Map<Object, Integer> indices = Maps.newHashMap();
for (int i = 0; i < methods.size(); i++) {
- Method method = methods.get(i);
- indices.put(method, i);
- }
-
+ indices.put(MethodWrapper.create(methods.get(i)), i);
+ }
this.indices = indices;
+ this.hashCode = indices.hashCode();
}
public int accept(Method method) {
- return indices.get(method);
+ return indices.get(MethodWrapper.create(method));
}
@Override public boolean equals(Object o) {
return o instanceof IndicesCallbackFilter &&
- ((IndicesCallbackFilter) o).declaringClass == declaringClass;
+ ((IndicesCallbackFilter) o).indices.equals(indices);
}
@Override public int hashCode() {
- return declaringClass.hashCode();
+ return hashCode;
}
}
=======================================
--- /core/test/com/google/inject/MethodInterceptionTest.java Thu Jul 7
17:34:16 2011
+++ /core/test/com/google/inject/MethodInterceptionTest.java Sat Jan 21
09:55:00 2012
@@ -79,7 +79,7 @@
Interceptable nullFoosOne = childOne.getInstance(Interceptable.class);
assertNotNull(nullFoosOne.bar());
- assertNull(nullFoosOne.foo());
+ assertNull(nullFoosOne.foo()); // confirm it's being intercepted
Injector childTwo = injector.createChildInjector(new AbstractModule() {
protected void configure() {
@@ -88,10 +88,21 @@
});
Interceptable nullFoosTwo = childTwo.getInstance(Interceptable.class);
- assertNull(nullFoosTwo.foo());
+ assertNull(nullFoosTwo.foo()); // confirm it's being intercepted
assertSame("Child injectors should share proxy classes, otherwise
memory leaks!",
nullFoosOne.getClass(), nullFoosTwo.getClass());
+
+ Injector injector2 = Guice.createInjector(new AbstractModule() {
+ protected void configure() {
+ bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)),
+ new ReturnNullInterceptor());
+ }
+ });
+ Interceptable separateNullFoos =
injector2.getInstance(Interceptable.class);
+ assertNull(separateNullFoos.foo()); // confirm it's being intercepted
+ assertSame("different injectors should share proxy classes, otherwise
memory leaks!",
+ nullFoosOne.getClass(), separateNullFoos.getClass());
}
public void testGetThis() {
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.