commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r631860 - in /commons/proper/proxy/trunk/src: main/java/org/apache/commons/proxy/ main/java/org/apache/commons/proxy/factory/cglib/ main/java/org/apache/commons/proxy/factory/javassist/ test/java/org/apache/commons/proxy/factory/
Date Thu, 28 Feb 2008 05:58:55 GMT
Author: jcarman
Date: Wed Feb 27 21:58:47 2008
New Revision: 631860

URL: http://svn.apache.org/viewvc?rev=631860&view=rev
Log:
Working on serialization, equals, and hashCode implementations.

Modified:
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/ProxyFactory.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
    commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java
    commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/ProxyFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/ProxyFactory.java?rev=631860&r1=631859&r2=631860&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/ProxyFactory.java (original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/ProxyFactory.java Wed Feb 27 21:58:47 2008
@@ -58,12 +58,12 @@
      * @param proxyClasses the proxy classes
      * @return true if all <code>proxyClasses</code> are interfaces
      */
-    public boolean canProxy( Class[] proxyClasses )
+    public boolean canProxy(Class[] proxyClasses)
     {
-        for( int i = 0; i < proxyClasses.length; i++ )
+        for (int i = 0; i < proxyClasses.length; i++)
         {
             Class proxyClass = proxyClasses[i];
-            if( !proxyClass.isInterface() )
+            if (!proxyClass.isInterface())
             {
                 return false;
             }
@@ -79,7 +79,7 @@
      * @param proxyClasses     the interfaces that the proxy should implement
      * @return a proxy which delegates to the object provided by the target object provider
      */
-    public Object createDelegatorProxy( ObjectProvider delegateProvider, Class[] proxyClasses )
+    public Object createDelegatorProxy(ObjectProvider delegateProvider, Class[] proxyClasses)
     {
         return createDelegatorProxy(Thread.currentThread().getContextClassLoader(), delegateProvider, proxyClasses);
     }
@@ -92,8 +92,8 @@
      * @param proxyClasses     the interfaces that the proxy should implement
      * @return a proxy which delegates to the object provided by the target <code>delegateProvider>
      */
-    public Object createDelegatorProxy( ClassLoader classLoader, ObjectProvider delegateProvider,
-                                        Class[] proxyClasses )
+    public Object createDelegatorProxy(ClassLoader classLoader, ObjectProvider delegateProvider,
+                                       Class[] proxyClasses)
     {
         return Proxy.newProxyInstance(classLoader, proxyClasses,
                 new DelegatorInvocationHandler(delegateProvider));
@@ -109,8 +109,8 @@
      * @return a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the
      *         <code>target</code> object.
      */
-    public Object createInterceptorProxy( Object target, Interceptor interceptor,
-                                          Class[] proxyClasses )
+    public Object createInterceptorProxy(Object target, Interceptor interceptor,
+                                         Class[] proxyClasses)
     {
         return createInterceptorProxy(Thread.currentThread().getContextClassLoader(), target, interceptor,
                 proxyClasses);
@@ -127,8 +127,8 @@
      * @return a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the
      *         <code>target</code> object.
      */
-    public Object createInterceptorProxy( ClassLoader classLoader, Object target, Interceptor interceptor,
-                                          Class[] proxyClasses )
+    public Object createInterceptorProxy(ClassLoader classLoader, Object target, Interceptor interceptor,
+                                         Class[] proxyClasses)
     {
         return Proxy
                 .newProxyInstance(classLoader, proxyClasses, new InterceptorInvocationHandler(target, interceptor));
@@ -142,7 +142,7 @@
      * @param proxyClasses the interfaces that the proxy should implement
      * @return a proxy which uses the provided {@link Invoker} to handle all method invocations
      */
-    public Object createInvokerProxy( Invoker invoker, Class[] proxyClasses )
+    public Object createInvokerProxy(Invoker invoker, Class[] proxyClasses)
     {
         return createInvokerProxy(Thread.currentThread().getContextClassLoader(), invoker,
                 proxyClasses);
@@ -156,8 +156,8 @@
      * @param proxyClasses the interfaces that the proxy should implement
      * @return a proxy which uses the provided {@link Invoker} to handle all method invocations
      */
-    public Object createInvokerProxy( ClassLoader classLoader, Invoker invoker,
-                                      Class[] proxyClasses )
+    public Object createInvokerProxy(ClassLoader classLoader, Invoker invoker,
+                                     Class[] proxyClasses)
     {
         return Proxy.newProxyInstance(classLoader, proxyClasses, new InvokerInvocationHandler(invoker));
     }
@@ -166,71 +166,107 @@
 // Inner Classes
 //**********************************************************************************************************************
 
-    private static class DelegatorInvocationHandler implements InvocationHandler, Serializable
+    private static class DelegatorInvocationHandler extends AbstractInvocationHandler
     {
         private final ObjectProvider delegateProvider;
 
-        protected DelegatorInvocationHandler( ObjectProvider delegateProvider )
+        protected DelegatorInvocationHandler(ObjectProvider delegateProvider)
         {
             this.delegateProvider = delegateProvider;
         }
 
-        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+        public Object invokeImpl(Object proxy, Method method, Object[] args) throws Throwable
         {
             try
             {
                 return method.invoke(delegateProvider.getObject(), args);
             }
-            catch( InvocationTargetException e )
+            catch (InvocationTargetException e)
             {
                 throw e.getTargetException();
             }
         }
     }
 
-    private static class InterceptorInvocationHandler implements InvocationHandler, Serializable
+    private static class InterceptorInvocationHandler extends AbstractInvocationHandler
     {
         private final Object target;
         private final Interceptor methodInterceptor;
 
-        public InterceptorInvocationHandler( Object target, Interceptor methodInterceptor )
+        public InterceptorInvocationHandler(Object target, Interceptor methodInterceptor)
         {
             this.target = target;
             this.methodInterceptor = methodInterceptor;
         }
 
-        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+        public Object invokeImpl(Object proxy, Method method, Object[] args) throws Throwable
         {
             final ReflectionInvocation invocation = new ReflectionInvocation(target, method, args);
             return methodInterceptor.intercept(invocation);
         }
     }
 
-    private static class InvokerInvocationHandler implements InvocationHandler, Serializable
+    private abstract static class AbstractInvocationHandler implements InvocationHandler, Serializable
+    {
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+        {
+            if (isHashCode(method))
+            {
+                return new Integer(System.identityHashCode(proxy));
+            }
+            else if (isEqualsMethod(method))
+            {
+                return Boolean.valueOf(proxy == args[0]);
+            }
+            else
+            {
+                return invokeImpl(proxy, method, args);
+            }
+        }
+
+        protected abstract Object invokeImpl(Object proxy, Method method, Object[] args) throws Throwable;
+    }
+
+    private static class InvokerInvocationHandler extends AbstractInvocationHandler
     {
         private final Invoker invoker;
 
-        public InvokerInvocationHandler( Invoker invoker )
+        public InvokerInvocationHandler(Invoker invoker)
         {
             this.invoker = invoker;
         }
 
-        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+        public Object invokeImpl(Object proxy, Method method, Object[] args) throws Throwable
         {
             return invoker.invoke(proxy, method, args);
         }
     }
 
+    protected static boolean isHashCode(Method method)
+    {
+        return "hashCode".equals(method.getName()) &&
+                Integer.TYPE.equals(method.getReturnType()) &&
+                method.getParameterTypes().length == 0;
+    }
+
+    protected static boolean isEqualsMethod(Method method)
+    {
+        return "equals".equals(method.getName()) &&
+                Boolean.TYPE.equals(method.getReturnType()) &&
+                method.getParameterTypes().length == 1 &&
+                Object.class.equals(method.getParameterTypes()[0]);
+    }
+
     private static class ReflectionInvocation implements Invocation, Serializable
     {
         private final Method method;
         private final Object[] arguments;
         private final Object target;
 
-        public ReflectionInvocation( Object target, Method method, Object[] arguments )
+        public ReflectionInvocation(Object target, Method method, Object[] arguments)
         {
             this.method = method;
-            this.arguments = ( arguments == null ? ProxyUtils.EMPTY_ARGUMENTS : arguments );
+            this.arguments = (arguments == null ? ProxyUtils.EMPTY_ARGUMENTS : arguments);
             this.target = target;
         }
 
@@ -255,7 +291,7 @@
             {
                 return method.invoke(target, arguments);
             }
-            catch( InvocationTargetException e )
+            catch (InvocationTargetException e)
             {
                 throw e.getTargetException();
             }

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java?rev=631860&r1=631859&r2=631860&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java (original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java Wed Feb 27 21:58:47 2008
@@ -22,6 +22,7 @@
 import net.sf.cglib.proxy.Dispatcher;
 import net.sf.cglib.proxy.Enhancer;
 import net.sf.cglib.proxy.MethodProxy;
+import net.sf.cglib.proxy.MethodInterceptor;
 import org.apache.commons.proxy.Interceptor;
 import org.apache.commons.proxy.Invocation;
 import org.apache.commons.proxy.Invoker;
@@ -30,7 +31,6 @@
 
 import java.io.Serializable;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 
 /**
  * A <a href="http://cglib.sourceforge.net/">CGLIB</a>-based {@link org.apache.commons.proxy.ProxyFactory}
@@ -48,55 +48,46 @@
 // Fields
 //**********************************************************************************************************************
 
-    private static CallbackFilter callbackFilter = new PublicCallbackFilter();
+    private static CallbackFilter callbackFilter = new CglibProxyFactoryCallbackFilter();
 
 //**********************************************************************************************************************
 // Other Methods
 //**********************************************************************************************************************
 
-    public Object createDelegatorProxy( ClassLoader classLoader, ObjectProvider targetProvider,
-                                        Class[] proxyClasses )
+    public Object createDelegatorProxy(ClassLoader classLoader, ObjectProvider targetProvider,
+                                       Class[] proxyClasses)
     {
         final Enhancer enhancer = new Enhancer();
         enhancer.setClassLoader(classLoader);
         enhancer.setInterfaces(toInterfaces(proxyClasses));
         enhancer.setSuperclass(getSuperclass(proxyClasses));
         enhancer.setCallbackFilter(callbackFilter);
-        final ProxyObjectDispatcher proxyObjectDispatcher = new ProxyObjectDispatcher();
-        enhancer.setCallbacks(new Callback[] {new ObjectProviderDispatcher(targetProvider), proxyObjectDispatcher});
-        final Object proxy = enhancer.create();
-        proxyObjectDispatcher.setProxyObject(proxy);
-        return proxy;
+        enhancer.setCallbacks(new Callback[]{new ObjectProviderDispatcher(targetProvider), new EqualsHandler(), new HashCodeHandler()});
+        return enhancer.create();
     }
 
-    public Object createInterceptorProxy( ClassLoader classLoader, Object target, Interceptor interceptor,
-                                          Class[] proxyClasses )
+    public Object createInterceptorProxy(ClassLoader classLoader, Object target, Interceptor interceptor,
+                                         Class[] proxyClasses)
     {
         final Enhancer enhancer = new Enhancer();
         enhancer.setClassLoader(classLoader);
         enhancer.setInterfaces(toInterfaces(proxyClasses));
         enhancer.setSuperclass(getSuperclass(proxyClasses));
         enhancer.setCallbackFilter(callbackFilter);
-        final ProxyObjectDispatcher proxyObjectDispatcher = new ProxyObjectDispatcher();
-        enhancer.setCallbacks(new Callback[] {new InterceptorBridge(target, interceptor), proxyObjectDispatcher});
-        final Object proxy = enhancer.create();
-        proxyObjectDispatcher.setProxyObject(proxy);
-        return proxy;
+        enhancer.setCallbacks(new Callback[]{new InterceptorBridge(target, interceptor), new EqualsHandler(), new HashCodeHandler()});
+        return enhancer.create();
     }
 
-    public Object createInvokerProxy( ClassLoader classLoader, Invoker invoker,
-                                      Class[] proxyClasses )
+    public Object createInvokerProxy(ClassLoader classLoader, Invoker invoker,
+                                     Class[] proxyClasses)
     {
         final Enhancer enhancer = new Enhancer();
         enhancer.setClassLoader(classLoader);
         enhancer.setInterfaces(toInterfaces(proxyClasses));
         enhancer.setSuperclass(getSuperclass(proxyClasses));
         enhancer.setCallbackFilter(callbackFilter);
-        final ProxyObjectDispatcher proxyObjectDispatcher = new ProxyObjectDispatcher();
-        enhancer.setCallbacks(new Callback[] {new InvokerBridge(invoker), proxyObjectDispatcher });
-        final Object proxy = enhancer.create();
-        proxyObjectDispatcher.setProxyObject(proxy);
-        return proxy;
+        enhancer.setCallbacks(new Callback[]{new InvokerBridge(invoker), new EqualsHandler(), new HashCodeHandler()});
+        return enhancer.create();
     }
 
 //**********************************************************************************************************************
@@ -108,28 +99,45 @@
         private final Interceptor inner;
         private final Object target;
 
-        public InterceptorBridge( Object target, Interceptor inner )
+        public InterceptorBridge(Object target, Interceptor inner)
         {
             this.inner = inner;
             this.target = target;
         }
 
-        public Object intercept( Object object, Method method, Object[] args, MethodProxy methodProxy ) throws Throwable
+        public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable
         {
             return inner.intercept(new MethodProxyInvocation(target, method, args, methodProxy));
         }
     }
 
+    private static class HashCodeHandler implements MethodInterceptor, Serializable
+    {
+        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable
+        {
+            return new Integer(System.identityHashCode(o));
+        }
+    }
+
+    private static class EqualsHandler implements MethodInterceptor, Serializable
+    {
+        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable
+        {
+            return Boolean.valueOf(o == objects[0]);
+        }
+    }
+
+
     private static class InvokerBridge implements net.sf.cglib.proxy.InvocationHandler, Serializable
     {
         private final Invoker original;
 
-        public InvokerBridge( Invoker original )
+        public InvokerBridge(Invoker original)
         {
             this.original = original;
         }
 
-        public Object invoke( Object object, Method method, Object[] objects ) throws Throwable
+        public Object invoke(Object object, Method method, Object[] objects) throws Throwable
         {
             return original.invoke(object, method, objects);
         }
@@ -142,7 +150,7 @@
         private final Object[] args;
         private final Object target;
 
-        public MethodProxyInvocation( Object target, Method method, Object[] args, MethodProxy methodProxy )
+        public MethodProxyInvocation(Object target, Method method, Object[] args, MethodProxy methodProxy)
         {
             this.target = target;
             this.method = method;
@@ -175,7 +183,7 @@
     {
         private final ObjectProvider delegateProvider;
 
-        public ObjectProviderDispatcher( ObjectProvider delegateProvider )
+        public ObjectProviderDispatcher(ObjectProvider delegateProvider)
         {
             this.delegateProvider = delegateProvider;
         }
@@ -186,27 +194,25 @@
         }
     }
 
-    private static class ProxyObjectDispatcher implements Dispatcher, Serializable
+    private static class CglibProxyFactoryCallbackFilter implements CallbackFilter
     {
-        private Object proxyObject;
-
-        public void setProxyObject( Object thisObject )
+        public int accept(Method method)
         {
-            this.proxyObject = thisObject;
+            if (isEqualsMethod(method))
+            {
+                return 1;
+            }
+            else if (isHashCode(method))
+            {
+                return 2;
+            }
+            else
+            {
+                return 0;
+            }
         }
 
-        public Object loadObject() throws Exception
-        {
-            return proxyObject;
-        }
-    }
 
-    private static class PublicCallbackFilter implements CallbackFilter
-    {
-        public int accept( Method method )
-        {
-            return Modifier.isPublic(method.getModifiers()) ? 0 : 1;
-        }
     }
 }
 

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java?rev=631860&r1=631859&r2=631860&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java (original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java Wed Feb 27 21:58:47 2008
@@ -59,10 +59,10 @@
 // Static Methods
 //**********************************************************************************************************************
 
-    private static void addGetMethodMethod( CtClass proxyClass ) throws CannotCompileException
+    private static void addGetMethodMethod(CtClass proxyClass) throws CannotCompileException
     {
         final CtMethod method = new CtMethod(JavassistUtils.resolve(Method.class), GET_METHOD_METHOD_NAME,
-                JavassistUtils.resolve(new Class[] {String.class, String.class, Class[].class}), proxyClass);
+                JavassistUtils.resolve(new Class[]{String.class, String.class, Class[].class}), proxyClass);
         final String body = "try { return Class.forName($1).getMethod($2, $3); } catch( Exception e ) " +
                 "{ throw new RuntimeException(\"Unable to look up method.\", e); }";
         method.setBody(body);
@@ -73,48 +73,46 @@
 // Other Methods
 //**********************************************************************************************************************
 
-    public Object createDelegatorProxy( ClassLoader classLoader, ObjectProvider targetProvider,
-                                        Class[] proxyClasses )
+    public Object createDelegatorProxy(ClassLoader classLoader, ObjectProvider targetProvider,
+                                       Class[] proxyClasses)
     {
         try
         {
             final Class clazz = delegatingProxyClassCache.getProxyClass(classLoader, proxyClasses);
-            return clazz.getConstructor(new Class[] {ObjectProvider.class})
-                    .newInstance(new Object[] {targetProvider});
+            return clazz.getConstructor(new Class[]{ObjectProvider.class})
+                    .newInstance(new Object[]{targetProvider});
         }
-        catch( Exception e )
+        catch (Exception e)
         {
             throw new ProxyFactoryException("Unable to instantiate proxy from generated proxy class.", e);
         }
     }
 
-    public Object createInterceptorProxy( ClassLoader classLoader, Object target, Interceptor interceptor,
-                                          Class[] proxyClasses )
+    public Object createInterceptorProxy(ClassLoader classLoader, Object target, Interceptor interceptor,
+                                         Class[] proxyClasses)
     {
         try
         {
             final Class clazz = interceptorProxyClassCache.getProxyClass(classLoader, proxyClasses);
-            final Method[] methods = AbstractProxyClassGenerator.getImplementationMethods(proxyClasses);
-            return clazz.getConstructor(new Class[] {Method[].class, Object.class, Interceptor.class})
-                    .newInstance(new Object[] {methods, target, interceptor});
+            return clazz.getConstructor(new Class[]{Object.class, Interceptor.class})
+                    .newInstance(new Object[]{target, interceptor});
         }
-        catch( Exception e )
+        catch (Exception e)
         {
             throw new ProxyFactoryException("Unable to instantiate proxy class instance.", e);
         }
     }
 
-    public Object createInvokerProxy( ClassLoader classLoader, Invoker invoker,
-                                      Class[] proxyClasses )
+    public Object createInvokerProxy(ClassLoader classLoader, Invoker invoker,
+                                     Class[] proxyClasses)
     {
         try
         {
             final Class clazz = invocationHandlerProxyClassCache.getProxyClass(classLoader, proxyClasses);
-            final Method[] methods = AbstractProxyClassGenerator.getImplementationMethods(proxyClasses);
-            return clazz.getConstructor(new Class[] {Method[].class, Invoker.class})
-                    .newInstance(new Object[] {methods, invoker});
+            return clazz.getConstructor(new Class[]{Invoker.class})
+                    .newInstance(new Object[]{invoker});
         }
-        catch( Exception e )
+        catch (Exception e)
         {
             throw new ProxyFactoryException("Unable to instantiate proxy from generated proxy class.", e);
         }
@@ -126,35 +124,40 @@
 
     private static class DelegatingProxyClassGenerator extends AbstractProxyClassGenerator
     {
-        public Class generateProxyClass( ClassLoader classLoader, Class[] proxyClasses )
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
         {
             try
             {
                 final CtClass proxyClass = JavassistUtils.createClass(getSuperclass(proxyClasses));
                 JavassistUtils.addField(ObjectProvider.class, "provider", proxyClass);
                 final CtConstructor proxyConstructor = new CtConstructor(
-                        JavassistUtils.resolve(new Class[] {ObjectProvider.class}),
+                        JavassistUtils.resolve(new Class[]{ObjectProvider.class}),
                         proxyClass);
                 proxyConstructor.setBody("{ this.provider = $1; }");
                 proxyClass.addConstructor(proxyConstructor);
                 JavassistUtils.addInterfaces(proxyClass, toInterfaces(proxyClasses));
+                addHashCodeMethod(proxyClass);
+                addEqualsMethod(proxyClass);
                 final Method[] methods = getImplementationMethods(proxyClasses);
-                for( int i = 0; i < methods.length; ++i )
+                for (int i = 0; i < methods.length; ++i)
                 {
-                    final Method method = methods[i];
-                    final CtMethod ctMethod = new CtMethod(JavassistUtils.resolve(method.getReturnType()),
-                            method.getName(),
-                            JavassistUtils.resolve(method.getParameterTypes()),
-                            proxyClass);
-                    final String body = "{ return ( $r ) ( ( " + method.getDeclaringClass().getName() +
-                            " )provider.getObject() )." +
-                            method.getName() + "($$); }";
-                    ctMethod.setBody(body);
-                    proxyClass.addMethod(ctMethod);
+                    if (!isEqualsMethod(methods[i]) && !isHashCode(methods[i]))
+                    {
+                        final Method method = methods[i];
+                        final CtMethod ctMethod = new CtMethod(JavassistUtils.resolve(method.getReturnType()),
+                                method.getName(),
+                                JavassistUtils.resolve(method.getParameterTypes()),
+                                proxyClass);
+                        final String body = "{ return ( $r ) ( ( " + method.getDeclaringClass().getName() +
+                                " )provider.getObject() )." +
+                                method.getName() + "($$); }";
+                        ctMethod.setBody(body);
+                        proxyClass.addMethod(ctMethod);
+                    }
                 }
                 return proxyClass.toClass(classLoader);
             }
-            catch( CannotCompileException e )
+            catch (CannotCompileException e)
             {
                 throw new ProxyFactoryException("Could not compile class.", e);
             }
@@ -163,7 +166,7 @@
 
     private static class InterceptorProxyClassGenerator extends AbstractProxyClassGenerator
     {
-        public Class generateProxyClass( ClassLoader classLoader, Class[] proxyClasses )
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
         {
             try
             {
@@ -173,70 +176,103 @@
                 JavassistUtils.addField(Object.class, "target", proxyClass);
                 JavassistUtils.addField(Interceptor.class, "interceptor", proxyClass);
                 addGetMethodMethod(proxyClass);
+                addHashCodeMethod(proxyClass);
+                addEqualsMethod(proxyClass);
                 final CtConstructor proxyConstructor = new CtConstructor(
                         JavassistUtils.resolve(
-                                new Class[] {Method[].class, Object.class, Interceptor.class}),
+                                new Class[]{Object.class, Interceptor.class}),
                         proxyClass);
                 proxyConstructor
                         .setBody(
-                                "{\n\tthis.target = $2;\n\tthis.interceptor = $3; }");
+                                "{\n\tthis.target = $1;\n\tthis.interceptor = $2; }");
                 proxyClass.addConstructor(proxyConstructor);
-                for( int i = 0; i < methods.length; ++i )
+                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 Class invocationClass = JavassistInvocation
-                            .getMethodInvocationClass(classLoader, methods[i]);
-
-                    final String body = "{\n\t return ( $r ) interceptor.intercept( new " + invocationClass.getName() +
-                            "( " + GET_METHOD_METHOD_NAME + "(\"" + methods[i].getDeclaringClass().getName() +
-                            "\", \"" + methods[i].getName() + "\", $sig), target, $args ) );\n }";
-                    method.setBody(body);
-                    proxyClass.addMethod(method);
+                    if (!isEqualsMethod(methods[i]) && !isHashCode(methods[i]))
+                    {
+                        final CtMethod method = new CtMethod(JavassistUtils.resolve(methods[i].getReturnType()),
+                                methods[i].getName(),
+                                JavassistUtils.resolve(methods[i].getParameterTypes()),
+                                proxyClass);
+                        final Class invocationClass = JavassistInvocation
+                                .getMethodInvocationClass(classLoader, methods[i]);
+
+                        final String body = "{\n\t return ( $r ) interceptor.intercept( new " + invocationClass.getName() +
+                                "( " + GET_METHOD_METHOD_NAME + "(\"" + methods[i].getDeclaringClass().getName() +
+                                "\", \"" + methods[i].getName() + "\", $sig), target, $args ) );\n }";
+                        method.setBody(body);
+                        proxyClass.addMethod(method);
+                    }
+
                 }
                 return proxyClass.toClass(classLoader);
             }
-            catch( CannotCompileException e )
+            catch (CannotCompileException e)
             {
                 throw new ProxyFactoryException("Could not compile class.", e);
             }
         }
+
+
+    }
+
+    private static void addEqualsMethod(CtClass proxyClass)
+            throws CannotCompileException
+    {
+        final CtMethod equalsMethod = new CtMethod(JavassistUtils.resolve(Boolean.TYPE), "equals",
+                JavassistUtils.resolve(new Class[]{Object.class}), proxyClass);
+        final String body = "{\n\treturn this == $1;\n}";
+        equalsMethod.setBody(body);
+        proxyClass.addMethod(equalsMethod);
+    }
+
+    private static void addHashCodeMethod(CtClass proxyClass)
+            throws CannotCompileException
+    {
+        final CtMethod hashCodeMethod = new CtMethod(JavassistUtils.resolve(Integer.TYPE), "hashCode",
+                new CtClass[0], proxyClass);
+        hashCodeMethod.setBody("{\n\treturn System.identityHashCode(this);\n}");
+        proxyClass.addMethod(hashCodeMethod);
     }
 
     private static class InvokerProxyClassGenerator extends AbstractProxyClassGenerator
     {
-        public Class generateProxyClass( ClassLoader classLoader, Class[] proxyClasses )
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
         {
             try
             {
                 final CtClass proxyClass = JavassistUtils.createClass(getSuperclass(proxyClasses));
                 final Method[] methods = getImplementationMethods(proxyClasses);
                 JavassistUtils.addInterfaces(proxyClass, toInterfaces(proxyClasses));
-                JavassistUtils.addField(Method[].class, "methods", proxyClass);
                 JavassistUtils.addField(Invoker.class, "invoker", proxyClass);
                 final CtConstructor proxyConstructor = new CtConstructor(
                         JavassistUtils.resolve(
-                                new Class[] {Method[].class, Invoker.class}),
+                                new Class[]{Invoker.class}),
                         proxyClass);
                 proxyConstructor
-                        .setBody("{\n\tthis.methods = $1;\n\tthis.invoker = $2; }");
+                        .setBody("{\n\tthis.invoker = $1; }");
                 proxyClass.addConstructor(proxyConstructor);
-                for( int i = 0; i < methods.length; ++i )
+                addGetMethodMethod(proxyClass);
+                addHashCodeMethod(proxyClass);
+                addEqualsMethod(proxyClass);
+                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 ) invoker.invoke( this, methods[" + i +
-                            "], $args );\n }";
-                    method.setBody(body);
-                    proxyClass.addMethod(method);
+                    if (!isEqualsMethod(methods[i]) && !isHashCode(methods[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 ) invoker.invoke( this, " + GET_METHOD_METHOD_NAME + "(\"" +
+                                methods[i].getDeclaringClass().getName() +
+                                "\", \"" + methods[i].getName() + "\", $sig), $args );\n }";
+                        method.setBody(body);
+                        proxyClass.addMethod(method);
+                    }
                 }
                 return proxyClass.toClass(classLoader);
             }
-            catch( CannotCompileException e )
+            catch (CannotCompileException e)
             {
                 throw new ProxyFactoryException("Could not compile class.", e);
             }

Modified: commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java?rev=631860&r1=631859&r2=631860&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java (original)
+++ commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java Wed Feb 27 21:58:47 2008
@@ -39,6 +39,7 @@
 import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
+import java.util.Date;
 
 /**
  * @author James Carman
@@ -50,14 +51,15 @@
 // Fields
 //**********************************************************************************************************************
 
-    private static final Class[] ECHO_ONLY = new Class[] {Echo.class};
+    private static final Class[] ECHO_ONLY = new Class[]{Echo.class};
     protected final ProxyFactory factory;
+    private static final Class[] COMPARABLE_ONLY = new Class[] { Comparable.class };
 
 //**********************************************************************************************************************
 // Constructors
 //**********************************************************************************************************************
 
-    protected AbstractProxyFactoryTestCase( ProxyFactory factory )
+    protected AbstractProxyFactoryTestCase(ProxyFactory factory)
     {
         this.factory = factory;
     }
@@ -71,9 +73,61 @@
         return new SingletonProvider(new BeanProvider(EchoImpl.class));
     }
 
+    public void testInterceptorHashCode()
+    {
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        assertEquals(proxy.hashCode(), System.identityHashCode(proxy));
+    }
+
+    public void testInvokerHashCode() throws Exception
+    {
+        final Echo proxy = (Echo) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
+        assertEquals(proxy.hashCode(), System.identityHashCode(proxy));
+    }
+
+    public void testDelegatorHashCode() throws Exception
+    {
+        final Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        assertEquals(proxy.hashCode(), System.identityHashCode(proxy));
+    }
+
+
+    public void testInterceptorEquals()
+    {
+        final Date date = new Date();
+        final Comparable proxy1 = (Comparable) factory.createInterceptorProxy(date,
+                new NoOpMethodInterceptor(), COMPARABLE_ONLY);
+        final Comparable proxy2 = (Comparable) factory.createInterceptorProxy(date,
+                new NoOpMethodInterceptor(), COMPARABLE_ONLY);
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
+    public void testInvokerEquals() throws Exception
+    {
+        final Comparable proxy1 = (Comparable) factory.createInvokerProxy(new InvokerTester(), COMPARABLE_ONLY);
+        final Comparable proxy2 = (Comparable) factory.createInvokerProxy(new InvokerTester(), COMPARABLE_ONLY);
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
+    public void testDelegatorEquals() throws Exception
+    {
+        final Date date = new Date();
+        final Comparable proxy1 = (Comparable) factory.createDelegatorProxy(new ConstantProvider(date),
+                COMPARABLE_ONLY);
+        final Comparable proxy2 = (Comparable) factory.createDelegatorProxy(new ConstantProvider(date),
+                COMPARABLE_ONLY);
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
     public void testBooleanInterceptorParameter()
     {
-        final Echo echo = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new InterceptorTester(), ECHO_ONLY);
+        final Echo echo = (Echo) factory.createInterceptorProxy(new EchoImpl(), new InterceptorTester(), ECHO_ONLY);
         assertFalse(echo.echoBack(false));
         assertTrue(echo.echoBack(true));
     }
@@ -81,18 +135,18 @@
     public void testCanProxy()
     {
         assertTrue(factory.canProxy(ECHO_ONLY));
-        assertFalse(factory.canProxy(new Class[] {EchoImpl.class}));
+        assertFalse(factory.canProxy(new Class[]{EchoImpl.class}));
     }
 
     public void testChangingArguments()
     {
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new ChangeArgumentInterceptor(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new ChangeArgumentInterceptor(), ECHO_ONLY);
         assertEquals("something different", proxy.echoBack("whatever"));
     }
 
     public void testCreateDelegatingProxy()
     {
-        final Echo echo = ( Echo ) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
+        final Echo echo = (Echo) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
         echo.echo();
         assertEquals("message", echo.echoBack("message"));
         assertEquals("ab", echo.echoBack("a", "b"));
@@ -100,24 +154,24 @@
 
     public void testCreateInterceptorProxy()
     {
-        final Echo target = ( Echo ) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(target, new SuffixInterceptor(" suffix"), ECHO_ONLY);
+        final Echo target = (Echo) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(target, new SuffixInterceptor(" suffix"), ECHO_ONLY);
         proxy.echo();
         assertEquals("message suffix", proxy.echoBack("message"));
     }
 
     public void testDelegatingProxyClassCaching() throws Exception
     {
-        final Echo proxy1 = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
-        final Echo proxy2 = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        final Echo proxy1 = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        final Echo proxy2 = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
         assertNotSame(proxy1, proxy2);
         assertSame(proxy1.getClass(), proxy2.getClass());
     }
 
     public void testDelegatingProxyInterfaceOrder()
     {
-        final Echo echo = ( Echo ) factory.createDelegatorProxy(createSingletonEcho(), new Class[] {Echo.class, DuplicateEcho.class});
-        final List expected = new LinkedList(Arrays.asList(new Class[] {Echo.class, DuplicateEcho.class}));
+        final Echo echo = (Echo) factory.createDelegatorProxy(createSingletonEcho(), new Class[]{Echo.class, DuplicateEcho.class});
+        final List expected = new LinkedList(Arrays.asList(new Class[]{Echo.class, DuplicateEcho.class}));
         final List actual = new LinkedList(Arrays.asList(echo.getClass().getInterfaces()));
         actual.retainAll(expected);  // Doesn't alter order!
         assertEquals(expected, actual);
@@ -125,62 +179,62 @@
 
     public void testDelegatingProxySerializable() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
         assertSerializable(proxy);
     }
 
     public void testInterceptingProxyClassCaching() throws Exception
     {
-        final Echo proxy1 = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
-        final Echo proxy2 = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy1 = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy2 = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
         assertNotSame(proxy1, proxy2);
         assertSame(proxy1.getClass(), proxy2.getClass());
     }
 
     public void testInterceptingProxySerializable() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
         assertSerializable(proxy);
     }
 
     public void testInterceptorProxyWithCheckedException() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
         try
         {
             proxy.ioException();
             fail();
         }
-        catch( IOException e )
+        catch (IOException e)
         {
         }
     }
 
     public void testInterceptorProxyWithUncheckedException() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), ECHO_ONLY);
         try
         {
             proxy.illegalArgument();
             fail();
         }
-        catch( IllegalArgumentException e )
+        catch (IllegalArgumentException e)
         {
         }
     }
 
     public void testInterfaceHierarchies()
     {
-        final SortedSet set = ( SortedSet ) factory.createDelegatorProxy(new ConstantProvider(new TreeSet()), new Class[] {SortedSet.class});
+        final SortedSet set = (SortedSet) factory.createDelegatorProxy(new ConstantProvider(new TreeSet()), new Class[]{SortedSet.class});
         set.add("Hello");
     }
 
     public void testInvokerProxy() throws Exception
     {
         final InvokerTester tester = new InvokerTester();
-        final Echo echo = ( Echo ) factory.createInvokerProxy(tester, ECHO_ONLY);
+        final Echo echo = (Echo) factory.createInvokerProxy(tester, ECHO_ONLY);
         echo.echoBack("hello");
-        assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class}), tester.method);
+        assertEquals(Echo.class.getMethod("echoBack", new Class[]{String.class}), tester.method);
         assertSame(echo, tester.proxy);
         assertNotNull(tester.args);
         assertEquals(1, tester.args.length);
@@ -189,24 +243,24 @@
 
     public void testInvokerProxyClassCaching() throws Exception
     {
-        final Echo proxy1 = ( Echo ) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
-        final Echo proxy2 = ( Echo ) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
+        final Echo proxy1 = (Echo) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
+        final Echo proxy2 = (Echo) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
         assertNotSame(proxy1, proxy2);
         assertSame(proxy1.getClass(), proxy2.getClass());
     }
 
     public void testInvokerProxySerializable() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
-        assertTrue(proxy instanceof Serializable);
+        final Echo proxy = (Echo) factory.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
+        assertSerializable(proxy);
     }
 
     public void testMethodInvocationClassCaching() throws Exception
     {
         final InterceptorTester tester = new InterceptorTester();
         final EchoImpl target = new EchoImpl();
-        final Echo proxy1 = ( Echo ) factory.createInterceptorProxy(target, tester, ECHO_ONLY);
-        final Echo proxy2 = ( Echo ) factory.createInterceptorProxy(target, tester, new Class[] {Echo.class, DuplicateEcho.class});
+        final Echo proxy1 = (Echo) factory.createInterceptorProxy(target, tester, ECHO_ONLY);
+        final Echo proxy2 = (Echo) factory.createInterceptorProxy(target, tester, new Class[]{Echo.class, DuplicateEcho.class});
         proxy1.echoBack("hello1");
         final Class invocationClass1 = tester.invocationClass;
         proxy2.echoBack("hello2");
@@ -217,69 +271,69 @@
     {
         final InterceptorTester tester = new InterceptorTester();
         final EchoImpl target = new EchoImpl();
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(target, tester, new Class[] {Echo.class, DuplicateEcho.class});
+        final Echo proxy = (Echo) factory.createInterceptorProxy(target, tester, new Class[]{Echo.class, DuplicateEcho.class});
         proxy.echoBack("hello");
-        assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class}), tester.method);
+        assertEquals(Echo.class.getMethod("echoBack", new Class[]{String.class}), tester.method);
     }
 
     public void testMethodInvocationImplementation() throws Exception
     {
         final InterceptorTester tester = new InterceptorTester();
         final EchoImpl target = new EchoImpl();
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(target, tester, ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(target, tester, ECHO_ONLY);
         proxy.echo();
         assertNotNull(tester.arguments);
         assertEquals(0, tester.arguments.length);
-        assertEquals(Echo.class.getMethod("echo", new Class[] {}), tester.method);
+        assertEquals(Echo.class.getMethod("echo", new Class[]{}), tester.method);
         assertEquals(target, tester.proxy);
         proxy.echoBack("Hello");
         assertNotNull(tester.arguments);
         assertEquals(1, tester.arguments.length);
         assertEquals("Hello", tester.arguments[0]);
-        assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class}), tester.method);
+        assertEquals(Echo.class.getMethod("echoBack", new Class[]{String.class}), tester.method);
         proxy.echoBack("Hello", "World");
         assertNotNull(tester.arguments);
         assertEquals(2, tester.arguments.length);
         assertEquals("Hello", tester.arguments[0]);
         assertEquals("World", tester.arguments[1]);
-        assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class, String.class}), tester.method);
+        assertEquals(Echo.class.getMethod("echoBack", new Class[]{String.class, String.class}), tester.method);
     }
 
     public void testPrimitiveParameter()
     {
-        final Echo echo = ( Echo ) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
+        final Echo echo = (Echo) factory.createDelegatorProxy(createSingletonEcho(), ECHO_ONLY);
         assertEquals(1, echo.echoBack(1));
     }
 
     public void testProxyWithCheckedException() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
         try
         {
             proxy.ioException();
             fail();
         }
-        catch( IOException e )
+        catch (IOException e)
         {
         }
     }
 
     public void testProxyWithUncheckedException() throws Exception
     {
-        final Echo proxy = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), ECHO_ONLY);
         try
         {
             proxy.illegalArgument();
             fail();
         }
-        catch( IllegalArgumentException e )
+        catch (IllegalArgumentException e)
         {
         }
     }
 
     public void testWithNonAccessibleTargetType()
     {
-        final Echo proxy = ( Echo ) factory.createInterceptorProxy(new PrivateEcho(), new NoOpMethodInterceptor(), ECHO_ONLY);
+        final Echo proxy = (Echo) factory.createInterceptorProxy(new PrivateEcho(), new NoOpMethodInterceptor(), ECHO_ONLY);
         proxy.echo();
     }
 
@@ -289,21 +343,21 @@
 
     private static class ChangeArgumentInterceptor implements Interceptor
     {
-        public Object intercept( Invocation methodInvocation ) throws Throwable
+        public Object intercept(Invocation methodInvocation) throws Throwable
         {
             methodInvocation.getArguments()[0] = "something different";
             return methodInvocation.proceed();
         }
     }
 
-    private static class InterceptorTester implements Interceptor
+    protected static class InterceptorTester implements Interceptor
     {
         private Object[] arguments;
         private Method method;
         private Object proxy;
         private Class invocationClass;
 
-        public Object intercept( Invocation methodInvocation ) throws Throwable
+        public Object intercept(Invocation methodInvocation) throws Throwable
         {
             arguments = methodInvocation.getArguments();
             method = methodInvocation.getMethod();
@@ -313,13 +367,13 @@
         }
     }
 
-    private static class InvokerTester implements Invoker
+    protected static class InvokerTester implements Invoker
     {
         private Object method;
         private Object[] args;
         private Object proxy;
 
-        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
         {
             this.proxy = proxy;
             this.method = method;
@@ -330,7 +384,7 @@
 
     protected static class NoOpMethodInterceptor implements Interceptor, Serializable
     {
-        public Object intercept( Invocation methodInvocation ) throws Throwable
+        public Object intercept(Invocation methodInvocation) throws Throwable
         {
             return methodInvocation.proceed();
         }

Modified: commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java?rev=631860&r1=631859&r2=631860&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java (original)
+++ commons/proper/proxy/trunk/src/test/java/org/apache/commons/proxy/factory/AbstractSubclassingProxyFactoryTestCase.java Wed Feb 27 21:58:47 2008
@@ -25,17 +25,20 @@
 import org.apache.commons.proxy.util.Echo;
 import org.apache.commons.proxy.util.EchoImpl;
 
+import java.util.Date;
+
 /**
  * @author James Carman
  * @since 1.0
  */
 public abstract class AbstractSubclassingProxyFactoryTestCase extends AbstractProxyFactoryTestCase
 {
+    private static final Class[] DATE_ONLY = new Class[]{Date.class};
 //**********************************************************************************************************************
 // Constructors
 //**********************************************************************************************************************
 
-    protected AbstractSubclassingProxyFactoryTestCase( ProxyFactory factory )
+    protected AbstractSubclassingProxyFactoryTestCase(ProxyFactory factory)
     {
         super(factory);
     }
@@ -46,14 +49,14 @@
 
     public void testCanProxy()
     {
-        assertTrue(factory.canProxy(new Class[] {Echo.class}));
-        assertTrue(factory.canProxy(new Class[] {EchoImpl.class}));
-        assertFalse(factory.canProxy(new Class[] {FinalEcho.class}));
-        assertTrue(factory.canProxy(new Class[] {FinalMethodEcho.class, Echo.class}));
-        assertFalse(factory.canProxy(new Class[] {NoDefaultConstructorEcho.class}));
-        assertTrue(factory.canProxy(new Class[] {ProtectedConstructorEcho.class}));
-        assertFalse(factory.canProxy(new Class[] {InvisibleEcho.class}));
-        assertFalse(factory.canProxy(new Class[] {Echo.class, EchoImpl.class, String.class}));
+        assertTrue(factory.canProxy(new Class[]{Echo.class}));
+        assertTrue(factory.canProxy(new Class[]{EchoImpl.class}));
+        assertFalse(factory.canProxy(new Class[]{FinalEcho.class}));
+        assertTrue(factory.canProxy(new Class[]{FinalMethodEcho.class, Echo.class}));
+        assertFalse(factory.canProxy(new Class[]{NoDefaultConstructorEcho.class}));
+        assertTrue(factory.canProxy(new Class[]{ProtectedConstructorEcho.class}));
+        assertFalse(factory.canProxy(new Class[]{InvisibleEcho.class}));
+        assertFalse(factory.canProxy(new Class[]{Echo.class, EchoImpl.class, String.class}));
     }
 
     public void testDelegatorWithMultipleSuperclasses()
@@ -61,18 +64,18 @@
         try
         {
             factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()),
-                    new Class[] {EchoImpl.class, String.class});
+                    new Class[]{EchoImpl.class, String.class});
             fail();
         }
-        catch( ProxyFactoryException e )
+        catch (ProxyFactoryException e)
         {
         }
     }
 
     public void testDelegatorWithSuperclass()
     {
-        final Echo echo = ( Echo ) factory
-                .createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[] {Echo.class, EchoImpl.class});
+        final Echo echo = (Echo) factory
+                .createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[]{Echo.class, EchoImpl.class});
         assertTrue(echo instanceof EchoImpl);
     }
 
@@ -81,18 +84,18 @@
         try
         {
             factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(),
-                    new Class[] {EchoImpl.class, String.class});
+                    new Class[]{EchoImpl.class, String.class});
             fail();
         }
-        catch( ProxyFactoryException e )
+        catch (ProxyFactoryException e)
         {
         }
     }
 
     public void testInterceptorWithSuperclass()
     {
-        final Echo echo = ( Echo ) factory
-                .createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), new Class[] {Echo.class, EchoImpl.class});
+        final Echo echo = (Echo) factory
+                .createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), new Class[]{Echo.class, EchoImpl.class});
         assertTrue(echo instanceof EchoImpl);
     }
 
