commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r645239 - in /commons/proper/proxy/branches/version-2.0-work/src: main/java/org/apache/commons/proxy/ main/java/org/apache/commons/proxy/invoker/ main/java/org/apache/commons/proxy/invoker/recorder/ test/java/org/apache/commons/proxy/ test/...
Date Sun, 06 Apr 2008 13:31:53 GMT
Author: jcarman
Date: Sun Apr  6 06:31:51 2008
New Revision: 645239

URL: http://svn.apache.org/viewvc?rev=645239&view=rev
Log:
New InvocationRecorder.

Added:
    commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/
    commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/InvocationRecorder.java
    commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/RecordedInvocation.java
    commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/
    commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestInvocationRecorder.java
    commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestRecordedInvocation.java
Modified:
    commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/ProxyUtils.java
    commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/NullInvoker.java
    commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/TestProxyUtils.java

Modified: commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/ProxyUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/ProxyUtils.java?rev=645239&r1=645238&r2=645239&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/ProxyUtils.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/ProxyUtils.java
Sun Apr  6 06:31:51 2008
@@ -38,7 +38,8 @@
 
     public static final Object[] EMPTY_ARGUMENTS = new Object[0];
     public static final Class[] EMPTY_ARGUMENT_TYPES = new Class[0];
-    private static final Map wrapperClassMap = new HashMap();
+    private static final Map<Class,Class> wrapperClassMap = new HashMap<Class,Class>();
+    private static Map<Class,Object> nullValueMap = new HashMap<Class,Object>();
 
 //**********************************************************************************************************************
 // Static Methods
@@ -56,6 +57,18 @@
         wrapperClassMap.put(Byte.TYPE, Byte.class);
     }
 
+    static
+    {
+        nullValueMap.put(Integer.TYPE, 0);
+        nullValueMap.put(Long.TYPE, ( long ) 0);
+        nullValueMap.put(Short.TYPE, ( short ) 0);
+        nullValueMap.put(Byte.TYPE, ( byte ) 0);
+        nullValueMap.put(Float.TYPE, 0.0f);
+        nullValueMap.put(Double.TYPE, 0.0);
+        nullValueMap.put(Character.TYPE, ( char ) 0);
+        nullValueMap.put(Boolean.TYPE, Boolean.FALSE);
+    }
+    
     /**
      * Creates a "null object" which implements the <code>proxyClasses</code>.
      *
@@ -150,7 +163,17 @@
      */
     public static Class getWrapperClass( Class primitiveType )
     {
-        return ( Class ) wrapperClassMap.get(primitiveType);
+        return wrapperClassMap.get(primitiveType);
+    }
+
+    /**
+     * Returns the proper "null value" as specified by the Java language.
+     * @param type the type
+     * @return the null value
+     */
+    public static <T> T nullValue(Class<T> type)
+    {
+        return (T)nullValueMap.get(type);
     }
 }
 

Modified: commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/NullInvoker.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/NullInvoker.java?rev=645239&r1=645238&r2=645239&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/NullInvoker.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/NullInvoker.java
Sun Apr  6 06:31:51 2008
@@ -18,6 +18,7 @@
 package org.apache.commons.proxy.invoker;
 
 import org.apache.commons.proxy.Invoker;
+import org.apache.commons.proxy.ProxyUtils;
 
 import java.io.Serializable;
 import java.lang.reflect.Method;
@@ -32,29 +33,7 @@
  * @since 1.0
  */
 public class NullInvoker implements Invoker, Serializable
