You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/12 07:34:02 UTC

svn commit: r773789 - /incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java

Author: tvolkert
Date: Tue May 12 05:34:02 2009
New Revision: 773789

URL: http://svn.apache.org/viewvc?rev=773789&view=rev
Log:
Refactored BeanDictionary to provide some static methods that were previously instance methods (instance method versions now call into the static methods)

Modified:
    incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java

Modified: incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java?rev=773789&r1=773788&r2=773789&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java (original)
+++ incubator/pivot/trunk/core/src/pivot/beans/BeanDictionary.java Tue May 12 05:34:02 2009
@@ -286,11 +286,7 @@
      * <tt>true</tt> if the property is read-only; <tt>false</tt>, otherwise.
      */
     public boolean isReadOnly(String key) {
-        if (key == null) {
-            throw new IllegalArgumentException("key is null.");
-        }
-
-        return (getSetterMethod(key, getType(key)) == null);
+        return isReadOnly(bean.getClass(), key);
     }
 
     /**
@@ -303,13 +299,7 @@
      * The type of the property.
      */
     public Class<?> getType(String key) {
-        if (key == null) {
-            throw new IllegalArgumentException("key is null.");
-        }
-
-        Method getterMethod = getGetterMethod(key);
-
-        return (getterMethod == null) ? null : getterMethod.getReturnType();
+        return getType(bean.getClass(), key);
     }
 
     /**
@@ -332,8 +322,78 @@
      * The getter method, or <tt>null</tt> if the method does not exist.
      */
     private Method getGetterMethod(String key) {
-        Class<?> type = bean.getClass();
+        return getGetterMethod(bean.getClass(), key);
+    }
+
+    /**
+     * Returns the setter method for a property.
+     *
+     * @param key
+     * The property name.
+     *
+     * @return
+     * The getter method, or <tt>null</tt> if the method does not exist.
+     */
+    private Method getSetterMethod(String key, Class<?> valueType) {
+        return getSetterMethod(bean.getClass(), key, valueType);
+    }
 
+    /**
+     * Tests the read-only state of a property.
+     *
+     * @param type
+     * The bean class.
+     *
+     * @param key
+     * The property name.
+     *
+     * @return
+     * <tt>true</tt> if the property is read-only; <tt>false</tt>, otherwise.
+     */
+    public static boolean isReadOnly(Class<?> type, String key) {
+        if (key == null) {
+            throw new IllegalArgumentException("key is null.");
+        }
+
+        return (getSetterMethod(type, key, getType(type, key)) == null);
+    }
+
+    /**
+     * Returns the type of a property.
+     *
+     * @param type
+     * The bean class.
+     *
+     * @param key
+     * The property name.
+     *
+     * @return
+     * The type of the property, or <tt>null</tt> if no such bean property
+     * exists.
+     */
+    public static Class<?> getType(Class<?> type, String key) {
+        if (key == null) {
+            throw new IllegalArgumentException("key is null.");
+        }
+
+        Method getterMethod = getGetterMethod(type, key);
+
+        return (getterMethod == null) ? null : getterMethod.getReturnType();
+    }
+
+    /**
+     * Returns the getter method for a property.
+     *
+     * @param type
+     * The bean class.
+     *
+     * @param key
+     * The property name.
+     *
+     * @return
+     * The getter method, or <tt>null</tt> if the method does not exist.
+     */
+    public static Method getGetterMethod(Class<?> type, String key) {
         // Upper-case the first letter
         key = Character.toUpperCase(key.charAt(0)) + key.substring(1);
         Method method = null;
@@ -358,14 +418,16 @@
     /**
      * Returns the setter method for a property.
      *
+     * @param type
+     * The bean class.
+     *
      * @param key
      * The property name.
      *
      * @return
      * The getter method, or <tt>null</tt> if the method does not exist.
      */
-    private Method getSetterMethod(String key, Class<?> valueType) {
-        Class<?> type = bean.getClass();
+    public static Method getSetterMethod(Class<?> type, String key, Class<?> valueType) {
         Method method = null;
 
         if (valueType != null) {
@@ -383,7 +445,7 @@
             if (method == null) {
                 // Look for a match on the value's super type
                 Class<?> superType = valueType.getSuperclass();
-                method = getSetterMethod(key, superType);
+                method = getSetterMethod(type, key, superType);
             }
 
             if (method == null) {
@@ -391,7 +453,7 @@
                 // signature with the corresponding primitive type
                 try {
                     Field primitiveTypeField = valueType.getField("TYPE");
-                    Class<?> primitiveValueType = (Class<?>)primitiveTypeField.get(this);
+                    Class<?> primitiveValueType = (Class<?>)primitiveTypeField.get(null);
 
                     try {
                         method = type.getMethod(methodName, new Class<?>[] {primitiveValueType});
@@ -413,7 +475,7 @@
                 while (method == null
                     && i < n) {
                     Class<?> interfaceType = interfaces[i++];
-                    method = getSetterMethod(key, interfaceType);
+                    method = getSetterMethod(type, key, interfaceType);
                 }
             }
         }