Author: simonetripodi
Date: Thu Mar 15 11:56:49 2012
New Revision: 1300940
URL: http://svn.apache.org/viewvc?rev=1300940&view=rev
Log:
[SANDBOX-369] Move logic for type compatibility checking from
AccessibleObjectsRegistry to TypeUtils - patch provided by Benedikt Ritter
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeUtils.java
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeUtilsTest.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=1300940&r1=1300939&r2=1300940&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Thu Mar 15
11:56:49 2012
@@ -56,6 +56,9 @@
<action dev="simonetripodi" type="update" issue="SANDBOX-371"
due-to="Benedikt Ritter">
Make sure that a property is readable in
DefaultBeanAccessor.getProperty( String name )
</action>
+ <action dev="simonetripodi" type="update" issue="SANDBOX-369"
due-to="Benedikt Ritter">
+ Move logic for type compatibility checking from
AccessibleObjectsRegistry to TypeUtils
+ </action>
<action dev="simonetripodi" type="update" issue="SANDBOX-365"
due-to="Benedikt Ritter">
Extend Assertions for checking null references in arrays
</action>
Modified:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java?rev=1300940&r1=1300939&r2=1300940&view=diff
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java
(original)
+++
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/AccessibleObjectsRegistry.java
Thu Mar 15 11:56:49 2012
@@ -23,7 +23,7 @@ import static java.lang.System.getSecuri
import static java.lang.reflect.Modifier.isPublic;
import static java.security.AccessController.doPrivileged;
import static org.apache.commons.beanutils2.TypeUtils.getPrimitiveWrapper;
-import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible;
+import static org.apache.commons.beanutils2.TypeUtils.*;
import static org.apache.commons.beanutils2.internal.Assertions.checkArgument;
import java.lang.ref.Reference;
@@ -125,15 +125,7 @@ abstract class AccessibleObjectsRegistry
int methodParamSize = methodsParams.length;
if ( methodParamSize == paramSize )
{
- boolean match = true;
- for ( int n = 0; n < methodParamSize; n++ )
- {
- if ( !isAssignmentCompatible( methodsParams[n],
parameterTypes[n] ) )
- {
- match = false;
- break;
- }
- }
+ boolean match = checkTypesCompatible( methodsParams,
parameterTypes );
if ( match )
{
Modified:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeUtils.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeUtils.java?rev=1300940&r1=1300939&r2=1300940&view=diff
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeUtils.java
(original)
+++
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeUtils.java
Thu Mar 15 11:56:49 2012
@@ -19,6 +19,7 @@ package org.apache.commons.beanutils2;
* under the License.
*/
+import static
org.apache.commons.beanutils2.internal.Assertions.checkNoneIsNull;
import static org.apache.commons.beanutils2.internal.Assertions.checkNotNull;
final class TypeUtils
@@ -185,4 +186,35 @@ final class TypeUtils
return clazz;
}
+ /**
+ * Checks if a set of parameters is compatible to another set. For example
this method can be used to check if all
+ * parameter types of one method are compatible to the parameter types of
another method. Note that two empty arrays
+ * will pass the test.
+ *
+ * @param types the types check against otherTypes. None must be {@code
null}!
+ * @param otherTypes the other types used for comparing. None must be
{@code null}!
+ * @return true if all parameters are compatible
+ * @throws NullPointerException if either {@code types}, {@code
otherTypes} or one of the contained types is
+ * {@code null}.
+ */
+ public static boolean checkTypesCompatible( Class<?>[] types, Class<?>[]
otherTypes )
+ {
+ checkNotNull( types, "Input parameter 'types' must not be null!" );
+ checkNotNull( otherTypes, "Input parameter 'otherTypes' must not be
null!" );
+ checkNoneIsNull( types );
+ checkNoneIsNull( otherTypes );
+ if ( types.length != otherTypes.length )
+ {
+ return false;
+ }
+ for ( int n = 0; n < types.length; n++ )
+ {
+ if ( !isAssignmentCompatible( types[n], otherTypes[n] ) )
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
}
Modified:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeUtilsTest.java?rev=1300940&r1=1300939&r2=1300940&view=diff
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeUtilsTest.java
(original)
+++
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeUtilsTest.java
Thu Mar 15 11:56:49 2012
@@ -19,6 +19,7 @@ package org.apache.commons.beanutils2;
* under the License.
*/
+import static org.apache.commons.beanutils2.TypeUtils.checkTypesCompatible;
import static org.apache.commons.beanutils2.TypeUtils.getPrimitiveType;
import static org.apache.commons.beanutils2.TypeUtils.getPrimitiveWrapper;
import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible;
@@ -1861,4 +1862,60 @@ public class TypeUtilsTest
toNonPrimitiveClass( null );
}
+ @Test
+ public void checkTypesCompatibleWithCompatibleTypes()
+ {
+ Class<?>[] primitives = new Class<?>[] { boolean.class, byte.class,
short.class,
+ int.class, long.class, float.class, double.class, char.class };
+
+ Class<?>[] wrappers = new Class<?>[] { Boolean.class, Byte.class,
Short.class,
+ Integer.class, Long.class, Float.class, Double.class,
Character.class };
+
+ assertTrue(checkTypesCompatible(primitives, wrappers));
+ }
+
+ @Test
+ public void checkTypesCompatibleWithIncompatibleTypes()
+ {
+ Class<?>[] primitives = new Class<?>[] { boolean.class, byte.class,
short.class,
+ int.class, long.class, float.class, double.class, char.class };
+
+ Class<?>[] objects = new Class<?>[] { String.class, Object.class,
TestBean.class };
+
+ assertFalse(checkTypesCompatible(primitives, objects));
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkTypesCompatibleArrayNull()
+ {
+ Class<?>[] types = new Class<?>[0];
+ Class<?>[] otherTypes = null;
+ checkTypesCompatible( types, otherTypes );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkTypesCompatibleNullArray()
+ {
+ Class<?>[] types = null;
+ Class<?>[] otherTypes = new Class<?>[0];
+ checkTypesCompatible( types, otherTypes );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkTypesCompatibleNullNull()
+ {
+ Class<?>[] types = null;
+ Class<?>[] otherTypes = null;
+ checkTypesCompatible( types, otherTypes );
+ }
+
+ @Test( expected = NullPointerException.class )
+ public void checkTypesCompatibleArrayWithNullReference()
+ {
+ Class<?>[] primitives = new Class<?>[] { boolean.class, null,
short.class };
+ Class<?>[] objects = new Class<?>[] { String.class, Object.class,
TestBean.class };
+
+ checkTypesCompatible(primitives, objects);
+ }
+
}