harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r556803 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/IndexedPropertyDescriptor.java test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java
Date Tue, 17 Jul 2007 03:47:31 GMT
Author: tonywu
Date: Mon Jul 16 20:47:30 2007
New Revision: 556803

URL: http://svn.apache.org/viewvc?view=rev&rev=556803
Log:
Fix getter and setter methods
1, handle null value
2, remove redundant code
3, add javadoc

Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java?view=diff&rev=556803&r1=556802&r2=556803
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
Mon Jul 16 20:47:30 2007
@@ -18,8 +18,8 @@
 package java.beans;
 
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import org.apache.harmony.beans.internal.nls.Messages;
+
+import org.apache.harmony.beans.BeansUtils;
 
 public class IndexedPropertyDescriptor extends PropertyDescriptor {
 
@@ -145,107 +145,92 @@
                 .concat(initialUpperCase(propertyName)));
     }
 
+    /**
+     * Sets the indexed getter as the specified method.
+     * 
+     * @param indexedGetter
+     *            the specified indexed getter.
+     * @throws IntrospectionException
+     */
     public void setIndexedReadMethod(Method indexedGetter)
             throws IntrospectionException {
-        if (indexedGetter != null) {
-            int modifiers = indexedGetter.getModifiers();
-            Class<?>[] parameterTypes;
-            Class<?> returnType;
-            Class<?> indexedPropertyType;
-
-            if (!Modifier.isPublic(modifiers)) {
-                throw new IntrospectionException(Messages.getString("beans.21")); //$NON-NLS-1$
-            }
-            parameterTypes = indexedGetter.getParameterTypes();
-            if (parameterTypes.length != 1) {
-                throw new IntrospectionException(Messages.getString("beans.22")); //$NON-NLS-1$
-            }
-            if (!parameterTypes[0].equals(int.class)) {
-                throw new IntrospectionException(Messages.getString("beans.23")); //$NON-NLS-1$
-            }
-            returnType = indexedGetter.getReturnType();
-            indexedPropertyType = getIndexedPropertyType();
-            if ((indexedPropertyType != null)
-                    && !returnType.equals(indexedPropertyType)) {
-                throw new IntrospectionException(Messages.getString("beans.24")); //$NON-NLS-1$
-            }
-        }
-        this.indexedGetter = indexedGetter;
+        this.internalSetIndexedReadMethod(indexedGetter);
     }
 
+    /**
+     * Sets the indexed setter as the specified method.
+     * 
+     * @param indexedSetter
+     *            the specified indexed setter.
+     * @throws IntrospectionException
+     */
     public void setIndexedWriteMethod(Method indexedSetter)
             throws IntrospectionException {
-        if (indexedSetter != null) {
-            int modifiers = indexedSetter.getModifiers();
-            Class<?>[] parameterTypes;
-            Class<?> firstParameterType;
-            Class<?> secondParameterType;
-            Class<?> propType;
-
-            if (!Modifier.isPublic(modifiers)) {
-                throw new IntrospectionException(Messages.getString("beans.25")); //$NON-NLS-1$
-            }
-            parameterTypes = indexedSetter.getParameterTypes();
-            if (parameterTypes.length != 2) {
-                throw new IntrospectionException(Messages.getString("beans.26")); //$NON-NLS-1$
-            }
-            firstParameterType = parameterTypes[0];
-            if (!firstParameterType.equals(int.class)) {
-                throw new IntrospectionException(Messages.getString("beans.27")); //$NON-NLS-1$
-            }
-            secondParameterType = parameterTypes[1];
-            propType = getIndexedPropertyType();
-            if (propType != null && !secondParameterType.equals(propType)) {
-                throw new IntrospectionException(Messages.getString("beans.28")); //$NON-NLS-1$
-            }
-        }
-        this.indexedSetter = indexedSetter;
+        this.internalSetIndexedWriteMethod(indexedSetter, false);
     }
 
+    /**
+     * Obtains the indexed setter.
+     * 
+     * @return the indexed setter.
+     */
     public Method getIndexedWriteMethod() {
         return indexedSetter;
     }
 
+    /**
+     * Obtains the indexed getter.
+     * 
+     * @return the indexed getter.
+     */
     public Method getIndexedReadMethod() {
         return indexedGetter;
     }
 