-{
-//**********************************************************************************************************************
-// Fields
-//**********************************************************************************************************************
-
-    private static Map primitiveValueMap = new HashMap();
-
-//**********************************************************************************************************************
-// Static Methods
-//**********************************************************************************************************************
-
-    static
-    {
-        primitiveValueMap.put(Integer.TYPE, new Integer(0));
-        primitiveValueMap.put(Long.TYPE, new Long(0));
-        primitiveValueMap.put(Short.TYPE, new Short(( short ) 0));
-        primitiveValueMap.put(Byte.TYPE, new Byte(( byte ) 0));
-        primitiveValueMap.put(Float.TYPE, new Float(0.0f));
-        primitiveValueMap.put(Double.TYPE, new Double(0.0));
-        primitiveValueMap.put(Character.TYPE, new Character(( char ) 0));
-        primitiveValueMap.put(Boolean.TYPE, Boolean.FALSE);
-    }
-
+{   
 //**********************************************************************************************************************
 // Invoker Implementation
 //**********************************************************************************************************************
@@ -62,14 +41,7 @@
     public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
     {
         final Class returnType = method.getReturnType();
-        if( returnType.isPrimitive() )
-        {
-            return primitiveValueMap.get(returnType);
-        }
-        else
-        {
-            return null;
-        }
+        return ProxyUtils.nullValue(returnType);
     }
 }
 

Added: commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/InvocationRecorder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/InvocationRecorder.java?rev=645239&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/InvocationRecorder.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/InvocationRecorder.java
Sun Apr  6 06:31:51 2008
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.invoker.recorder;
+
+import org.apache.commons.proxy.Invoker;
+import org.apache.commons.proxy.ProxyFactory;
+import org.apache.commons.proxy.ProxyUtils;
+
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @auothor James Carman
+ */
+public class InvocationRecorder
+{
+    private final ProxyFactory proxyFactory;
+    private List<RecordedInvocation> recordedInvocations = new LinkedList<RecordedInvocation>();
+
+    public InvocationRecorder( ProxyFactory proxyFactory )
+    {
+        this.proxyFactory = proxyFactory;
+    }
+
+    public List<RecordedInvocation> getRecordedInvocations()
+    {
+        return recordedInvocations;
+    }
+
+    public <T> T proxy( Class<T> type )
+    {
+        if(proxyFactory.canProxy(type))
+        {
+            return proxyFactory.createInvokerProxy(new InvocationRecorderInvoker(), type);
+        }
+        return ProxyUtils.nullValue(type);
+    }
+
+    private class InvocationRecorderInvoker implements Invoker
+    {
+        public Object invoke( Object o, Method method, Object[] args ) throws Throwable
+        {
+            recordedInvocations.add(new RecordedInvocation(method, args));
+            return proxy(method.getReturnType());
+        }
+    }
+}

Added: commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/RecordedInvocation.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/RecordedInvocation.java?rev=645239&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/RecordedInvocation.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/src/main/java/org/apache/commons/proxy/invoker/recorder/RecordedInvocation.java
Sun Apr  6 06:31:51 2008
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.invoker.recorder;
+
+import org.apache.commons.proxy.ProxyUtils;
+
+import java.lang.reflect.Method;
+
+/**
+ * @auothor James Carman
+ */
+public class RecordedInvocation
+{
+//**********************************************************************************************************************
+// Fields
+//**********************************************************************************************************************
+
+    private final Method invokedMethod;
+    private final Object[] arguments;
+
+//**********************************************************************************************************************
+// Constructors
+//**********************************************************************************************************************
+
+    public RecordedInvocation( Method invokedMethod, Object[] arguments )
+    {
+        this.invokedMethod = invokedMethod;
+        this.arguments = arguments;
+    }
+
+//**********************************************************************************************************************
+// Canonical Methods
+//**********************************************************************************************************************
+
+    public Method getInvokedMethod()
+    {
+        return invokedMethod;
+    }
+
+    public Object[] getArguments()
+    {
+        return arguments;
+    }
+
+    public String toString()
+    {
+        StringBuffer buffer = new StringBuffer();
+        buffer.append(invokedMethod.getDeclaringClass().getName());
+        buffer.append(".");
+        buffer.append(invokedMethod.getName());
+        buffer.append("(");
+        int count = arguments.length;
+        for( int i = 0; i < count; i++ )
+        {
+            Object arg = arguments[i];
+            if( i > 0 )
+            {
+                buffer.append(", ");
+            }
+            convert(buffer, arg);
+        }
+        buffer.append(")");
+        return buffer.toString();
+    }
+
+    protected void convert( StringBuffer buffer, Object input )
+    {
+        if( input == null )
+        {
+            buffer.append("<null>");
+            return;
+        }
+
+        // Primitive types, and non-object arrays
+        // use toString().
+        if( !( input instanceof Object[] ) )
+        {
+            buffer.append(input.toString());
+            return;
+        }
+        else
+        {
+            buffer.append("(");
+            buffer.append(ProxyUtils.getJavaClassName(input.getClass()));
+            buffer.append("){");
+            Object[] array = ( Object[] ) input;
+            int count = array.length;
+            for( int i = 0; i < count; i++ )
+            {
+                if( i > 0 )
+                {
+                    buffer.append(", ");
+                }
+                // We use convert() again, because it could be a multi-dimensional array
+                // where each element must be converted.
+                convert(buffer, array[i]);
+            }
+            buffer.append("}");
+        }
+    }
+}

Modified: commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/TestProxyUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/TestProxyUtils.java?rev=645239&r1=645238&r2=645239&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/TestProxyUtils.java
(original)
+++ commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/TestProxyUtils.java
Sun Apr  6 06:31:51 2008
@@ -50,6 +50,24 @@
         System.setProperties(prevProperties);
     }
 
