You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shale.apache.org by cr...@apache.org on 2006/09/13 07:57:56 UTC

svn commit: r442859 - /shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java

Author: craigmcc
Date: Tue Sep 12 22:57:56 2006
New Revision: 442859

URL: http://svn.apache.org/viewvc?view=rev&rev=442859
Log:
Eliminate direct Commons BeanUtils dependencies in shale-test module.

SHALE-280

Modified:
    shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java

Modified: shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java?view=diff&rev=442859&r1=442858&r2=442859
==============================================================================
--- shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java (original)
+++ shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/mock/MockPropertyResolver.java Tue Sep 12 22:57:56 2006
@@ -16,15 +16,16 @@
 
 package org.apache.shale.test.mock;
 
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
-
 import javax.faces.el.EvaluationException;
 import javax.faces.el.PropertyNotFoundException;
 import javax.faces.el.PropertyResolver;
 
-import org.apache.commons.beanutils.PropertyUtils;
-
 /**
  * <p>Mock implementation of <code>PropertyResolver</code>.</p>
  *
@@ -60,24 +61,17 @@
         if (base == null) {
             throw new NullPointerException();
         }
+        if (base instanceof Map) {
+            return ((Map) base).get(property);
+        }
         String name = property.toString();
+        PropertyDescriptor descriptor = descriptor(base.getClass(), name);
         try {
-            if (base instanceof Map) {
-                Map map = (Map) base;
-                if (map.containsKey(name)) {
-                    return map.get(name);
-                } else {
-                    throw new PropertyNotFoundException(name);
-                }
-            } else {
-                return PropertyUtils.getSimpleProperty(base, name);
-            }
+            return descriptor.getReadMethod().invoke(base, (Object[]) null);
         } catch (IllegalAccessException e) {
             throw new EvaluationException(e);
         } catch (InvocationTargetException e) {
             throw new EvaluationException(e.getTargetException());
-        } catch (NoSuchMethodException e) {
-            throw new PropertyNotFoundException(name);
         }
 
     }
@@ -99,19 +93,18 @@
         if (base == null) {
             throw new NullPointerException();
         }
+        if (base instanceof Map) {
+            ((Map) base).put(property, value);
+            return;
+        }
         String name = property.toString();
+        PropertyDescriptor descriptor = descriptor(base.getClass(), name);
         try {
-            if (base instanceof Map) {
-                ((Map) base).put(name, value);
-            } else {
-                PropertyUtils.setSimpleProperty(base, name, value);
-            }
+            descriptor.getWriteMethod().invoke(base, new Object[] { value });
         } catch (IllegalAccessException e) {
             throw new EvaluationException(e);
         } catch (InvocationTargetException e) {
             throw new EvaluationException(e.getTargetException());
-        } catch (NoSuchMethodException e) {
-            throw new PropertyNotFoundException(name);
         }
 
     }
@@ -133,8 +126,12 @@
         if (base == null) {
             throw new NullPointerException();
         }
+        if (base instanceof Map) {
+            return false; // We have no way to know anything more specific
+        }
         String name = property.toString();
-        return !PropertyUtils.isWriteable(base, name);
+        PropertyDescriptor descriptor = descriptor(base.getClass(), name);
+        return (descriptor.getWriteMethod() == null);
 
     }
 
@@ -155,16 +152,17 @@
         if (base == null) {
             throw new NullPointerException();
         }
-        String name = property.toString();
-        try {
-            return PropertyUtils.getPropertyType(base, name);
-        } catch (IllegalAccessException e) {
-            throw new EvaluationException(e);
-        } catch (InvocationTargetException e) {
-            throw new EvaluationException(e.getTargetException());
-        } catch (NoSuchMethodException e) {
-            throw new PropertyNotFoundException(name);
+        if (base instanceof Map) {
+            Object value = ((Map) base).get(property);
+            if (value != null) {
+                return value.getClass();
+            } else {
+                return Object.class;
+            }
         }
+        String name = property.toString();
+        PropertyDescriptor descriptor = descriptor(base.getClass(), name);
+        return descriptor.getPropertyType();
 
     }
 
@@ -174,6 +172,44 @@
         throws PropertyNotFoundException {
 
         return getType(base, "" + index);
+
+    }
+
+
+    // --------------------------------------------------------- Private Methods
+
+
+    /**
+     * <p>Return the <code>PropertyDescriptor</code> for the specified
+     * property of the specified class.</p>
+     *
+     * @param clazz Class from which to extract a property descriptor
+     * @param name Name of the desired property
+     *
+     * @exception EvaluationException if we cannot access the introspecition
+     *  information for this class
+     * @exception PropertyNotFoundException if the specified property does
+     *  not exist on the specified class
+     */
+    private PropertyDescriptor descriptor(Class clazz, String name) {
+
+        System.err.println("descriptor(class=" + clazz.getName() + ", name=" + name);
+        BeanInfo info = null;
+        try {
+            info = Introspector.getBeanInfo(clazz);
+            System.err.println("  Found BeanInfo " + info);
+        } catch (IntrospectionException e) {
+            throw new EvaluationException(e);
+        }
+        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
+        for (int i = 0; i < descriptors.length; i++) {
+            if (name.equals(descriptors[i].getName())) {
+                System.err.print("  Found PropertyDescriptor " + descriptors[i]);
+                return descriptors[i];
+            }
+        }
+        System.err.print("  No property descriptor for property " + name);
+        throw new PropertyNotFoundException(name);
 
     }