+    /**
+     * Determines if this <code>IndexedPropertyDescriptor</code> is equal to
+     * the specified object. Two <code>IndexedPropertyDescriptor</code> s are
+     * equal if the reader, indexed reader, writer, indexed writer, property
+     * types, indexed property type, property editor and flags are equal.
+     * 
+     * @param obj
+     * @return true if this indexed property descriptor is equal to the
+     *         specified object.
+     */
     @Override
     public boolean equals(Object obj) {
-        boolean result = super.equals(obj);
-
-        if (result) {
-            IndexedPropertyDescriptor pd = (IndexedPropertyDescriptor) obj;
-
-            if (indexedGetter != null) {
-                result = indexedGetter.equals(pd.getIndexedReadMethod());
-            } else if (result && indexedGetter == null) {
-                result = pd.getIndexedReadMethod() == null;
-            }
-
-            if (result) {
-                if (indexedSetter != null) {
-                    result = indexedSetter.equals(pd.getIndexedWriteMethod());
-                } else if (indexedSetter == null) {
-                    result = pd.getIndexedWriteMethod() == null;
-                }
-            }
+        if (!(obj instanceof IndexedPropertyDescriptor)) {
+            return false;
         }
 
-        return result;
+        IndexedPropertyDescriptor other = (IndexedPropertyDescriptor) obj;
+
+        return (super.equals(other)
+                && (indexedPropertyType == null ? other.indexedPropertyType == null
+                        : indexedPropertyType.equals(other.indexedPropertyType))
+                && (indexedGetter == null ? other.indexedGetter == null
+                        : indexedGetter.equals(other.indexedGetter)) && (indexedSetter
== null ? other.indexedSetter == null
+                : indexedSetter.equals(other.indexedSetter)));
+    }
+    
+    /**
+     * HashCode of the IndexedPropertyDescriptor
+     */
+    @Override
+    public int hashCode() {
+        return super.hashCode() + BeansUtils.getHashCode(indexedPropertyType)
+                + BeansUtils.getHashCode(indexedGetter)
+                + BeansUtils.getHashCode(indexedSetter);
     }
+    
 
+    /**
+     * Obtains the Class object of the indexed property type.
+     * 
+     * @return the Class object of the indexed property type.
+     */
     public Class<?> getIndexedPropertyType() {
-        Class<?> result = null;
-
-        if (indexedGetter != null) {
-            result = indexedGetter.getReturnType();
-        } else if (indexedSetter != null) {
-            Class<?>[] parameterTypes = indexedSetter.getParameterTypes();
-
-            result = parameterTypes[1];
-        }
-        return result;
+        return indexedPropertyType;
     }
 
     private void setIndexedReadMethod(Class beanClass, String indexedGetterName)

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java?view=diff&rev=556803&r1=556802&r2=556803
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IndexedPropertyDescriptorTest.java
Mon Jul 16 20:47:30 2007
@@ -19,6 +19,7 @@
 
 import java.beans.IndexedPropertyDescriptor;
 import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
 import java.lang.reflect.Method;
 
 import junit.framework.TestCase;
@@ -1378,6 +1379,69 @@
             fail("Should throw IntrospectionException");
         } catch (IntrospectionException e) {
         }
+        
+        ipd = new IndexedPropertyDescriptor("data", NormalBean.class);
+        ipd.setIndexedReadMethod(null);
+        try {
+            ipd.setIndexedWriteMethod(NormalBean.class.getMethod("setData", Integer.TYPE,
Integer.TYPE));
+            fail("should throw IntrospectionException");
+        } catch (IntrospectionException e) {
+            // expected
+        }
+    }
+    
+    public void testSetIndexedMethodNullNull() throws Exception {
+        try {
+            IndexedPropertyDescriptor i = new IndexedPropertyDescriptor("a",
+                    NormalBean.class, "getData", "setData", null,
+                    "setData");
+            i.setIndexedWriteMethod(null);
+            fail("should throw IntrospectionException.");
+        } catch (IntrospectionException e) {
+            // expected
+        }
+        try {
+            IndexedPropertyDescriptor i = new IndexedPropertyDescriptor("a",
+                    NormalBean.class, "getData", "setData",
+                    "getData", null);
+            i.setIndexedReadMethod(null);
+            fail("should throw IntrospectionException.");
+        } catch (IntrospectionException e) {
+            // expected
+        }
+    }
+
+    
+    public void testSetIndexedReadMethodFollowANullValue() throws Exception {
+        try {
+            IndexedPropertyDescriptor i = new IndexedPropertyDescriptor("a",
+                    DummyBean.class, "readMethod", "writeMethod", null,
+                    "indexedReadMethod");
+            Method irm = DummyBean.class.getDeclaredMethod("indexedReadMethod",
+                    Integer.TYPE);
+            i.setIndexedReadMethod(irm);
+            fail("should throw IntrospectionException.");
+        } catch (IntrospectionException e) {
+            // expected
+        }
+    }
+
+    static class DummyBean {
+
+        public int[] readMehtod() {
+            return null;
+        }
+
+        public void writeMethod(int[] a) {
+        }
+
+        public double indexedReadMethod(int i) {
+            return 0;
+        }
+
+        public void indexedWriteMethod(int i, int j) {
+        }
+
     }
 
     class NotJavaBean {
@@ -1407,5 +1471,114 @@
             this.propertyOne[i] = value;
         }
 
