commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r382409 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/ java/org/apache/commons/proxy/factory/javassist/ test/org/apache/commons/proxy/factory/ test/org/apache/commons/proxy/util/
Date Thu, 02 Mar 2006 15:25:44 GMT
Author: jcarman
Date: Thu Mar  2 07:25:41 2006
New Revision: 382409

URL: http://svn.apache.org/viewcvs?rev=382409&view=rev
Log:
Bug fix: error when intercepting methods with primitive parameters.

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/javassist/JavassistInvocation.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.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=382409&r1=382408&r2=382409&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 Mar  2 07:25:41 2006
@@ -20,6 +20,8 @@
 
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 /**
  * Provides some helpful proxy utility methods.
@@ -123,6 +125,24 @@
             return getJavaClassName( clazz.getComponentType() ) + "[]";
         }
         return clazz.getName();
+    }
+
+    private static final Map wrapperClassMap = new HashMap();
+    static
+    {
+        wrapperClassMap.put( Integer.TYPE, Integer.class );
+        wrapperClassMap.put( Character.TYPE, Character.class );
+        wrapperClassMap.put( Boolean.TYPE, Boolean.class );
+        wrapperClassMap.put( Short.TYPE, Short.class );
+        wrapperClassMap.put( Long.TYPE, Long.class );
+        wrapperClassMap.put( Float.TYPE, Float.class );
+        wrapperClassMap.put( Double.TYPE, Double.class );
+        wrapperClassMap.put( Byte.TYPE, Byte.class );
+    }
+
+    public static Class getWrapperClass( Class primitiveType )
+    {
+        return ( Class )wrapperClassMap.get( primitiveType );
     }
 }
 

Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistInvocation.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistInvocation.java?rev=382409&r1=382408&r2=382409&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistInvocation.java
(original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistInvocation.java
Thu Mar  2 07:25:41 2006
@@ -38,7 +38,6 @@
 //----------------------------------------------------------------------------------------------------------------------
 // Fields
 //----------------------------------------------------------------------------------------------------------------------
-
     private static WeakHashMap loaderToClassCache = new WeakHashMap();
     protected final Method method;
     protected final Object target;
@@ -48,6 +47,18 @@
 // Static Methods
 //----------------------------------------------------------------------------------------------------------------------
 
+    private static String createCastExpression( Class type, String objectToCast )
+    {
+        if( !type.isPrimitive() )
+        {
+            return "( " + ProxyUtils.getJavaClassName( type ) + " )" + objectToCast;
+        }
+        else
+        {
+            return "( ( " + ProxyUtils.getWrapperClass( type ).getName() + " )" + objectToCast
+ " )." + type.getName() + "Value()";
+        }
+    }
+
     private static Class createInvocationClass( ClassLoader classLoader, Method interfaceMethod
)
             throws CannotCompileException
     {
@@ -57,7 +68,8 @@
                 "_invocation",
                 JavassistInvocation.class );
         final CtConstructor constructor = new CtConstructor(
-                JavassistUtils.resolve( new Class[]{Method.class, Object.class, Object[].class}
), ctClass );
+                JavassistUtils.resolve( new Class[]{ Method.class, Object.class, Object[].class
} ),
+                ctClass );
         constructor.setBody( "{\n\tsuper($$);\n}" );
         ctClass.addConstructor( constructor );
         final CtMethod proceedMethod = new CtMethod( JavassistUtils.resolve( Object.class
), "proceed",
@@ -67,6 +79,12 @@
         if( !Void.TYPE.equals( interfaceMethod.getReturnType() ) )
         {
             proceedBody.append( "\treturn " );
+            if( interfaceMethod.getReturnType().isPrimitive() )
+            {
+                proceedBody.append( "new " );
+                proceedBody.append( ProxyUtils.getWrapperClass( interfaceMethod.getReturnType()
).getName() );
+                proceedBody.append( "( " );
+            }
         }
         else
         {
@@ -79,23 +97,28 @@
         proceedBody.append( "(" );
         for( int i = 0; i < argumentTypes.length; ++i )
         {
-            proceedBody.append( "(" );
-            proceedBody.append( ProxyUtils.getJavaClassName( argumentTypes[i] ) );
-            proceedBody.append( ")arguments[" );
-            proceedBody.append( i );
-            proceedBody.append( "]" );
+            final Class argumentType = argumentTypes[i];
+            proceedBody.append( createCastExpression( argumentType, "arguments[" + i + "]"
) );
             if( i != argumentTypes.length - 1 )
             {
                 proceedBody.append( ", " );
             }
         }
-        proceedBody.append( ");\n" );
+        if( !Void.TYPE.equals( interfaceMethod.getReturnType() ) && interfaceMethod.getReturnType().isPrimitive()
)
+        {
+            proceedBody.append( ") );\n" );
+        }
+        else
+        {
+            proceedBody.append( ");\n" );
+        }
         if( Void.TYPE.equals( interfaceMethod.getReturnType() ) )
         {
             proceedBody.append( "\treturn null;\n" );
         }
         proceedBody.append( "}" );
-        proceedMethod.setBody( proceedBody.toString() );
+        final String body = proceedBody.toString();
+        proceedMethod.setBody( body );
         ctClass.addMethod( proceedMethod );
         invocationClass = ctClass.toClass( classLoader );
         return invocationClass;
@@ -103,7 +126,7 @@
 
     private static Map getClassCache( ClassLoader classLoader )
     {
-        Map cache = ( Map )loaderToClassCache.get( classLoader );
+        Map cache = ( Map ) loaderToClassCache.get( classLoader );
         if( cache == null )
         {
             cache = new HashMap();
@@ -112,12 +135,13 @@
         return cache;
     }
 
-    public synchronized static Class getMethodInvocationClass( ClassLoader classLoader, Method
interfaceMethod )
+    public synchronized static Class getMethodInvocationClass( ClassLoader classLoader,
+                                                               Method interfaceMethod )
             throws CannotCompileException
     {
         final Map classCache = getClassCache( classLoader );
         final String key = toClassCacheKey( interfaceMethod );
-        final WeakReference invocationClassRef = ( WeakReference )classCache.get( key );
+        final WeakReference invocationClassRef = ( WeakReference ) classCache.get( key );
         Class invocationClass;
         if( invocationClassRef == null )
         {
@@ -128,7 +152,7 @@
         {
             synchronized( invocationClassRef )
             {
-                invocationClass = ( Class )invocationClassRef.get();
+                invocationClass = ( Class ) invocationClassRef.get();
                 if( invocationClass == null )
                 {
                     invocationClass = createInvocationClass( classLoader, interfaceMethod
);

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=382409&r1=382408&r2=382409&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
Thu Mar  2 07:25:41 2006
@@ -33,12 +33,9 @@
 /**
  * A <a href="http://www.jboss.org/products/javassist">Javassist</a>-based {@link
org.apache.commons.proxy.ProxyFactory}
  * implementation.
- * <p>
- *   <b>Dependencies</b>:
- * <ul>
- *   <li>Javassist version 3.0 or greater</li>
- * </ul>
- * </p>
+ * <p/>
+ * <b>Dependencies</b>: <ul> <li>Javassist version 3.0 or greater</li>
</ul> </p>
+ *
  * @author James Carman
  * @since 1.0
  */
