harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r556829 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/PropertyDescriptor.java test/java/org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java
Date Tue, 17 Jul 2007 06:45:39 GMT
Author: tonywu
Date: Mon Jul 16 23:45:35 2007
New Revision: 556829

URL: http://svn.apache.org/viewvc?view=rev&rev=556829
Log:
Handle the exceptional case of the constructor without setter and getter.

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

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyDescriptor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyDescriptor.java?view=diff&rev=556829&r1=556828&r2=556829
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyDescriptor.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyDescriptor.java
Mon Jul 16 23:45:35 2007
@@ -20,7 +20,6 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
-import java.util.Vector;
 
 import org.apache.harmony.beans.BeansUtils;
 import org.apache.harmony.beans.internal.nls.Messages;
@@ -47,20 +46,22 @@
         }
         this.setName(propertyName);
         this.setDisplayName(propertyName);
-        if (setterName != null) {
-            if (hasMethod(beanClass, setterName)) {
-                setWriteMethod(beanClass, setterName);
-            } else {
-                throw new IntrospectionException(Messages.getString("beans.20")); //$NON-NLS-1$
-            }
-        }
         if (getterName != null) {
-            if (hasMethod(beanClass, getterName)) {
+            if(getterName.length() == 0){
+                throw new IntrospectionException("read or write method cannot be empty.");
//$NON-NLS-1$    
+            }
+            try{
                 setReadMethod(beanClass, getterName);
-            } else {
-                throw new IntrospectionException(Messages.getString("beans.1F")); //$NON-NLS-1$
+            }catch(IntrospectionException e){
+                setReadMethod(beanClass, createDefaultMethodName(propertyName, "get")); //$NON-NLS-1$
             }
         }
+        if (setterName != null) {
+            if(setterName.length() == 0){
+                throw new IntrospectionException("read or write method cannot be empty.");
//$NON-NLS-1$    
+            }
+            setWriteMethod(beanClass, setterName);
+        }
     }
 
     public PropertyDescriptor(String propertyName, Method getter, Method setter)
@@ -71,14 +72,12 @@
         }
         this.setName(propertyName);
         this.setDisplayName(propertyName);
-        setWriteMethod(setter);
         setReadMethod(getter);
+        setWriteMethod(setter);
     }
 
     public PropertyDescriptor(String propertyName, Class<?> beanClass)
             throws IntrospectionException {
-        String getterName;
-        String setterName;
         if (beanClass == null) {
             throw new IntrospectionException(Messages.getString("beans.03")); //$NON-NLS-1$
         }
@@ -87,22 +86,13 @@
         }
         this.setName(propertyName);
         this.setDisplayName(propertyName);