+    public void testNullValue()
+    {
+        assertNullValue(null, String.class);
+        assertNullValue(( char ) 0, Character.TYPE);
+        assertNullValue(0, Integer.TYPE);
+        assertNullValue(( long ) 0, Long.TYPE);
+        assertNullValue(( short ) 0, Short.TYPE);
+        assertNullValue(( double ) 0, Double.TYPE);
+        assertNullValue(( float ) 0, Float.TYPE);
+        assertNullValue(false, Boolean.TYPE);
+        assertNullValue(( byte ) 0, Byte.TYPE);
+    }
+
+    private void assertNullValue( Object expected, Class type )
+    {
+        assertEquals(expected, ProxyUtils.nullValue(type));
+    }
+
     public void testCreateNullObject() throws Exception
     {
         final Echo nullEcho = ( Echo ) ProxyUtils
@@ -62,8 +80,8 @@
     public void testCreateNullObjectWithClassLoader() throws Exception
     {
         final Echo nullEcho = ( Echo ) ProxyUtils.createNullObject(new JavassistProxyFactory(),
-                Echo.class.getClassLoader(),
-                new Class[] {Echo.class});
+                                                                   Echo.class.getClassLoader(),
+                                                                   new Class[] {Echo.class});
         assertNull(nullEcho.echoBack("hello"));
         assertNull(nullEcho.echoBack("hello", "world"));
         assertEquals(( int ) 0, nullEcho.echoBack(12345));
@@ -72,7 +90,8 @@
     public void testGetAllInterfaces()
     {
         assertNull(ProxyUtils.getAllInterfaces(null));
-        assertEquals(Arrays.asList(new Class[] {DuplicateEcho.class, Serializable.class,
Echo.class}), Arrays.asList(ProxyUtils.getAllInterfaces(EchoImpl.class)));
+        assertEquals(Arrays.asList(new Class[] {DuplicateEcho.class, Serializable.class,
Echo.class}),
+                     Arrays.asList(ProxyUtils.getAllInterfaces(EchoImpl.class)));
     }
 
     public void testGetJavaClassName() throws Exception

Added: commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestInvocationRecorder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestInvocationRecorder.java?rev=645239&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestInvocationRecorder.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestInvocationRecorder.java
Sun Apr  6 06:31:51 2008
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.invoker.recorder;
+
+import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
+import org.apache.commons.proxy.util.AbstractTestCase;
+import org.apache.commons.proxy.ProxyUtils;
+
+import java.util.List;
+
+/**
+ * @auothor James Carman
+ */
+public class TestInvocationRecorder extends AbstractTestCase
+{
+    public void testRecording() throws Exception
+    {
+        InvocationRecorder recorder = new InvocationRecorder(new CglibProxyFactory());
+        Person personProxy = recorder.proxy(Person.class);
+
+        assertEquals(null, personProxy.getAddress().getCity());
+        List<RecordedInvocation> recordedInvocations = recorder.getRecordedInvocations();
+        final RecordedInvocation getAddressInvocation = recordedInvocations.get(0);
+        assertEquals(Person.class.getMethod("getAddress"), getAddressInvocation.getInvokedMethod());
+        assertEquals(0, getAddressInvocation.getArguments().length);
+    }
+
+    public void testProxyNonProxyableType()
+    {
+        InvocationRecorder recorder = new InvocationRecorder(new CglibProxyFactory());
+        assertProxyIsNullValue(recorder, String.class);
+        assertProxyIsNullValue(recorder, Long.TYPE);
+        assertProxyIsNullValue(recorder, Integer.TYPE);
+        assertProxyIsNullValue(recorder, Short.TYPE);
+        assertProxyIsNullValue(recorder, Byte.TYPE);
+
+        assertProxyIsNullValue(recorder, Double.TYPE);
+        assertProxyIsNullValue(recorder, Float.TYPE);
+
+        assertProxyIsNullValue(recorder, Boolean.TYPE);
+
+        assertProxyIsNullValue(recorder, Character.TYPE);
+    }
+
+    private <T> void assertProxyIsNullValue( InvocationRecorder recorder, Class<T>
type )
+    {
+        assertNullValue(recorder.proxy(type), type);
+    }
+
+    public <T> void assertNullValue( T value, Class<T> type )
+    {
+        assertEquals(value, ProxyUtils.nullValue(type));
+    }
+
+    public static interface Person
+    {
+        public Address getAddress();
+    }
+
+    public static interface Address
+    {
+        public String getCity();
+    }
+
+}

Added: commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestRecordedInvocation.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestRecordedInvocation.java?rev=645239&view=auto
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestRecordedInvocation.java
(added)
+++ commons/proper/proxy/branches/version-2.0-work/src/test/java/org/apache/commons/proxy/invoker/recorder/TestRecordedInvocation.java
Sun Apr  6 06:31:51 2008
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.invoker.recorder;
+
+import org.apache.commons.proxy.util.AbstractTestCase;
+
+/**
+ * @auothor James Carman
+ */
+public class TestRecordedInvocation extends AbstractTestCase
+{
+    public void testToString() throws Exception
+    {
+        RecordedInvocation invocation = new RecordedInvocation(String.class.getMethod("toString"),
new Object[0] );
+        assertEquals("java.lang.String.toString()", invocation.toString());
+
+        invocation = new RecordedInvocation(String.class.getMethod("substring", Integer.TYPE),
new Object[] { 1 } );
+        assertEquals("java.lang.String.substring(1)", invocation.toString());
+
+        invocation = new RecordedInvocation(String.class.getMethod("substring", Integer.TYPE,
Integer.TYPE), new Object[] { 1, 2 } );
+        assertEquals("java.lang.String.substring(1, 2)", invocation.toString());
+
+        invocation = new RecordedInvocation(String.class.getMethod("equals", Object.class),
new Object[] { null } );
+        assertEquals("java.lang.String.equals(<null>)", invocation.toString());
+    }
+}



Mime
View raw message