You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/07/23 13:52:51 UTC
svn commit: r1364590 - in /commons/sandbox/beanutils2/trunk/src: changes/
main/java/org/apache/commons/beanutils2/
test/java/org/apache/commons/beanutils2/
Author: simonetripodi
Date: Mon Jul 23 11:52:51 2012
New Revision: 1364590
URL: http://svn.apache.org/viewvc?rev=1364590&view=rev
Log:
[SANDBOX-419] Implement setIndexed(String propertyName) on DefaultBeanAccessor - patch provided by Benedikt Ritter
Added:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java (with props)
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java (with props)
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetterAccessor.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=1364590&r1=1364589&r2=1364590&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Mon Jul 23 11:52:51 2012
@@ -38,6 +38,9 @@
<action dev="simonetripodi" type="add" issue="SANDBOX-421" due-to="Benedikt Ritter">
Make sure VoidMethodsTestCase uses a NullBeanAcessor
</action>
+ <action dev="simonetripodi" type="add" issue="SANDBOX-419" due-to="Benedikt Ritter">
+ Implement setIndexed(String propertyName) on DefaultBeanAccessor
+ </action>
<action dev="simonetripodi" type="add" issue="SANDBOX-405" due-to="Benedikt Ritter">
Implement copyPropertiesTo(T target) on DefaultBeanAccessor
</action>
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java?rev=1364590&r1=1364589&r2=1364590&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java Mon Jul 23 11:52:51 2012
@@ -115,8 +115,9 @@ final class DefaultBeanAccessor<B>
public IndexedPropertySetterAccessor<B> setIndexed( String propertyName )
throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException
{
- // TODO
- return null;
+ checkNotNull( propertyName, "Parameter 'propertyName' must not be null!" );
+ return new DefaultIndexedPropertySetterAccessor<B>( bean, propertyName,
+ properties.getIndexedWriteMethod( propertyName ) );
}
/**
Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java?rev=1364590&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java Mon Jul 23 11:52:51 2012
@@ -0,0 +1,70 @@
+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.TypeUtils.isAssignmentCompatible;
+import static org.apache.commons.beanutils2.Assertions.checkArgument;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+final class DefaultIndexedPropertySetter<B>
+ implements BeanPropertySetter<B>
+{
+
+ private final B bean;
+
+ private final Method setterMethod;
+
+ private final int index;
+
+ public DefaultIndexedPropertySetter( B bean, Method indexedSetterMethod, int index )
+ {
+ this.bean = bean;
+ this.setterMethod = indexedSetterMethod;
+ this.index = index;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public <V> BeanAccessor<B> with( V value )
+ throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
+ {
+ // TODO this assumes, that the index is the first parameter, and the value to set is the second.
+ // will this always be the case or do we have to check if one parameter either of the parameters
+ // is an int and the other is assignment compatible to value?
+ Class<?> paramType = setterMethod.getParameterTypes()[1];
+
+ if ( value == null )
+ {
+ checkArgument( !paramType.isPrimitive(), "Null can not be assigned to a primitive property!" );
+ }
+ else
+ {
+ checkArgument( isAssignmentCompatible( paramType, value.getClass() ),
+ "Value of type %s is not compatible to parameter type %s!",
+ value.getClass().getName(), paramType.getName() );
+ }
+
+ setterMethod.invoke( bean, index, value );
+
+ return new DefaultBeanAccessor<B>( bean );
+ }
+
+}
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetterAccessor.java?rev=1364590&r1=1364589&r2=1364590&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetterAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetterAccessor.java Mon Jul 23 11:52:51 2012
@@ -24,33 +24,32 @@ import static org.apache.commons.beanuti
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-final class DefaultIndexedPropertySetterAccessor
- implements IndexedPropertyGetterAccessor
+final class DefaultIndexedPropertySetterAccessor<B>
+ implements IndexedPropertySetterAccessor<B>
{
- private final Object bean;
+ private final B bean;
private final String propertyName;
- private final Method indexedReadMethod;
+ private final Method indexedSetterMethod;
- public DefaultIndexedPropertySetterAccessor( Object bean, String propertyName, Method indexedReadMethod )
+ public DefaultIndexedPropertySetterAccessor( B bean, String propertyName, Method indexedSetterMethod )
{
this.bean = bean;
this.propertyName = propertyName;
- this.indexedReadMethod = indexedReadMethod;
+ this.indexedSetterMethod = indexedSetterMethod;
}
/**
* {@inheritDoc}
*/
- public BeanAccessor<?> at( int index )
+ public BeanPropertySetter<B> at( int index )
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
checkArgument( index >= 0, "Indexed property '%s' in bean of type %s cannot be get from a negative index %s",
propertyName, bean.getClass().getName(), index );
- Object indexedValue = indexedReadMethod.invoke( bean, index );
- return new DefaultBeanAccessor<Object>( indexedValue );
+ return new DefaultIndexedPropertySetter<B>( bean, indexedSetterMethod, index );
}
}
Added: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java?rev=1364590&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java (added)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java Mon Jul 23 11:52:51 2012
@@ -0,0 +1,98 @@
+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.assertTrue;
+
+import java.beans.IntrospectionException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SetIndexedPropertyTestCase
+{
+
+ private TestBean testBean;
+
+ @Before
+ public void setUp()
+ {
+ testBean = new TestBean();
+ }
+
+ @After
+ public void tearDown()
+ {
+ testBean = null;
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void setIndexedNegative()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "intIndexed" ).at( -1 );
+ }
+
+ @Test( expected = IntrospectionException.class )
+ public void setIndexedUnknown()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "unkown" );
+ }
+
+ @Test
+ public void setIntIndexed()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( 4711 );
+ assertTrue( testBean.getIntIndexed( 1 ) == 4711 );
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void setIntIndexedNull()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "intIndexed" ).at( 1 ).with( null );
+ }
+
+ @Test
+ public void setStringIndexed()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( "Hello World!" );
+ assertTrue( testBean.getStringIndexed( 1 ).equals( "Hello World!" ) );
+ }
+
+ @Test
+ public void setStringIndexedNull()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( null );
+ assertTrue( testBean.getStringIndexed( 1 ) == null );
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void setStringIndexedInt()
+ throws Exception
+ {
+ on( testBean ).setIndexed( "stringIndexed" ).at( 1 ).with( 4711 );
+ }
+
+}
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/SetIndexedPropertyTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain