Author: simonetripodi Date: Fri Jun 8 12:48:29 2012 New Revision: 1348047 URL: http://svn.apache.org/viewvc?rev=1348047&view=rev Log: [SANDBOX-399] Handle methods that return void separately - patch provided by Benedikt Ritter
Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java (with props) commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java (with props) Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1348047&r1=1348046&r2=1348047&view=diff ============================================================================== --- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original) +++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Fri Jun 8 12:48:29 2012 @@ -32,6 +32,9 @@ <action dev="simonetripodi" type="add" issue="SANDBOX-401" due-to="Benedikt Ritter"> Performance improvement: store hash code of AccessibleObjectDescriptor as member variable </action> + <action dev="simonetripodi" type="add" issue="SANDBOX-399" due-to="Benedikt Ritter"> + Handle methods that return void separately + </action> <action dev="simonetripodi" type="add" issue="SANDBOX-398" due-to="Benedikt Ritter"> Implement unit tests for BeanUtils.onClassName() </action> Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java?rev=1348047&r1=1348046&r2=1348047&view=diff ============================================================================== --- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java (original) +++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java Fri Jun 8 12:48:29 2012 @@ -69,6 +69,10 @@ final class DefaultArgumentsAccessor } Object result = method.invoke( bean, getParameters( arguments ) ); + if ( method.getReturnType() == void.class || result == null) + { + return new NullBeanAccessor<Object>(beanType.getName(), methodName); + } return new DefaultBeanAccessor<Object>( result ); } Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java?rev=1348047&view=auto ============================================================================== --- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java (added) +++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java Fri Jun 8 12:48:29 2012 @@ -0,0 +1,181 @@ +package org.apache.commons.beanutils2; + +/* + * 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. + */ + +import static java.lang.String.format; + +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +final class NullBeanAccessor<B> + implements BeanAccessor<B> +{ + + private final String errorMessage; + + public NullBeanAccessor( String beanTypeName, String methodName ) + { + errorMessage = format( "%s.%s returned null!", beanTypeName, methodName ); + } + + /** + * {@inheritDoc} + */ + public BeanAccessor<?> get( String propertyName ) + throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public IndexedPropertyGetterAccessor getIndexed( String propertyName ) + throws IntrospectionException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public MappedPropertyGetterAccessor getMapped( String propertyName ) + throws InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public B get() + { + return null; + } + + /** + * {@inheritDoc} + */ + public <V> V cast() + { + return null; + } + + /** + * {@inheritDoc} + */ + public BeanPropertySetter<B> set( String propertyName ) + throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public IndexedPropertySetterAccessor<B> setIndexed( String propertyName ) + throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public IndexedPropertySetterAccessor<B> setMapped( String propertyName ) + throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public boolean isReadable( String propertyName ) + throws IntrospectionException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public boolean isWritable( String propertyName ) + throws IntrospectionException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public B cloneBean() + throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException, + NoSuchMethodException + { + return null; + } + + /** + * {@inheritDoc} + */ + public <T extends B> void copyPropertiesTo( T target ) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public Map<String, Object> describe() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public void populate( Map<String, Object> properties ) + throws IllegalAccessException, IllegalArgumentException, IntrospectionException, InvocationTargetException, + NoSuchMethodException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public ArgumentsAccessor invoke( String methodName ) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException + { + throw new NullPointerException( errorMessage ); + } + + /** + * {@inheritDoc} + */ + public ArgumentsAccessor invokeExact( String methodName ) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException + { + throw new NullPointerException( errorMessage ); + } + +} Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java?rev=1348047&view=auto ============================================================================== --- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java (added) +++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java Fri Jun 8 12:48:29 2012 @@ -0,0 +1,162 @@ +package org.apache.commons.beanutils2; + +/* + * 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. + */ + +import static org.apache.commons.beanutils2.BeanUtils.on; +import static org.junit.Assert.assertNull; + +import java.util.HashMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Test case to make sure, that methods with void signature are handled correctly. + */ +public class VoidMethodsTestCase +{ + + private BeanAccessor<?> voidAccessor = null; + + @Before + public void setUp() + throws Exception + { + voidAccessor = on( TestBean.class ).invokeStatic( "incrementCounter" ).withArguments(); + } + + @After + public void tearDown() + { + voidAccessor = null; + } + + @Test + public void get() + throws Exception + { + assertNull( voidAccessor.get() ); + } + + @Test( expected = NullPointerException.class ) + public void getProperty() + throws Exception + { + voidAccessor.get( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void getIndexed() + throws Exception + { + voidAccessor.getIndexed( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void getMapped() + throws Exception + { + voidAccessor.getMapped( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void setProperty() + throws Exception + { + voidAccessor.set( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void setIndexed() + throws Exception + { + voidAccessor.setIndexed( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void setMapped() + throws Exception + { + voidAccessor.setMapped( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void isReadable() + throws Exception + { + voidAccessor.isReadable( "propertyName" ); + } + + @Test( expected = NullPointerException.class ) + public void isWritable() + throws Exception + { + voidAccessor.isWritable( "propertyName" ); + } + + @Test + public void cast() + { + assertNull( voidAccessor.cast() ); + } + + @Test + public void cloneBean() + throws Exception + { + assertNull( voidAccessor.cloneBean() ); + } + + @Test( expected = NullPointerException.class ) + public void describe() + throws Exception + { + voidAccessor.describe(); + } + + @Test( expected = NullPointerException.class ) + public void populate() + throws Exception + { + voidAccessor.populate( new HashMap<String, Object>( 0 ) ); + } + + @Test + public void copyPropertiesTo() + throws Exception + { + // no idea how to invoke this + // voidAccessor.copyPropertiesTo( target ); + } + + @Test( expected = NullPointerException.class ) + public void invoke() + throws Exception + { + voidAccessor.invoke( "methodName" ); + } + + @Test( expected = NullPointerException.class ) + public void invokeExact() + throws Exception + { + voidAccessor.invokeExact( "methodName" ); + } + +} Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java ------------------------------------------------------------------------------ svn:mime-type = text/plain