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/08 13:59:50 UTC
svn commit: r279556 - in /jakarta/commons/sandbox/proxy/trunk/src:
java/org/apache/commons/proxy/ java/org/apache/commons/proxy/factory/reflect/
java/org/apache/commons/proxy/interceptor/
test/org/apache/commons/proxy/factory/ test/org/apache/commons/p...
Author: jcarman
Date: Thu Sep 8 04:59:42 2005
New Revision: 279556
URL: http://svn.apache.org/viewcvs?rev=279556&view=rev
Log:
Added in test cases for abstract superclasses (shed light on another bug with interface hierarchies).
Modified:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodInterceptorChain.java
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java?rev=279556&r1=279555&r2=279556&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java Thu Sep 8 04:59:42 2005
@@ -18,6 +18,10 @@
import org.apache.commons.proxy.handler.NullInvocationHandler;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+
/**
* @author James Carman
* @version 1.0
@@ -45,5 +49,59 @@
public static Object createNullObject( ProxyFactory proxyFactory, ClassLoader classLoader, Class... proxyClasses )
{
return proxyFactory.createInvocationHandlerProxy( classLoader, new NullInvocationHandler(), proxyClasses );
+ }
+
+
+ /**
+ * <p>Gets an array of {@link Class} objects representing all interfaces implemented by the given
+ * class and its superclasses.</p>
+ *
+ * <p>The order is determined by looking through each interface in turn as
+ * declared in the source file and following its hierarchy up. Then each
+ * superclass is considered in the same way. Later duplicates are ignored,
+ * so the order is maintained.</p>
+ * <p>
+ * <b>Note</b>: Implementation of this method was "borrowed" from
+ * <a href="http://jakarta.apache.org/commons/lang/">Jakarta Commons Lang</a> to avoid a dependency.
+ * </p>
+ * @param cls the class to look up, may be <code>null</code>
+ * @return an array of {@link Class} objects representing all interfaces implemented by the given
+ * class and its superclasses or <code>null</code> if input class is null.
+ */
+ public static Class[] getAllInterfaces( Class cls )
+ {
+ final List<Class> interfaces = getAllInterfacesImpl( cls );
+ return ( Class[] ) interfaces.toArray( new Class[interfaces.size()] );
+ }
+
+ private static List<Class> getAllInterfacesImpl( Class cls )
+ {
+ if( cls == null )
+ {
+ return null;
+ }
+ List<Class> list = new ArrayList<Class>();
+ while( cls != null )
+ {
+ Class[] interfaces = cls.getInterfaces();
+ for( int i = 0; i < interfaces.length; i++ )
+ {
+ if( !list.contains( interfaces[i] ) )
+ {
+ list.add( interfaces[i] );
+ }
+ List superInterfaces = getAllInterfacesImpl( interfaces[i] );
+ for( Iterator it = superInterfaces.iterator(); it.hasNext(); )
+ {
+ Class intface = ( Class ) it.next();
+ if( !list.contains( intface ) )
+ {
+ list.add( intface );
+ }
+ }
+ }
+ cls = cls.getSuperclass();
+ }
+ return list;
}
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java?rev=279556&r1=279555&r2=279556&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java Thu Sep 8 04:59:42 2005
@@ -16,6 +16,8 @@
*/
package org.apache.commons.proxy.factory.reflect;
+import org.apache.commons.proxy.ProxyUtils;
+
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -61,7 +63,7 @@
*/
public Object createProxy()
{
- return createProxy( getDelegate().getClass().getInterfaces() );
+ return createProxy( ProxyUtils.getAllInterfaces( getDelegate().getClass() ) );
}
/**
@@ -72,7 +74,7 @@
*/
public Object createProxy( ClassLoader classLoader )
{
- return createProxy( classLoader, getDelegate().getClass().getInterfaces() );
+ return createProxy( classLoader, ProxyUtils.getAllInterfaces( getDelegate().getClass() ) );
}
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodInterceptorChain.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodInterceptorChain.java?rev=279556&r1=279555&r2=279556&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodInterceptorChain.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodInterceptorChain.java Thu Sep 8 04:59:42 2005
@@ -19,8 +19,13 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.commons.proxy.ObjectProvider;
import org.apache.commons.proxy.ProxyFactory;
+import org.apache.commons.proxy.ProxyUtils;
import org.apache.commons.proxy.provider.AbstractObjectProvider;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
/**
* A <code>MethodInterceptorChain</code> assists with creating proxies which go through a series of
* <code>MethodInterceptors</code>.
@@ -33,7 +38,6 @@
//----------------------------------------------------------------------------------------------------------------------
// Fields
//----------------------------------------------------------------------------------------------------------------------
-
private final MethodInterceptor[] interceptors;
//----------------------------------------------------------------------------------------------------------------------
@@ -67,12 +71,14 @@
proxyClasses );
}
+
+
public ObjectProvider createProxyProvider( ProxyFactory proxyFactory, ClassLoader classLoader, Object terminus,
Class... proxyClasses )
{
if( proxyClasses.length == 0 )
{
- proxyClasses = terminus.getClass().getInterfaces();
+ proxyClasses = ProxyUtils.getAllInterfaces( terminus.getClass() );
}
return new ProxyObjectProvider( proxyFactory, classLoader, terminus, proxyClasses );
}
Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java?rev=279556&r1=279555&r2=279556&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java Thu Sep 8 04:59:42 2005
@@ -20,6 +20,7 @@
import org.apache.commons.proxy.exception.ProxyFactoryException;
import org.apache.commons.proxy.handler.NullInvocationHandler;
import org.apache.commons.proxy.provider.ConstantProvider;
+import org.apache.commons.proxy.util.AbstractEcho;
import org.apache.commons.proxy.util.Echo;
import org.apache.commons.proxy.util.EchoImpl;
@@ -32,6 +33,13 @@
protected AbstractSubclassingProxyFactoryTestCase( ProxyFactory factory )
{
super( factory );
+ }
+
+ public void testWithAbstractSuperclass()
+ {
+ final Echo echo = ( Echo )factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ), AbstractEcho.class );
+ assertEquals( "hello", echo.echoBack( "hello" ) );
+ assertEquals( "helloworld", echo.echoBack( "hello", "world" ) );
}
public void testCanProxy()
Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java?rev=279556&r1=279555&r2=279556&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java Thu Sep 8 04:59:42 2005
@@ -21,12 +21,8 @@
* @author James Carman
* @version 1.0
*/
-public class EchoImpl implements Echo, DuplicateEcho
+public class EchoImpl extends AbstractEcho implements DuplicateEcho
{
- public String echoBack( String message )
- {
- return message;
- }
public String echoBack( String message1, String message2 )
{
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org