You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jc...@apache.org on 2005/09/04 07:37:55 UTC
svn commit: r278561 - in /jakarta/commons/sandbox/proxy/trunk/src:
java/org/apache/commons/proxy/factory/cglib/
java/org/apache/commons/proxy/factory/javassist/
java/org/apache/commons/proxy/factory/reflect/
java/org/apache/commons/proxy/factory/util/ ...
Author: jcarman
Date: Sat Sep 3 22:37:49 2005
New Revision: 278561
URL: http://svn.apache.org/viewcvs?rev=278561&view=rev
Log:
Added support for "invocation handler proxies."
Modified:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java Sat Sep 3 22:37:49 2005
@@ -26,6 +26,7 @@
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
+import java.lang.reflect.InvocationHandler;
/**
* A <a href="http://cglib.sourceforge.net/">CGLIB</a>-based {@link org.apache.commons.proxy.ProxyFactory}
@@ -56,6 +57,30 @@
return enhancer.create();
}
+ public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler,
+ Class... proxyInterfaces )
+ {
+ final Enhancer enhancer = new Enhancer();
+ enhancer.setClassLoader( classLoader );
+ enhancer.setInterfaces( proxyInterfaces );
+ enhancer.setCallback( new InvocationHandlerBridge( invocationHandler ) );
+ return enhancer.create();
+ }
+
+ private class InvocationHandlerBridge implements net.sf.cglib.proxy.InvocationHandler
+ {
+ private final InvocationHandler original;
+
+ public InvocationHandlerBridge( InvocationHandler original )
+ {
+ this.original = original;
+ }
+
+ public Object invoke( Object object, Method method, Object[] objects ) throws Throwable
+ {
+ return original.invoke( object, method, objects );
+ }
+ }
private class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor
{
private final MethodInterceptor inner;
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java Sat Sep 3 22:37:49 2005
@@ -28,6 +28,7 @@
import org.apache.commons.proxy.factory.util.ProxyClassCache;
import java.lang.reflect.Method;
+import java.lang.reflect.InvocationHandler;
/**
* A <a href="http://www.jboss.org/products/javassist">Javassist</a>-based {@link org.apache.commons.proxy.ProxyFactory}
@@ -42,6 +43,7 @@
new DelegatingProxyClassGenerator() );
private static final ProxyClassCache interceptingProxyClassCache = new ProxyClassCache(
new InterceptingProxyClassGenerator() );
+ private static final ProxyClassCache invocationHandlerProxyClassCache = new ProxyClassCache( new InvocationHandlerProxyClassGenerator() );
public Object createInterceptingProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor,
Class... proxyInterfaces )
@@ -71,6 +73,59 @@
catch( Exception e )
{
throw new ProxyFactoryException( "Unable to instantiate proxy from generated proxy class.", e );
+ }
+ }
+
+ public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler,
+ Class... proxyInterfaces )
+ {
+ try
+ {
+
+ final Class clazz = invocationHandlerProxyClassCache.getProxyClass( classLoader, proxyInterfaces );
+ final Method[] methods = AbstractProxyClassGenerator.getImplementationMethods( proxyInterfaces );
+ return clazz.getConstructor( Method[].class, InvocationHandler.class ).newInstance( methods, invocationHandler );
+ }
+ catch( Exception e )
+ {
+ throw new ProxyFactoryException( "Unable to instantiate proxy from generated proxy class.", e );
+ }
+ }
+
+ private static class InvocationHandlerProxyClassGenerator extends AbstractProxyClassGenerator
+ {
+ public Class generateProxyClass( ClassLoader classLoader, Class... proxyInterfaces )
+ {
+ try
+ {
+ final CtClass proxyClass = JavassistUtils.createClass();
+ final Method[] methods = getImplementationMethods( proxyInterfaces );
+ JavassistUtils.addInterfaces( proxyClass, proxyInterfaces );
+ JavassistUtils.addField( Method[].class, "methods", proxyClass );
+ JavassistUtils.addField( InvocationHandler.class, "invocationHandler", proxyClass );
+ final CtConstructor proxyConstructor = new CtConstructor(
+ JavassistUtils.resolve(
+ new Class[]{Method[].class, InvocationHandler.class} ),
+ proxyClass );
+ proxyConstructor
+ .setBody( "{\n\tthis.methods = $1;\n\tthis.invocationHandler = $2; }" );
+ proxyClass.addConstructor( proxyConstructor );
+ for( int i = 0; i < methods.length; ++i )
+ {
+ final CtMethod method = new CtMethod( JavassistUtils.resolve( methods[i].getReturnType() ),
+ methods[i].getName(),
+ JavassistUtils.resolve( methods[i].getParameterTypes() ),
+ proxyClass );
+ final String body = "{\n\t return ( $r ) invocationHandler.invoke( this, methods[" + i + "], $args );\n }";
+ method.setBody( body );
+ proxyClass.addMethod( method );
+ }
+ return proxyClass.toClass( classLoader );
+ }
+ catch( CannotCompileException e )
+ {
+ throw new ProxyFactoryException( "Could not compile class.", e );
+ }
}
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java Sat Sep 3 22:37:49 2005
@@ -21,6 +21,7 @@
import org.apache.commons.proxy.factory.util.AbstractProxyFactory;
import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
/**
* A JDK {@link java.lang.reflect.Proxy Proxy}-based {@link org.apache.commons.proxy.ProxyFactory} implementation.
@@ -44,4 +45,9 @@
new DelegateProviderInvocationHandler( targetProvider ) );
}
+ public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler,
+ Class... proxyInterfaces )
+ {
+ return Proxy.newProxyInstance( classLoader, proxyInterfaces, invocationHandler );
+ }
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java Sat Sep 3 22:37:49 2005
@@ -22,7 +22,6 @@
import org.apache.commons.logging.LogFactory;
import org.apache.commons.proxy.ObjectProvider;
import org.apache.commons.proxy.ProxyFactory;
-import org.apache.commons.proxy.provider.NullProvider;
import java.lang.reflect.InvocationHandler;
@@ -46,24 +45,18 @@
this.log = log;
}
- public Object createInterceptingProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
+ public final Object createInterceptingProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
{
return createInterceptingProxy( Thread.currentThread().getContextClassLoader(), target, interceptor,
proxyInterfaces );
}
- public Object createDelegatingProxy( ObjectProvider targetProvider, Class... proxyInterfaces )
+ public final Object createDelegatingProxy( ObjectProvider targetProvider, Class... proxyInterfaces )
{
return createDelegatingProxy( Thread.currentThread().getContextClassLoader(), targetProvider, proxyInterfaces );
}
- public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler,
- Class... proxyInterfaces )
- {
- return createInterceptingProxy( classLoader, new NullProvider(), new InvocationHandlerMethodInterceptor( invocationHandler ), proxyInterfaces );
- }
-
- public Object createInvocationHandlerProxy( InvocationHandler invocationHandler, Class... proxyInterfaces )
+ public final Object createInvocationHandlerProxy( InvocationHandler invocationHandler, Class... proxyInterfaces )
{
return createInvocationHandlerProxy( Thread.currentThread().getContextClassLoader(), invocationHandler, proxyInterfaces );
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java?rev=278561&r1=278560&r2=278561&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java Sat Sep 3 22:37:49 2005
@@ -60,6 +60,7 @@
final Echo echo = ( Echo )factory.createInvocationHandlerProxy( tester, Echo.class );
echo.echoBack( "hello" );
assertEquals( Echo.class.getMethod( "echoBack", String.class ), tester.method );
+ assertSame( echo, tester.proxy );
assertNotNull( tester.args );
assertEquals( 1, tester.args.length );
assertEquals( "hello", tester.args[0] );
@@ -245,9 +246,11 @@
{
private Object method;
private Object[] args;
+ private Object proxy;
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
{
+ this.proxy = proxy;
this.method = method;
this.args = args;
return null;
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org