@@ -101,41 +104,74 @@
         try
         {
             factory.createInvokerProxy(new NullInvoker(),
-                    new Class[] {EchoImpl.class, String.class});
+                    new Class[]{EchoImpl.class, String.class});
             fail();
         }
-        catch( ProxyFactoryException e )
+        catch (ProxyFactoryException e)
         {
         }
     }
 
     public void testInvokerWithSuperclass()
     {
-        final Echo echo = ( Echo ) factory
-                .createInvokerProxy(new NullInvoker(), new Class[] {Echo.class, EchoImpl.class});
+        final Echo echo = (Echo) factory
+                .createInvokerProxy(new NullInvoker(), new Class[]{Echo.class, EchoImpl.class});
         assertTrue(echo instanceof EchoImpl);
     }
 
     public void testProxiesWithClashingFinalMethodInSuperclass()
     {
-        final Class[] proxyClasses = new Class[] {Echo.class, FinalMethodEcho.class};
-        Echo proxy = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), proxyClasses);
+        final Class[] proxyClasses = new Class[]{Echo.class, FinalMethodEcho.class};
+        Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), proxyClasses);
         assertEquals("final", proxy.echoBack("echo"));
 
-        proxy = ( Echo ) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses);
+        proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses);
         assertEquals("final", proxy.echoBack("echo"));
 
