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/09/05 16:43:36 UTC
svn commit: r278756 - in /jakarta/commons/sandbox/proxy/trunk/src:
java/org/apache/commons/proxy/ProxyUtils.java
java/org/apache/commons/proxy/handler/NullInvocationHandler.java
test/org/apache/commons/proxy/TestProxyUtils.java
Author: jcarman
Date: Mon Sep 5 07:43:32 2005
New Revision: 278756
URL: http://svn.apache.org/viewcvs?rev=278756&view=rev
Log:
Implementing Null Object pattern.
Added:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java (with props)
jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java (with props)
Modified:
jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
Added: 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=278756&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java Mon Sep 5 07:43:32 2005
@@ -0,0 +1,49 @@
+/* $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;
+
+import org.apache.commons.proxy.handler.NullInvocationHandler;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ProxyUtils
+{
+ /**
+ * Creates a "null object" which implements the <code>proxyInterfaces</code>.
+ * @param proxyFactory the proxy factory to be used to create the proxy object
+ * @param proxyInterfaces the proxy interfaces
+ * @return a "null object" which implements the <code>proxyInterfaces</code>.
+ */
+ public static Object createNullObject( ProxyFactory proxyFactory, Class... proxyInterfaces )
+ {
+ return proxyFactory.createInvocationHandlerProxy( new NullInvocationHandler(), proxyInterfaces );
+ }
+
+ /**
+ * Creates a "null object" which implements the <code>proxyInterfaces</code>.
+ * @param proxyFactory the proxy factory to be used to create the proxy object
+ * @param classLoader the class loader to be used by the proxy factory to create the proxy object
+ * @param proxyInterfaces the proxy interfaces
+ * @return a "null object" which implements the <code>proxyInterfaces</code>.
+ */
+ public static Object createNullObject( ProxyFactory proxyFactory, ClassLoader classLoader, Class... proxyInterfaces )
+ {
+ return proxyFactory.createInvocationHandlerProxy( classLoader, new NullInvocationHandler(), proxyInterfaces );
+ }
+}
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java?rev=278756&r1=278755&r2=278756&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java (original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java Mon Sep 5 07:43:32 2005
@@ -34,7 +34,43 @@
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
{
- return null;
+ final Class<?> returnType = method.getReturnType();
+ if( returnType.isPrimitive() )
+ {
+ if( Integer.TYPE.equals( returnType ) )
+ {
+ return 0;
+ }
+ else if( Long.TYPE.equals( returnType ) )
+ {
+ return 0L;
+ }
+ else if( Double.TYPE.equals( returnType ) )
+ {
+ return 0.0;
+ }
+ else if( Float.TYPE.equals( returnType ) )
+ {
+ return 0.0f;
+ }
+ else if( Short.TYPE.equals( returnType ) )
+ {
+ return ( short )0;
+ }
+ else if( Character.TYPE.equals( returnType ) )
+ {
+ return ( char )0;
+ }
+ else if( Byte.TYPE.equals( returnType ) )
+ {
+ return ( byte )0;
+ }
+ return 0;
+ }
+ else
+ {
+ return null;
+ }
}
}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java?rev=278756&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java Mon Sep 5 07:43:32 2005
@@ -0,0 +1,33 @@
+/* $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;
+import junit.framework.*;
+import org.apache.commons.proxy.ProxyUtils;
+import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
+import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
+import org.apache.commons.proxy.util.Echo;
+
+public class TestProxyUtils extends TestCase
+{
+ public void testCreateNullObject() throws Exception
+ {
+ final Echo nullEcho = ( Echo )ProxyUtils.createNullObject( new JavassistProxyFactory(), Echo.class );
+ assertNull( nullEcho.echoBack( "hello" ) );
+ assertNull( nullEcho.echoBack( "hello", "world" ) );
+ assertEquals( ( int ) 0, nullEcho.echoBack( 12345 ) );
+ }
+}
\ No newline at end of file
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.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