+    }
+    
+    //Regression Test
+    class InCompatibleGetterSetterBean
+    {
+        private Object[] data = new Object[10];
+        public void setData(Object[] data) {
+            this.data = data;
+        }
+        public Object[] getDate() {
+            return data;
+        }
+        public void setData(int index, Object o) {
+            this.data[index] = o;
+        }
+    }
+    
+    public void testInCompatibleGetterSetterBean() {
+        try {
+            new IndexedPropertyDescriptor("data",
+                    InCompatibleGetterSetterBean.class);
+            fail("should throw IntrospectionException");
+        } catch (IntrospectionException e) {
+            // expected
+        }
+    }
+    
+    class NormalBean {
+        private Object[] data = new Object[10];
+
+        public Object[] getData() {
+            return data;
+        }
+
+        public void setData(Object[] data) {
+            this.data = data;
+        }
+
+        public void setData(int index, Object o) {
+            data[index] = o;
+        }
+        
+        public void setData(int index, int value) {
+            // do nothing
+        }
+
+        public Object getData(int index) {
+            return data[index];
+        }
+    }
+    
+    public void testEquals_superClass() throws Exception {
+        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("data",
+                NormalBean.class);
+        IndexedPropertyDescriptor indexedPropertyDescriptor = new IndexedPropertyDescriptor(
+                "data", NormalBean.class);
+        assertFalse(indexedPropertyDescriptor.equals(propertyDescriptor));
+        assertTrue(propertyDescriptor.equals(indexedPropertyDescriptor));
+    }
+    
+    public void testHashCode() throws Exception {
+        String propertyName = "PropertyFour";
+        Class<MockJavaBean> beanClass = MockJavaBean.class;
+
+        Method readMethod = beanClass.getMethod("get" + propertyName,
+                (Class[]) null);
+        Method writeMethod = beanClass.getMethod("set" + propertyName,
+                new Class[] { String[].class });
+        Method indexedReadMethod = beanClass.getMethod("get" + propertyName,
+                new Class[] { Integer.TYPE });
+        Method indexedWriteMethod = beanClass.getMethod("set" + propertyName,
+                new Class[] { Integer.TYPE, String.class });
+
+        IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor(
+                propertyName, readMethod, writeMethod, indexedReadMethod,
+                indexedWriteMethod);
+
+        IndexedPropertyDescriptor ipd2 = new IndexedPropertyDescriptor(
+                propertyName, beanClass);
+
+        assertEquals(ipd, ipd2);
+        assertEquals(ipd.hashCode(), ipd2.hashCode());
+    }
+    
+    public void testIncompatibleGetterAndIndexedGetterBean() {
+        try {
+            new IndexedPropertyDescriptor("data",
+                    IncompatibleGetterAndIndexedGetterBean.class);
+            fail("should throw IntrospectionException");
+
+        } catch (IntrospectionException e) {
+            //expected
+        }
+    }
+
+    private class IncompatibleGetterAndIndexedGetterBean {
+        private int[] data;
+
+        public int getData() {
+            return data[0];
+        }
+
+        public int getData(int index) {
+            return data[index];
+        }
+        
+        public void setData(int index, int data) {
+            this.data[index] = data;
+        }
     }
 }



Mime
View raw message