[weld-commits] Weld SVN: r6414 - in extensions/trunk: src/main/java/org/jboss/weld/extensions/managedproducer and 1 other directory.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Tue Jun 8 07:32:12 EDT 2010
Author: swd847
Date: 2010-06-08 07:32:11 -0400 (Tue, 08 Jun 2010)
New Revision: 6414
Modified:
extensions/trunk/pom.xml
extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerBean.java
extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerInvocationHandler.java
Log:
initial attempt at changing ManagedProducer to javassist
Modified: extensions/trunk/pom.xml
===================================================================
--- extensions/trunk/pom.xml 2010-06-08 11:15:20 UTC (rev 6413)
+++ extensions/trunk/pom.xml 2010-06-08 11:32:11 UTC (rev 6414)
@@ -154,6 +154,11 @@
</dependency>
<dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ </dependency>
+
+ <dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
@@ -164,7 +169,7 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
-
+
<dependency>
<groupId>org.jboss.test-harness</groupId>
<artifactId>jboss-test-harness</artifactId>
Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerBean.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerBean.java 2010-06-08 11:15:20 UTC (rev 6413)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerBean.java 2010-06-08 11:32:11 UTC (rev 6414)
@@ -17,15 +17,17 @@
package org.jboss.weld.extensions.managedproducer;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
+import javassist.util.proxy.MethodFilter;
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
+
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Default;
@@ -61,9 +63,6 @@
final BeanManager manager;
- final Constructor<?> proxyConstructor;
-
-
final AnnotatedMethod<?> method;
public ManagedProducerBean(AnnotatedMethod<?> method, BeanManager manager)
@@ -96,46 +95,52 @@
}
// get the bean types
types = new HashSet<Type>();
- Set<Class<?>> interfaces = new HashSet<Class<?>>();
+ Set<Class<?>> classes = new HashSet<Class<?>>();
for (Type t : method.getTypeClosure())
{
if (t instanceof Class<?>)
{
Class<?> c = (Class<?>) t;
- if (c.isInterface())
- {
- types.add(c);
- interfaces.add(c);
- }
+ types.add(c);
+ classes.add(c);
}
else if (t instanceof ParameterizedType)
{
ParameterizedType p = (ParameterizedType) t;
Class<?> c = (Class<?>) p.getRawType();
- if (c.isInterface())
- {
- types.add(t);
- }
+ types.add(t);
}
}
// build the properties
- Class<?>[] iarray = new Class[interfaces.size()];
+ Class<?>[] iarray = new Class[classes.size()];
int count = 0;
this.manager = manager;
- for (Class<?> c : interfaces)
+ for (Class<?> c : classes)
{
iarray[count++] = c;
}
- proxyClass = Proxy.getProxyClass(beanClass.getClassLoader(), iarray);
- try
+ ProxyFactory f = new ProxyFactory();
+ Class<?> retType = method.getJavaMember().getReturnType();
+ if (retType.isInterface())
{
- proxyConstructor = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
+ f.setSuperclass(Object.class);
+ Class<?>[] ifaces = { retType };
+ f.setInterfaces(ifaces);
}
- catch (Exception e)
+ else
{
- throw new RuntimeException(e);
+ f.setSuperclass(retType);
}
+ f.setFilter(new MethodFilter()
+ {
+ public boolean isHandled(Method m)
+ {
+ // ignore finalize()
+ return !m.getName().equals("finalize");
+ }
+ });
+ proxyClass = f.createClass();
}
@@ -193,12 +198,13 @@
{
} };
Set<Bean<?>> beans = manager.getBeans(InjectionPoint.class, quals);
- Bean injectionPointBean = (Bean) beans.iterator().next();
+ Bean<?> injectionPointBean = (Bean<?>) beans.iterator().next();
InjectionPoint injectionPoint = (InjectionPoint) manager.getReference(injectionPointBean, InjectionPoint.class, creationalContext);
ManagedProducerInvocationHandler<?> hdl = new ManagedProducerInvocationHandler(manager, this.method, this, injectionPoint);
try
{
- M obj = (M) proxyConstructor.newInstance(new Object[] { hdl });
+ M obj = (M) proxyClass.newInstance();
+ ((ProxyObject) obj).setHandler(hdl);
creationalContext.push(obj);
return obj;
}
Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerInvocationHandler.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerInvocationHandler.java 2010-06-08 11:15:20 UTC (rev 6413)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/managedproducer/ManagedProducerInvocationHandler.java 2010-06-08 11:32:11 UTC (rev 6414)
@@ -17,7 +17,6 @@
package org.jboss.weld.extensions.managedproducer;
import java.lang.annotation.Annotation;
-import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
@@ -26,6 +25,8 @@
import java.util.Map;
import java.util.Set;
+import javassist.util.proxy.MethodHandler;
+
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.AmbiguousResolutionException;
import javax.enterprise.inject.UnsatisfiedResolutionException;
@@ -36,7 +37,7 @@
import org.jboss.weld.extensions.bean.InjectionPointImpl;
-public class ManagedProducerInvocationHandler<T> implements InvocationHandler
+public class ManagedProducerInvocationHandler<T> implements MethodHandler
{
final BeanManager manager;
@@ -82,7 +83,7 @@
this.injectionPoint = injectionPoint;
}
- public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
+ public Object invoke(Object proxy, Method thisMethod, Method m, Object[] args) throws Throwable
{
CreationalContext<?> ctx = manager.createCreationalContext(bean);
Object[] params = new Object[method.getParameterTypes().length];
@@ -100,7 +101,7 @@
Object base = manager.getReference(mainClassBean, annotatedMethod.getDeclaringType().getJavaClass(), ctx);
Object result = method.invoke(base, params);
- Object ret = m.invoke(result, args);
+ Object ret = thisMethod.invoke(result, args);
ctx.release();
return ret;
}
More information about the weld-commits
mailing list