harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r475867 - in /incubator/harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ test/java/org/apache/harmony/beans/tests/java/beans/
Date Thu, 16 Nov 2006 18:57:02 GMT
Author: ayza
Date: Thu Nov 16 10:57:01 2006
New Revision: 475867

URL: http://svn.apache.org/viewvc?view=rev&rev=475867
Log:
processing of inherited properties corrected; testIntrospection_5 was enabled

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/BeanInfoWrapper.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Introspector.java
    incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/BeanInfoWrapper.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/BeanInfoWrapper.java?view=diff&rev=475867&r1=475866&r2=475867
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/BeanInfoWrapper.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/BeanInfoWrapper.java
Thu Nov 16 10:57:01 2006
@@ -27,8 +27,10 @@
 
 class BeanInfoWrapper implements BeanInfo {
 
+    // external BeanInfo class if it was specified 
     private BeanInfo info;
 
+    // information gathered by means of reflection API
     private BeanInfoImpl impl;
 
     private BeanInfoWrapper parentBeanInfoWrapper;
@@ -42,6 +44,7 @@
         PropertyDescriptor[] result = null;
         PropertyDescriptor[] infoResult = null;
 
+        // if external BeanInfo class was specified
         if (info != null) {
             BeanInfo[] infos = info.getAdditionalBeanInfo();
 
@@ -59,6 +62,8 @@
             }
         }
 
