You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/07/06 22:05:46 UTC
svn commit: r961002 - in /pivot/trunk/core/src/org/apache/pivot:
beans/BeanAdapter.java json/JSON.java
Author: gbrown
Date: Tue Jul 6 20:05:46 2010
New Revision: 961002
URL: http://svn.apache.org/viewvc?rev=961002&view=rev
Log:
Resolve PIVOT-554.
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java
pivot/trunk/core/src/org/apache/pivot/json/JSON.java
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java?rev=961002&r1=961001&r2=961002&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java Tue Jul 6 20:05:46 2010
@@ -170,6 +170,10 @@ public class BeanAdapter implements Map<
* The bean object to wrap.
*/
public BeanAdapter(Object bean, boolean ignoreReadOnlyProperties) {
+ if (bean == null) {
+ throw new IllegalArgumentException();
+ }
+
this.bean = bean;
this.ignoreReadOnlyProperties = ignoreReadOnlyProperties;
}
@@ -196,14 +200,14 @@ public class BeanAdapter implements Map<
*/
@Override
public Object get(String key) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
Object value = null;
if (key.startsWith(FIELD_PREFIX)) {
@@ -256,14 +260,14 @@ public class BeanAdapter implements Map<
*/
@Override
public Object put(String key, Object value) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
if (key.startsWith(FIELD_PREFIX)) {
Field field = getField(key.substring(1));
@@ -295,8 +299,11 @@ public class BeanAdapter implements Map<
if (setterMethod == null) {
// Get the property type and attempt to coerce the value to it
Class<?> propertyType = getType(key);
- setterMethod = getSetterMethod(key, propertyType);
- value = coerce(value, propertyType);
+
+ if (propertyType != null) {
+ setterMethod = getSetterMethod(key, propertyType);
+ value = coerce(value, propertyType);
+ }
}
if (setterMethod == null) {
@@ -351,14 +358,14 @@ public class BeanAdapter implements Map<
*/
@Override
public boolean containsKey(String key) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
boolean containsKey;
if (key.startsWith(FIELD_PREFIX)) {
@@ -412,10 +419,6 @@ public class BeanAdapter implements Map<
* <tt>true</tt> if the property is read-only; <tt>false</tt>, otherwise.
*/
public boolean isReadOnly(String key) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
return isReadOnly(bean.getClass(), key);
}
@@ -429,10 +432,6 @@ public class BeanAdapter implements Map<
* #getType(Class, String)
*/
public Class<?> getType(String key) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
return getType(bean.getClass(), key);
}
@@ -446,10 +445,6 @@ public class BeanAdapter implements Map<
* #getGenericType(Class, String)
*/
public Type getGenericType(String key) {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
return getGenericType(bean.getClass(), key);
}
@@ -461,10 +456,6 @@ public class BeanAdapter implements Map<
*/
@Override
public Iterator<String> iterator() {
- if (bean == null) {
- throw new IllegalStateException("bean is null.");
- }
-
return new PropertyIterator();
}
@@ -511,6 +502,10 @@ public class BeanAdapter implements Map<
* non-public or static
*/
private Field getField(String fieldName) {
+ if (fieldName == null) {
+ throw new IllegalArgumentException("fieldName is null.");
+ }
+
return getField(bean.getClass(), fieldName);
}
@@ -519,7 +514,7 @@ public class BeanAdapter implements Map<
* exists, this method will return <tt>true</tt> (it will <u>not</u> throw
* an exception).
*
- * @param type
+ * @param beanClass
* The bean class.
*
* @param key
@@ -528,22 +523,30 @@ public class BeanAdapter implements Map<
* @return
* <tt>true</tt> if the property is read-only; <tt>false</tt>, otherwise.
*/
- public static boolean isReadOnly(Class<?> type, String key) {
+ public static boolean isReadOnly(Class<?> beanClass, String key) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
boolean isReadOnly = true;
if (key.startsWith(FIELD_PREFIX)) {
- Field field = getField(type, key.substring(1));
+ Field field = getField(beanClass, key.substring(1));
if (field != null) {
isReadOnly = ((field.getModifiers() & Modifier.FINAL) != 0);
}
} else {
- Method getterMethod = getGetterMethod(type, key);
+ Method getterMethod = getGetterMethod(beanClass, key);
if (getterMethod != null) {
- Method setterMethod = getSetterMethod(type, key, getType(type, key));
+ Method setterMethod = getSetterMethod(beanClass, key, getType(beanClass, key));
isReadOnly = (setterMethod == null);
}
}
@@ -565,10 +568,18 @@ public class BeanAdapter implements Map<
* exists.
*/
public static Class<?> getType(Class<?> beanClass, String key) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
Class<?> type = null;
if (key.startsWith(FIELD_PREFIX)) {
@@ -604,10 +615,18 @@ public class BeanAdapter implements Map<
* an instance of {@link java.lang.Class} will be returned.
*/
public static Type getGenericType(Class<?> beanClass, String key) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
if (key == null) {
throw new IllegalArgumentException("key is null.");
}
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
Type genericType = null;
if (key.startsWith(FIELD_PREFIX)) {
@@ -631,21 +650,33 @@ public class BeanAdapter implements Map<
* Returns the public, non-static fields for a property. Note that fields
* will only be consulted for bean properties after bean methods.
*
- * @param type
- * The bean class
+ * @param beanClass
+ * The bean class.
*
- * @param fieldName
- * The property name
+ * @param key
+ * The property name.
*
* @return
* The field, or <tt>null</tt> if the field does not exist, or is
* non-public or static.
*/
- public static Field getField(Class<?> type, String fieldName) {
+ public static Field getField(Class<?> beanClass, String key) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
+ if (key == null) {
+ throw new IllegalArgumentException("key is null.");
+ }
+
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
Field field = null;
try {
- field = type.getField(fieldName);
+ field = beanClass.getField(key);
int modifiers = field.getModifiers();
@@ -674,6 +705,18 @@ public class BeanAdapter implements Map<
* The getter method, or <tt>null</tt> if the method does not exist.
*/
public static Method getGetterMethod(Class<?> beanClass, String key) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
+ if (key == null) {
+ throw new IllegalArgumentException("key is null.");
+ }
+
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
// Upper-case the first letter
key = Character.toUpperCase(key.charAt(0)) + key.substring(1);
Method getterMethod = null;
@@ -708,6 +751,18 @@ public class BeanAdapter implements Map<
* The getter method, or <tt>null</tt> if the method does not exist.
*/
public static Method getSetterMethod(Class<?> beanClass, String key, Class<?> valueType) {
+ if (beanClass == null) {
+ throw new IllegalArgumentException("beanClass is null.");
+ }
+
+ if (key == null) {
+ throw new IllegalArgumentException("key is null.");
+ }
+
+ if (key.length() == 0) {
+ throw new IllegalArgumentException("key is empty.");
+ }
+
Method setterMethod = null;
if (valueType != null) {
Modified: pivot/trunk/core/src/org/apache/pivot/json/JSON.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSON.java?rev=961002&r1=961001&r2=961002&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/json/JSON.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/json/JSON.java Tue Jul 6 20:05:46 2010
@@ -31,14 +31,15 @@ public class JSON {
* Returns the value at the given path.
*
* @param root
- * The root object; must be an instance of {@link org.apache.pivot.collections.Map}
- * or {@link org.apache.pivot.collections.List}.
+ * The root object.
*
* @param path
- * The path to the value, in JavaScript path notation.
+ * The path to the value as a JavaScript path.
*
* @return
* The value at the given path.
+ *
+ * @see #get(Object, Sequence)
*/
public static Object get(Object root, String path) {
if (root == null) {
@@ -49,11 +50,24 @@ public class JSON {
throw new IllegalArgumentException("path is null.");
}
- return get(root, split(path));
+ return get(root, parse(path));
}
+ /**
+ * Returns the value at the given path.
+ *
+ * @param root
+ * The root object; must be an instance of {@link org.apache.pivot.collections.Map}
+ * or {@link org.apache.pivot.collections.List} or a Java bean object.
+ *
+ * @param keys
+ * The path to the value, as a set of keys.
+ *
+ * @return
+ * The value at the given path.
+ */
@SuppressWarnings("unchecked")
- private static Object get(Object root, Sequence<String> keys) {
+ public static Object get(Object root, Sequence<String> keys) {
Object value = root;
for (int i = 0, n = keys.getLength(); i < n; i++) {
@@ -231,7 +245,7 @@ public class JSON {
Object previousValue;
- Sequence<String> keys = split(path);
+ Sequence<String> keys = parse(path);
if (keys.getLength() == 0) {
throw new IllegalArgumentException("Bad path.");
}
@@ -277,7 +291,7 @@ public class JSON {
Object previousValue;
- Sequence<String> keys = split(path);
+ Sequence<String> keys = parse(path);
if (keys.getLength() == 0) {
throw new IllegalArgumentException("Bad path.");
}
@@ -323,7 +337,7 @@ public class JSON {
boolean containsKey;
- Sequence<String> keys = split(path);
+ Sequence<String> keys = parse(path);
if (keys.getLength() == 0) {
throw new IllegalArgumentException("Bad path.");
}
@@ -348,7 +362,12 @@ public class JSON {
return containsKey;
}
- private static Sequence<String> split(String path) {
+ /**
+ * Parses a JSON path into a sequence of string keys.
+ *
+ * @param path
+ */
+ public static Sequence<String> parse(String path) {
ArrayList<String> keys = new ArrayList<String>();
int i = 0;