-        getterName = createDefaultMethodName(propertyName, "is"); //$NON-NLS-1$
-        if (hasMethod(beanClass, getterName)) {
-            setReadMethod(beanClass, getterName);
-        } else {
-            getterName = createDefaultMethodName(propertyName, "get"); //$NON-NLS-1$
-            if (hasMethod(beanClass, getterName)) {
-                setReadMethod(beanClass, getterName);
-            }
-        }
-        setterName = createDefaultMethodName(propertyName, "set"); //$NON-NLS-1$
-        if (hasMethod(beanClass, setterName)) {
-            setWriteMethod(beanClass, setterName);
-        }
-        if (getter == null && setter == null) {
-            throw new IntrospectionException(Messages.getString("beans.01", propertyName));
//$NON-NLS-1$
+        try {
+            setReadMethod(beanClass, createDefaultMethodName(propertyName, "is")); //$NON-NLS-1$
+        } catch (Exception e) {
+            setReadMethod(beanClass, createDefaultMethodName(propertyName, "get")); //$NON-NLS-1$
         }
+        
+        setWriteMethod(beanClass, createDefaultMethodName(propertyName, "set")); //$NON-NLS-1$
     }
 
     public void setWriteMethod(Method setter) throws IntrospectionException {
@@ -156,7 +146,7 @@
 
     @Override
     public boolean equals(Object object) {
-        boolean result = (object != null && object instanceof PropertyDescriptor);
+        boolean result = object instanceof PropertyDescriptor;
         if (result) {
             PropertyDescriptor pd = (PropertyDescriptor) object;
             boolean gettersAreEqual = (this.getter == null) && (pd.getReadMethod()
== null)
@@ -219,11 +209,6 @@
         return bound;
     }
 
-    boolean hasMethod(Class<?> beanClass, String methodName) {
-        Method[] methods = findMethods(beanClass, methodName);
-        return (methods.length > 0);
-    }
-
     String createDefaultMethodName(String propertyName, String prefix) {
         String result = null;
         if (propertyName != null) {
@@ -234,50 +219,33 @@
         return result;
     }
 
-    Method[] findMethods(Class<?> aClass, String methodName) {
-        Method[] allMethods = aClass.getMethods();
-        Vector<Method> matchedMethods = new Vector<Method>();
-        Method[] result;
-        for (Method method : allMethods) {
-            if (method.getName().equals(methodName)) {
-                matchedMethods.add(method);
-            }
-        }
-        result = new Method[matchedMethods.size()];
-        for (int j = 0; j < matchedMethods.size(); ++j) {
-            result[j] = matchedMethods.elementAt(j);
-        }
-        return result;
-    }
-
-    void setReadMethod(Class<?> beanClass, String getterName) {
-        boolean result = false;
-        Method[] getters = findMethods(beanClass, getterName);
-        for (Method element : getters) {
-            try {
-                setReadMethod(element);
-                result = true;
-            } catch (IntrospectionException ie) {
-            }
-            if (result) {
-                break;
-            }
+    void setReadMethod(Class<?> beanClass, String getterName) throws IntrospectionException
{
+        try {
+            Method readMethod = beanClass.getMethod(getterName, new Class[]{});
+            setReadMethod(readMethod);
+        } catch (Exception e) {
+            throw new IntrospectionException(e.getLocalizedMessage());
         }
     }
 
     void setWriteMethod(Class<?> beanClass, String setterName) throws IntrospectionException
{
-        boolean result = false;
-        Method[] setters = findMethods(beanClass, setterName);
-        for (Method element : setters) {
-            try {
-                setWriteMethod(element);
-                result = true;
-            } catch (IntrospectionException ie) {
-            }
-            if (result) {
-                break;
+        Method writeMethod = null;
+        try {
+            if(getter != null){
+                writeMethod = beanClass.getMethod(setterName, new Class[]{getter.getReturnType()});
+            }else{
+                Method[] methods = beanClass.getMethods();
+                for (Method method : methods) {
+                    if(method.getName().equals(setterName)){
+                        writeMethod = method;
+                        break;
+                    }
+                }
             }
+        } catch (Exception e) {
+            throw new IntrospectionException(e.getLocalizedMessage());
         }
+        setWriteMethod(writeMethod);
     }
 
     public PropertyEditor createPropertyEditor(Object bean) {

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java?view=diff&rev=556829&r1=556828&r2=556829
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyDescriptorTest.java
Mon Jul 16 23:45:35 2007
@@ -232,8 +232,75 @@
         assertFalse(pd.isExpert());
         assertFalse(pd.isHidden());
         assertFalse(pd.isPreferred());
-    }
+        
+        propertyName = "propertyWithoutGet";
+        try{
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        try{
+            new PropertyDescriptor(propertyName, beanClass, "getPropertyWithoutGet", "setPropertyWithoutGet");
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        propertyName = "propertyWithoutSet";
+        beanClass = MockJavaBean.class;
+        try{
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        propertyName = "propertyWithDifferentGetSet";
+        try{
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        propertyName = "propertyWithInvalidGet";
+            new PropertyDescriptor(propertyName, beanClass);
+        
+        propertyName = "propertyWithoutPublicGet";
+        try{
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        propertyName = "propertyWithGet1Param";
+        try{
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        }catch(IntrospectionException e){
+        }
+        
+        propertyName = "propertyWithIs1Param";
+        PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, beanClass);
+        assertEquals("getPropertyWithIs1Param", pd2.getReadMethod().getName());
+
+        propertyName = "propertyWithSet2Param";
+        try {
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        } catch (IntrospectionException e) {
+        }
 
+        propertyName = "propertyWithIsGet";
+        PropertyDescriptor pd3 = new PropertyDescriptor(propertyName, beanClass);
+        assertEquals("isPropertyWithIsGet", pd3.getReadMethod().getName());
+
+        propertyName = "propertyWithVoidGet";
+        try {
+            new PropertyDescriptor(propertyName, beanClass);
+            fail("Should throw IntrospectionException");
+        } catch (IntrospectionException e) {
+        }
+    }
+    
     public void testPropertyDescriptorStringClass_PropertyNameCapital()
             throws IntrospectionException {
         String propertyName = "PropertyOne";
@@ -369,18 +436,23 @@
         assertFalse(pd.isExpert());
         assertFalse(pd.isHidden());
         assertFalse(pd.isPreferred());
+        
+        try{
+            pd = new PropertyDescriptor(propertyName, beanClass,
+                "", "set" + propertyName);
+            fail("should throw exception");
+        }catch(IntrospectionException e){
+        }
     }
 
     public void testPropertyDescriptorStringClassStringString_ReadMethodInvalid()
             throws IntrospectionException {
-        String propertyName = "PropertyTwo";
+        String propertyName = "booleanProperty";
         Class<MockJavaBean> beanClass = MockJavaBean.class;
-        try {
-            new PropertyDescriptor(propertyName, beanClass, "getXX", "set"
-                    + propertyName);
-            fail("Should throw IntrospectionException.");
-        } catch (IntrospectionException e) {
-        }
+        PropertyDescriptor pd = new PropertyDescriptor(propertyName, beanClass, "getXX",
"set"
+                + propertyName);
+        assertEquals("getBooleanProperty", pd.getReadMethod().getName());
+        assertEquals("setbooleanProperty", pd.getWriteMethod().getName());
     }
 
     public void testPropertyDescriptorStringClassStringString_WriteMethodNull()



Mime
View raw message