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/10/14 20:28:47 UTC
svn commit: r321167 - in /jakarta/commons/sandbox/proxy/trunk/src:
java/org/apache/commons/proxy/provider/CloningProvider.java
test/org/apache/commons/proxy/provider/TestCloningProvider.java
Author: jcarman
Date: Fri Oct 14 11:28:42 2005
New Revision: 321167
URL: http://svn.apache.org/viewcvs?rev=321167&view=rev
Log:
Added support for a cloning provider.
Added:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java (with props)
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java (with props)
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java?rev=321167&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java Fri Oct 14 11:28:42 2005
@@ -0,0 +1,74 @@
+/* $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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.ProxyUtils;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * Merely calls <code>clone()</code> (reflectively) on the given {@link Cloneable} object.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class CloningProvider implements ObjectProvider
+{
+ private final Cloneable cloneable;
+ private Method cloneMethod;
+
+ public CloningProvider( Cloneable cloneable )
+ {
+ this.cloneable = cloneable;
+ }
+
+ private synchronized Method getCloneMethod()
+ {
+ if( cloneMethod == null )
+ {
+ try
+ {
+ cloneMethod = cloneable.getClass().getMethod( "clone", ProxyUtils.EMPTY_ARGUMENT_TYPES );
+ }
+ catch( NoSuchMethodException e )
+ {
+ throw new ObjectProviderException( "Class " + cloneable.getClass().getName() + " does not have a public clone() method." );
+ }
+ }
+ return cloneMethod;
+ }
+
+ public Object getObject()
+ {
+ try
+ {
+ return getCloneMethod().invoke( cloneable, ProxyUtils.EMPTY_ARGUMENTS );
+ }
+ catch( IllegalAccessException e )
+ {
+ throw new ObjectProviderException( "Class " + cloneable.getClass().getName() + " does not have a public clone() method.", e );
+ }
+ catch( InvocationTargetException e )
+ {
+ throw new ObjectProviderException( "Attempt to clone object of type " + cloneable.getClass().getName() + " threw an exception.", e );
+ }
+ }
+
+}
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CloningProvider.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java?rev=321167&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java Fri Oct 14 11:28:42 2005
@@ -0,0 +1,72 @@
+/* $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.provider;
+
+import junit.framework.TestCase;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.util.Date;
+
+public class TestCloningProvider extends TestCase
+{
+ public void testValidCloneable()
+ {
+ final Date now = new Date();
+ final CloningProvider provider = new CloningProvider( now );
+ final Date clone = ( Date ) provider.getObject();
+ assertEquals( now, clone );
+ assertNotSame( now, clone );
+ }
+
+ public void testWithInvalidCloneable()
+ {
+ final CloningProvider provider = new CloningProvider( new InvalidCloneable() );
+ try
+ {
+ provider.getObject();
+ fail();
+ }
+ catch( ObjectProviderException e )
+ {
+ }
+ }
+
+ public void testWithExceptionThrown()
+ {
+ final CloningProvider provider = new CloningProvider( new ExceptionCloneable() );
+ try
+ {
+ provider.getObject();
+ fail();
+ }
+ catch( ObjectProviderException e )
+ {
+ }
+ }
+
+ public static class InvalidCloneable implements Cloneable
+ {
+ }
+
+ public static class ExceptionCloneable implements Cloneable
+ {
+ public Object clone()
+ {
+ throw new RuntimeException( "No clone for you!" );
+ }
+ }
+}
\ No newline at end of file
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCloningProvider.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