+        // if no external BeanInfo class was specified or we fail to 
+        // to obtain the result on the previous step by some reason
         if (info == null || infoResult == null) {
             PropertyDescriptor[] implResult = impl.getPropertyDescriptors();
 
@@ -66,19 +71,8 @@
             if (parentBeanInfoWrapper != null) {
                 PropertyDescriptor[] parentResult = parentBeanInfoWrapper
                         .getPropertyDescriptors();
-                Map<String, FeatureDescriptor> hm = concatArraysUniqueByName(
-                        implResult, parentResult);
-
-                Collection<FeatureDescriptor> values = hm.values();
-                Iterator<FeatureDescriptor> iterator;
-                int idx = 0;
-
-                result = new PropertyDescriptor[values.size()];
-                iterator = values.iterator();
-                while (iterator.hasNext()) {
-                    result[idx++] = (PropertyDescriptor) iterator.next();
-                }
 
+                result = concatArraysToOneArray(implResult, parentResult);
                 Arrays.sort(result, new Comparator<PropertyDescriptor>() {
                     public int compare(PropertyDescriptor pd1,
                             PropertyDescriptor pd2) {
@@ -185,13 +179,12 @@
                         implResult, parentResult);
 
                 Collection<FeatureDescriptor> values = hm.values();
-                Iterator<FeatureDescriptor> iterator;
                 int idx = 0;
 
                 result = new EventSetDescriptor[values.size()];
-                iterator = values.iterator();
-                while (iterator.hasNext()) {
-                    result[idx++] = (EventSetDescriptor) iterator.next();
+                
+                for (FeatureDescriptor fd : values) {
+                    result[idx++] = (EventSetDescriptor) fd;
                 }
 
                 Arrays.sort(result, new Comparator<EventSetDescriptor>() {
@@ -277,7 +270,7 @@
         return result;
     }
 
-    void merge(BeanInfoWrapper parentBeanInfoWrapper) {
+    void setParentToMerge(BeanInfoWrapper parentBeanInfoWrapper) {
         this.parentBeanInfoWrapper = parentBeanInfoWrapper;
     }
 
@@ -305,20 +298,101 @@
     private static PropertyDescriptor[] concatArraysToOneArray(
             PropertyDescriptor[] childs, PropertyDescriptor[] parents) {
         if (childs != null || parents != null) {
-            Map<String, FeatureDescriptor> hm = concatArraysUniqueByName(
-                    childs, parents);
-            PropertyDescriptor[] result = new PropertyDescriptor[hm.size()];
-
-            Iterator<String> iterator = hm.keySet().iterator();
-            int idx = 0;
+            Map<String, PropertyDescriptor> resultHM =
+                    new HashMap<String, PropertyDescriptor>();
 
-            while (iterator.hasNext()) {
-                String name = iterator.next();
+            if (childs != null) {
+                for (PropertyDescriptor element : childs) {
+                    resultHM.put(element.getName(), element);
+                }
+            }
 
-                result[idx++] = (PropertyDescriptor) hm.get(name);
+            if (parents != null) {
+                for (PropertyDescriptor parentPD : parents) {
+                    PropertyDescriptor childPD =
+                            resultHM.get(parentPD.getName());
+                    
+                    if (childPD == null) {
+                        resultHM.put(parentPD.getName(), parentPD);
+                    } else {
+                        boolean childIsIndexed =
+                                childPD instanceof IndexedPropertyDescriptor; 
+                        boolean parentIsIndexed =
+                                parentPD instanceof IndexedPropertyDescriptor;
+                        boolean shouldBeMerged;
+                        Class<?> childPropType = childPD.getPropertyType();
+                        Class<?> childIdxPropType = childIsIndexed ?
+                               ((IndexedPropertyDescriptor) childPD)
+                                   .getIndexedPropertyType() : null;
+                        Class<?> parentPropType = parentPD.getPropertyType();
+                        Class<?> parentIdxPropType = parentIsIndexed ?
+                               ((IndexedPropertyDescriptor) parentPD)
+                                   .getIndexedPropertyType() : null;
+                        
+                        // additional consistency checks
+                        // for child
+                        assert (!childIsIndexed ? childPropType != null : true);
+                        assert (childIsIndexed ?
+                                childIdxPropType != null : true);
+                        assert (childIsIndexed ? childPropType == null ||
+                                childPropType.isArray() : true);
+                        assert (childIsIndexed && childPropType != null ?
+                                childPropType.getComponentType() ==
+                                        childIdxPropType : true);
+                        // for parent
+                        assert (!parentIsIndexed ?
+                                parentPropType != null : true);
+                        assert (parentIsIndexed ?
+                                parentIdxPropType != null : true);
+                        assert (parentIsIndexed ? parentPropType == null ||
+                                parentPropType.isArray() : true);
+                        assert (parentIsIndexed && parentPropType != null ?
+                                parentPropType.getComponentType() ==
+                                        parentIdxPropType : true);
+
+                        // let's check types
+                        shouldBeMerged = childPropType != null &&
+                                childPropType == parentPropType;
+                        if (childIsIndexed) {
+                            shouldBeMerged |=
+                                childPropType == null
+                                && parentPropType != null
+                                && parentPropType.isArray()
+                                && childIdxPropType ==
+                                    parentPropType.getComponentType();                  
             
+                        } 
+                        if (parentIsIndexed) {
+                            shouldBeMerged |=
+                                parentPropType == null
+                                && childPropType != null
+                                && childPropType.isArray()
+                                && parentIdxPropType ==
+                                    childPropType.getComponentType();                   
            
+                            
+                        }
+                        if (childIsIndexed && parentIsIndexed) {
+                            shouldBeMerged |=
+                                childIdxPropType == parentIdxPropType;
+                        }
+                        
+                        // merge if descriptors are compatible
+                        if (shouldBeMerged) {
+                            try {
+                                resultHM.put(parentPD.getName(),
+                                        mergePDs(childPD, parentPD));
+                            } catch (IntrospectionException e) {
+                                // should be logged
+                                //e.printStackTrace();
+                            }
+                        }
+                        // we should not do anything if parent is not
+                        // compatible with the child
+                    }
+                }
             }
 
-            return result;
+            return resultHM.values().toArray(
+                    new PropertyDescriptor[resultHM.size()]);
         }
 
         return null;
@@ -380,5 +454,87 @@
         }
 
         return null;
+    }
+    
+    /**
+     * Merges two property descriptors. 
+     * @param childPD child property descriptor
+     * @param parentPD parent property descriptor
+     * @return merged descriptor 
+     * XXX 
+     */
+    private static PropertyDescriptor mergePDs(PropertyDescriptor childPD,
+            PropertyDescriptor parentPD) throws IntrospectionException {
+
+        assert childPD != null;
+        assert childPD instanceof IndexedPropertyDescriptor ?
+                (childPD.getPropertyType() != null ||
+                        ((IndexedPropertyDescriptor) childPD)
+                                .getIndexedPropertyType() != null)
+                : childPD.getPropertyType() != null;
+        assert parentPD != null;
+        assert parentPD instanceof IndexedPropertyDescriptor ?
+                (parentPD.getPropertyType() != null ||
+                        ((IndexedPropertyDescriptor) parentPD)
+                                .getIndexedPropertyType() != null)
+                : parentPD.getPropertyType() != null;
+        
+        PropertyDescriptor result;
+
+        if (childPD instanceof IndexedPropertyDescriptor) {
+            result = new IndexedPropertyDescriptor(childPD.getName(),
+                    childPD.getReadMethod(), childPD.getWriteMethod(),
+                    ((IndexedPropertyDescriptor) childPD).getIndexedReadMethod(),
+                    ((IndexedPropertyDescriptor) childPD).getIndexedWriteMethod());
+        } else if (parentPD instanceof IndexedPropertyDescriptor) {
+            result = new IndexedPropertyDescriptor(childPD.getName(),
+                    childPD.getReadMethod(), childPD.getWriteMethod(),
+                    ((IndexedPropertyDescriptor) parentPD).getIndexedReadMethod(),
+                    ((IndexedPropertyDescriptor) parentPD).getIndexedWriteMethod());
+        } else {
+            result = new PropertyDescriptor(childPD.getName(),
+                    childPD.getReadMethod(), childPD.getWriteMethod());
+        }
+        copy(result, childPD);
+
+        if (result.getReadMethod() == null &&
+                parentPD.getReadMethod() != null) {
+            result.setReadMethod(parentPD.getReadMethod());
+        }
+        if (result.getWriteMethod() == null &&
+                parentPD.getWriteMethod() != null) {
+            result.setWriteMethod(parentPD.getWriteMethod());
+        }
+        
+        if (result instanceof IndexedPropertyDescriptor) {
+            IndexedPropertyDescriptor result2 =
+                    (IndexedPropertyDescriptor) result;
+            IndexedPropertyDescriptor parentPD2 =
+                    (IndexedPropertyDescriptor) parentPD;
+            
+            if (result2.getIndexedReadMethod() == null &&
+                    parentPD2.getIndexedReadMethod() != null) {
+                result2.setIndexedReadMethod(parentPD2.getIndexedReadMethod());
+            }
+            if (result2.getIndexedWriteMethod() == null &&
+                    parentPD2.getIndexedWriteMethod() != null) {
+                result2.setIndexedWriteMethod(parentPD2.getIndexedWriteMethod());
+            }
+        }
+
+        return result;
+    }
+    
+    private static void copy(PropertyDescriptor target, 
+            PropertyDescriptor source) throws IntrospectionException {
+        target.setBound(source.isBound());
+        target.setConstrained(source.isConstrained());
+        target.setDisplayName(source.getDisplayName());
+        target.setExpert(source.isExpert());
+        target.setHidden(source.isHidden());
+        target.setPreferred(source.isPreferred());
+        target.setPropertyEditorClass(source.getPropertyEditorClass());
+        target.setShortDescription(source.getShortDescription());
+        // XXX Attributes?
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java?view=diff&rev=475867&r1=475866&r2=475867
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/IndexedPropertyDescriptor.java
Thu Nov 16 10:57:01 2006
@@ -43,7 +43,7 @@
 
         super(propertyName, getter, setter);
         setIndexedReadMethod(indexedGetter);
-        setIndexedWriteMethod(indexedGetter);
+        setIndexedWriteMethod(indexedSetter);
     }
 
     public IndexedPropertyDescriptor(String propertyName, Class<?> beanClass)

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Introspector.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Introspector.java?view=diff&rev=475867&r1=475866&r2=475867
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Introspector.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Introspector.java
Thu Nov 16 10:57:01 2006
@@ -163,7 +163,7 @@
             BeanInfoWrapper parentBeanInfo = getBeanInfo(parent, stopClass,
                     ignoreSuperClassBeanInfo, ignoreSuperClassBeanInfo);
 
-            wrapper.merge(parentBeanInfo);
+            wrapper.setParentToMerge(parentBeanInfo);
         }
 
         // ...

Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java?view=diff&rev=475867&r1=475866&r2=475867
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/IntrospectorTest.java
Thu Nov 16 10:57:01 2006
@@ -824,6 +824,7 @@
         BeanInfo info = Introspector.getBeanInfo(beanClass);
         PropertyDescriptor[] pds;
 
+        
         assertEquals(0, info.getEventSetDescriptors().length);
         assertEquals(13, info.getMethodDescriptors().length);
 
@@ -831,6 +832,9 @@
         assertEquals(2, pds.length);
 
         for (PropertyDescriptor element : pds) {
+            System.out.println("QQQ " + element.getName() + " " +
+                    (element instanceof IndexedPropertyDescriptor));
+            
             if (element.getName().equals("class")) {
                 assertNull(element.getWriteMethod());
                 assertNotNull(element.getReadMethod());
@@ -884,10 +888,12 @@
     public void testIntrospection_7() throws IntrospectionException {
         Class<FakeFox501> beanClass = FakeFox501.class;
         BeanInfo info = Introspector.getBeanInfo(beanClass);
+        PropertyDescriptor[] propertyDesc;
+        
         assertEquals(0, info.getEventSetDescriptors().length);
         assertEquals(12, info.getMethodDescriptors().length);
 
-        PropertyDescriptor[] propertyDesc = info.getPropertyDescriptors();
+        propertyDesc = info.getPropertyDescriptors();
         assertEquals(2, propertyDesc.length);
 
         for (PropertyDescriptor element : propertyDesc) {
@@ -895,7 +901,9 @@
                 assertNull(element.getWriteMethod());
                 assertNotNull(element.getReadMethod());
             } else {
-                IndexedPropertyDescriptor indexedDesc = (IndexedPropertyDescriptor) element;
+                IndexedPropertyDescriptor indexedDesc =
+                        (IndexedPropertyDescriptor) element;
+                
                 assertEquals("fox501", element.getName());
                 assertEquals(String[].class.getName(), element
                         .getPropertyType().getName());
@@ -1414,7 +1422,7 @@
 //        TestSuite suite = new TestSuite();
         TestSuite suite = new TestSuite(IntrospectorTest.class);
         
-//        suite.addTest(new IntrospectorTest("testGetPropertyDescriptors_H1838"));
+//        suite.addTest(new IntrospectorTest("testIntrospection_7"));
         return suite;
     }
     



Mime
View raw message