-        proxy = ( Echo ) factory.createInvokerProxy(new NullInvoker(), proxyClasses);
+        proxy = (Echo) factory.createInvokerProxy(new NullInvoker(), proxyClasses);
         assertEquals("final", proxy.echoBack("echo"));
     }
 
     public void testWithAbstractSuperclass()
     {
-        final Echo echo = ( Echo ) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[] {AbstractEcho.class});
+        final Echo echo = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[]{AbstractEcho.class});
         assertEquals("hello", echo.echoBack("hello"));
         assertEquals("helloworld", echo.echoBack("hello", "world"));
     }
 
+    public void testInterceptorEquals()
+    {
+        final EqualsEcho echo = new EqualsEcho("text");
+        final Echo proxy1 = (Echo) factory.createInterceptorProxy(echo,
+                new NoOpMethodInterceptor(), new Class[] { EqualsEcho.class } );
+        final Echo proxy2 = (Echo) factory.createInterceptorProxy(echo,
+                new NoOpMethodInterceptor(), new Class[] { EqualsEcho.class } );
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
+    public void testInvokerEquals() throws Exception
+    {
+        final Date proxy1 = (Date) factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
+        final Date proxy2 = (Date) factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
+    public void testDelegatorEquals() throws Exception
+    {
+        final EqualsEcho echo = new EqualsEcho("text");
+        final Echo proxy1 = (Echo) factory.createDelegatorProxy(new ConstantProvider(echo),
+                new Class[] { EqualsEcho.class });
+        final Echo proxy2 = (Echo) factory.createDelegatorProxy(new ConstantProvider(echo),
+                new Class[] { EqualsEcho.class });
+        assertEquals(proxy1, proxy1);
+        assertFalse(proxy1.equals(proxy2));
+        assertFalse(proxy2.equals(proxy1));
+    }
+
 //**********************************************************************************************************************
 // Inner Classes
 //**********************************************************************************************************************
@@ -146,19 +182,34 @@
 
     public static class FinalMethodEcho extends EchoImpl
     {
-        public final String echoBack( String message )
+        public final String echoBack(String message)
         {
             return "final";
         }
     }
 
+    public static class EqualsEcho extends EchoImpl
+    {
+        private final String text;
+
+        public EqualsEcho()
+        {
+            this("testing");
+        }
+
+        public EqualsEcho(String text)
+        {
+            this.text = text;
+        }
+    }
+    
     private static class InvisibleEcho extends EchoImpl
     {
     }
 
     public static class NoDefaultConstructorEcho extends EchoImpl
     {
-        public NoDefaultConstructorEcho( String param )
+        public NoDefaultConstructorEcho(String param)
         {
         }
     }



Mime
View raw message