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/08/31 22:22:54 UTC
svn commit: r265573 - in /jakarta/commons/sandbox/proxy/trunk/src:
java/org/apache/commons/proxy/factory/
java/org/apache/commons/proxy/factory/javassist/
test/org/apache/commons/proxy/factory/
Author: jcarman
Date: Wed Aug 31 13:22:49 2005
New Revision: 265573
URL: http://svn.apache.org/viewcvs?rev=265573&view=rev
Log:
Improving test coverage.
Added:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java (with props)
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java (with props)
Modified:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistMethodInvocation.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/AbstractProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java?rev=265573&r1=265572&r2=265573&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java Wed Aug 31 13:22:49 2005
@@ -23,11 +23,10 @@
import org.apache.commons.proxy.ProxyFactory;
import java.lang.reflect.Method;
-import java.util.Set;
import java.util.HashSet;
-import java.util.List;
import java.util.LinkedList;
-import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
/**
* A helpful superclass for {@link org.apache.commons.proxy.ProxyFactory} implementations.
@@ -80,48 +79,5 @@
}
final Method[] results = new Method[resultingMethods.size()];
return resultingMethods.toArray( results );
- }
-
- private static class MethodSignature
- {
- private final String name;
- private final List<Class> parameterTypes;
-
- public MethodSignature( Method method )
- {
- this.name = method.getName();
- this.parameterTypes = Arrays.<Class>asList( method.getParameterTypes() );
- }
-
- public boolean equals( Object o )
- {
- if( this == o )
- {
- return true;
- }
- if( o == null || getClass() != o.getClass() )
- {
- return false;
- }
- final MethodSignature that = ( MethodSignature ) o;
- if( name != null ? !name.equals( that.name ) : that.name != null )
- {
- return false;
- }
- if( parameterTypes != null ? !parameterTypes.equals( that.parameterTypes ) :
- that.parameterTypes != null )
- {
- return false;
- }
- return true;
- }
-
- public int hashCode()
- {
- int result;
- result = ( name != null ? name.hashCode() : 0 );
- result = 29 * result + ( parameterTypes != null ? parameterTypes.hashCode() : 0 );
- return result;
- }
}
}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java?rev=265573&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java Wed Aug 31 13:22:49 2005
@@ -0,0 +1,67 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.proxy.factory;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class MethodSignature
+{
+ private final String name;
+ private final List<Class> parameterTypes;
+
+ public MethodSignature( Method method )
+ {
+ this.name = method.getName();
+ this.parameterTypes = Arrays.<Class>asList( method.getParameterTypes() );
+ }
+
+ public boolean equals( Object o )
+ {
+ if( this == o )
+ {
+ return true;
+ }
+ if( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+ final MethodSignature that = ( MethodSignature ) o;
+ if( !name.equals( that.name ) )
+ {
+ return false;
+ }
+ if( !parameterTypes.equals( that.parameterTypes ) )
+ {
+ return false;
+ }
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int result;
+ result = name.hashCode();
+ result = 29 * result + parameterTypes.hashCode();
+ return result;
+ }
+}
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/MethodSignature.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistMethodInvocation.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistMethodInvocation.java?rev=265573&r1=265572&r2=265573&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistMethodInvocation.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistMethodInvocation.java Wed Aug 31 13:22:49 2005
@@ -21,15 +21,11 @@
import javassist.CtConstructor;
import javassist.CtMethod;
import org.aopalliance.intercept.MethodInvocation;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-import java.util.WeakHashMap;
import java.util.HashMap;
/**
@@ -39,8 +35,8 @@
public abstract class JavassistMethodInvocation implements MethodInvocation
{
// TODO: Make sure this doesn't cause memory leaks in application servers!
- private static final HashMap invocationClassCache = new HashMap();
-
+ private static final HashMap<CacheKey,Class> invocationClassCache = new HashMap<CacheKey,Class>();
+ private static final Log log = LogFactory.getLog( JavassistMethodInvocation.class );
protected final Method method;
protected final Object target;
protected final Object[] arguments;
@@ -81,6 +77,7 @@
Class invocationClass = ( Class ) invocationClassCache.get( key );
if( invocationClass == null )
{
+ log.debug( "Generating method invocation class for method " + interfaceMethod + "..." );
final CtClass ctClass = JavassistUtils.createClass(
interfaceMethod.getDeclaringClass().getSimpleName() + "_" + interfaceMethod.getName() +
"_invocation",
@@ -154,76 +151,11 @@
return false;
}
final CacheKey cacheKey = ( CacheKey ) o;
- if( classLoader != null ? !classLoader.equals( cacheKey.classLoader ) : cacheKey.classLoader != null )
- {
- return false;
- }
- if( method != null ? !method.equals( cacheKey.method ) : cacheKey.method != null )
- {
- return false;
- }
- return true;
- }
-
- public int hashCode()
- {
- int result;
- result = ( classLoader != null ? classLoader.hashCode() : 0 );
- result = 29 * result + ( method != null ? method.hashCode() : 0 );
- return result;
- }
- }
-
- public static Method[] getImplementationMethods( Class... proxyInterfaces )
- {
- final Set<MethodSignature> signatures = new HashSet<MethodSignature>();
- final List<Method> resultingMethods = new LinkedList<Method>();
- for( int i = 0; i < proxyInterfaces.length; i++ )
- {
- Class proxyInterface = proxyInterfaces[i];
- final Method[] methods = proxyInterface.getDeclaredMethods();
- for( int j = 0; j < methods.length; j++ )
- {
- final MethodSignature signature = new MethodSignature( methods[j] );
- if( !signatures.contains( signature ) )
- {
- signatures.add( signature );
- resultingMethods.add( methods[j] );
- }
- }
- }
- final Method[] results = new Method[resultingMethods.size()];
- return resultingMethods.toArray( results );
- }
-
- private static class MethodSignature
- {
- private final String name;
- private final List<Class> parameterTypes;
-
- public MethodSignature( Method method )
- {
- this.name = method.getName();
- this.parameterTypes = Arrays.<Class>asList( method.getParameterTypes() );
- }
-
- public boolean equals( Object o )
- {
- if( this == o )
- {
- return true;
- }
- if( o == null || getClass() != o.getClass() )
- {
- return false;
- }
- final MethodSignature that = ( MethodSignature ) o;
- if( name != null ? !name.equals( that.name ) : that.name != null )
+ if( !classLoader.equals( cacheKey.classLoader ) )
{
return false;
}
- if( parameterTypes != null ? !parameterTypes.equals( that.parameterTypes ) :
- that.parameterTypes != null )
+ if( !method.equals( cacheKey.method ) )
{
return false;
}
@@ -233,8 +165,8 @@
public int hashCode()
{
int result;
- result = ( name != null ? name.hashCode() : 0 );
- result = 29 * result + ( parameterTypes != null ? parameterTypes.hashCode() : 0 );
+ result = classLoader.hashCode();
+ result = 29 * result + method.hashCode();
return result;
}
}
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=265573&r1=265572&r2=265573&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 Wed Aug 31 13:22:49 2005
@@ -114,10 +114,11 @@
{
final MethodInvocationTester tester = new MethodInvocationTester();
final EchoImpl target = new EchoImpl();
- final Echo proxy = ( Echo ) factory.createInterceptingProxy( target, tester, Echo.class );
- proxy.echoBack( "hello1" );
+ final Echo proxy1 = ( Echo ) factory.createInterceptingProxy( target, tester, Echo.class );
+ final Echo proxy2 = ( Echo ) factory.createInterceptingProxy( target, tester, Echo.class, DuplicateEcho.class );
+ proxy1.echoBack( "hello1" );
final Class invocationClass1 = tester.invocationClass;
- proxy.echoBack( "hello2" );
+ proxy2.echoBack( "hello2" );
assertEquals( invocationClass1, tester.invocationClass );
}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java?rev=265573&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java Wed Aug 31 13:22:49 2005
@@ -0,0 +1,34 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.proxy.factory;
+import junit.framework.TestCase;
+import org.apache.commons.proxy.util.DuplicateEcho;
+import org.apache.commons.proxy.util.Echo;
+
+public class TestMethodSignature extends TestCase
+{
+ public void testEquals() throws Exception
+ {
+ final MethodSignature sig = new MethodSignature( Echo.class.getMethod( "echoBack", String.class ) );
+ assertTrue( sig.equals( sig ) );
+ assertFalse( sig.equals( "echoBack" ) );
+ assertEquals( sig, new MethodSignature( Echo.class.getMethod( "echoBack", String.class ) ) );
+ assertEquals( sig, new MethodSignature( DuplicateEcho.class.getMethod( "echoBack", String.class ) ) );
+ assertFalse( sig.equals( new MethodSignature( Echo.class.getMethod( "echoBack", String.class, String.class ) ) ) );
+ assertFalse( sig.equals( new MethodSignature( Echo.class.getMethod( "echo" ) ) ) );
+ }
+}
\ No newline at end of file
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/TestMethodSignature.java
------------------------------------------------------------------------------
svn:keywords = Id
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org