@@ -90,7 +87,7 @@
     }
 
     public Object createInvokerProxy( ClassLoader classLoader, Invoker invoker,
-                                                Class[] proxyClasses )
+                                      Class[] proxyClasses )
     {
         try
         {
@@ -164,7 +161,8 @@
                                 new Class[]{ Method[].class, Object.class, Interceptor.class
} ),
                         proxyClass );
                 proxyConstructor
-                        .setBody( "{\n\tthis.methods = $1;\n\tthis.target = $2;\n\tthis.interceptor
= $3; }" );
+                        .setBody(
+                                "{\n\tthis.methods = $1;\n\tthis.target = $2;\n\tthis.interceptor
= $3; }" );
                 proxyClass.addConstructor( proxyConstructor );
                 for( int i = 0; i < methods.length; ++i )
                 {
@@ -178,6 +176,7 @@
                                         "( methods[" + i + "], target, $args ) );\n }";
                     method.setBody( body );
                     proxyClass.addMethod( method );
+
                 }
                 return proxyClass.toClass( classLoader );
             }
@@ -210,9 +209,10 @@
                                                             method.getName(),
                                                             JavassistUtils.resolve( method.getParameterTypes()
),
                                                             proxyClass );
-                    ctMethod.setBody( "{ return ( $r ) ( ( " + method.getDeclaringClass().getName()
+
-                                      " )provider.getObject() )." +
-                                      method.getName() + "($$); }" );
+                    final String body = "{ return ( $r ) ( ( " + method.getDeclaringClass().getName()
+
+                                        " )provider.getObject() )." +
+                                        method.getName() + "($$); }";
+                    ctMethod.setBody( body );
                     proxyClass.addMethod( ctMethod );
 
                 }

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=382409&r1=382408&r2=382409&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
Thu Mar  2 07:25:41 2006
@@ -90,6 +90,13 @@
         assertEquals( "ab", echo.echoBack( "a", "b" ) );
     }
 
+    public void testBooleanInterceptorParameter()
+    {
+        final Echo echo = ( Echo ) factory.createInterceptorProxy( new EchoImpl(), new  InterceptorTester(),
ECHO_ONLY );
+        assertFalse( echo.echoBack( false ) );
+        assertTrue( echo.echoBack( true ) );
+
+    }
     public void testPrimitiveParameter()
     {
         final Echo echo = ( Echo ) factory.createDelegatorProxy( createSingletonEcho(), ECHO_ONLY
);
@@ -278,10 +285,10 @@
 
         public Object intercept( Invocation methodInvocation ) throws Throwable
         {
-            this.arguments = methodInvocation.getArguments();
-            this.method = methodInvocation.getMethod();
-            this.proxy = methodInvocation.getProxy();
-            this.invocationClass = methodInvocation.getClass();
+            arguments = methodInvocation.getArguments();
+            method = methodInvocation.getMethod();
+            proxy = methodInvocation.getProxy();
+            invocationClass = methodInvocation.getClass();
             return methodInvocation.proceed();
         }
     }

Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java?rev=382409&r1=382408&r2=382409&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java Thu
Mar  2 07:25:41 2006
@@ -27,6 +27,7 @@
     public String echoBack( String message1, String message2 );
     public String echoBack( String[] messages );
     public int echoBack( int i );
+    public boolean echoBack( boolean b );
     public void echo();
 
     public void ioException() throws IOException;

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=382409&r1=382408&r2=382409&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 Mar  2 07:25:41 2006
@@ -16,14 +16,19 @@
 package org.apache.commons.proxy.util;
 
 import java.io.IOException;
-import java.io.Serializable;
 
 /**
  * @author James Carman
  * @since 1.0
  */
-public class EchoImpl extends AbstractEcho implements DuplicateEcho, Serializable
+public class EchoImpl extends AbstractEcho implements DuplicateEcho
 {
+    private static final long serialVersionUID = -4844873352607521103L;
+
+    public boolean echoBack( boolean b )
+    {
+        return b;
+    }
 
     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


Mime
View raw message