[exo-jcr-commits] exo-jcr SVN: r2976 - ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl.
do-not-reply at jboss.org
do-not-reply at jboss.org
Mon Aug 23 10:28:11 EDT 2010
Author: aparfonov
Date: 2010-08-23 10:28:10 -0400 (Mon, 23 Aug 2010)
New Revision: 2976
Modified:
ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
Log:
EXOJCR-890
Modified: ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java 2010-08-23 13:30:30 UTC (rev 2975)
+++ ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java 2010-08-23 14:28:10 UTC (rev 2976)
@@ -44,6 +44,16 @@
public class BeanBuilder
{
+ static final Collection<String> SKIP_METHODS = new HashSet<String>();
+
+ static
+ {
+ // Since we need support for Groovy must skip this.
+ // All "Groovy Objects" implements interface groovy.lang.GroovyObject
+ // and has method setMetaClass. Not need to process it.
+ SKIP_METHODS.add("setMetaClass");
+ }
+
/**
* Create Java Bean from Json Source.
*
@@ -67,21 +77,14 @@
for (Method method : methods)
{
-
String methodName = method.getName();
+ Class<?>[] parameterTypes = method.getParameterTypes();
- if (methodName.startsWith("set") && methodName.length() > "set".length())
+ if (!SKIP_METHODS.contains(methodName) && methodName.startsWith("set") && parameterTypes.length == 1
+ && methodName.length() > 3)
{
- Class<?>[] parameterTypes = method.getParameterTypes();
-
- if (parameterTypes.length != 1)
- {
- throw new JsonException("Each set method must have one parameters, but method " + clazz.getName() + "#"
- + method.getName() + " has " + parameterTypes.length);
- }
-
Class<?> methodParameterClazz = parameterTypes[0];
- String key = methodName.substring("set".length());
+ String key = methodName.substring(3);
// first letter to lower case
key = (key.length() > 1) ? Character.toLowerCase(key.charAt(0)) + key.substring(1) : key.toLowerCase();
Modified: ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java 2010-08-23 13:30:30 UTC (rev 2975)
+++ ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java 2010-08-23 14:28:10 UTC (rev 2976)
@@ -18,6 +18,17 @@
*/
package org.exoplatform.ws.frameworks.json.impl;
+import org.exoplatform.ws.frameworks.json.JsonGenerator;
+import org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types;
+import org.exoplatform.ws.frameworks.json.value.JsonValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
+import org.exoplatform.ws.frameworks.json.value.impl.BooleanValue;
+import org.exoplatform.ws.frameworks.json.value.impl.DoubleValue;
+import org.exoplatform.ws.frameworks.json.value.impl.LongValue;
+import org.exoplatform.ws.frameworks.json.value.impl.NullValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ObjectValue;
+import org.exoplatform.ws.frameworks.json.value.impl.StringValue;
+
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -26,21 +37,11 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import org.exoplatform.ws.frameworks.json.JsonGenerator;
-import org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types;
-import org.exoplatform.ws.frameworks.json.value.JsonValue;
-import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
-import org.exoplatform.ws.frameworks.json.value.impl.BooleanValue;
-import org.exoplatform.ws.frameworks.json.value.impl.DoubleValue;
-import org.exoplatform.ws.frameworks.json.value.impl.LongValue;
-import org.exoplatform.ws.frameworks.json.value.impl.NullValue;
-import org.exoplatform.ws.frameworks.json.value.impl.ObjectValue;
-import org.exoplatform.ws.frameworks.json.value.impl.StringValue;
-
/**
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id: JsonGeneratorImpl.java 34417 2009-07-23 14:42:56Z dkatayev $
@@ -48,8 +49,25 @@
public class JsonGeneratorImpl implements JsonGenerator
{
+ static final Collection<String> SKIP_METHODS = new HashSet<String>();
+
+ static
+ {
+ // Prevent discovering of Java class.
+ SKIP_METHODS.add("getClass");
+ // Since we need support for Groovy must skip this.
+ // All "Groovy Objects" implements interface groovy.lang.GroovyObject
+ // and has method getMetaClass. Not need to discover it.
+ SKIP_METHODS.add("getMetaClass");
+ }
+
/**
- * {@inheritDoc}
+ * Create JSON array from specified collection.
+ *
+ * @param collection source collection
+ * @return JSON representation of collection
+ * @throws JsonException if collection can't be transformed in JSON
+ * representation
*/
public JsonValue createJsonArray(Collection<?> collection) throws JsonException
{
@@ -61,7 +79,7 @@
{
// If :
// 1. Known types (primitive, String, array of primitive or String)
- // 2. Array of any object (expect for Java Bean)
+ // 2. Array of any object (expect for Java Bean)
// 3. Collection<?>
// 4. Map<String, ?>
if (JsonUtils.getType(o) != null)
@@ -73,7 +91,12 @@
}
/**
- * {@inheritDoc}
+ * Create JSON array from specified object. Parameter <code>array</code> must
+ * be array.
+ *
+ * @param array source array
+ * @return JSON representation of array
+ * @throws JsonException if array can't be transformed in JSON representation
*/
public JsonValue createJsonArray(Object array) throws JsonException
{
@@ -135,7 +158,7 @@
Object el = Array.get(array, i);
// If :
// 1. Known types (primitive, String, array of primitive or String)
- // 2. Array of any object (expect for Java Bean)
+ // 2. Array of any object (expect for Java Bean)
// 3. Collection<?>
// 4. Map<String, ?>
if (JsonUtils.getType(el) != null)
@@ -151,6 +174,13 @@
return jsonArray;
}
+ /**
+ * Create JSON object from specified map.
+ *
+ * @param map source map
+ * @return JSON representation of map
+ * @throws JsonException if map can't be transformed in JSON representation
+ */
public JsonValue createJsonObject(Map<String, Object> map) throws JsonException
{
if (map == null)
@@ -163,7 +193,7 @@
Object o = map.get(k);
// If :
// 1. Known types (primitive, String, array of primitive or String)
- // 2. Array of any object (expect for Java Bean)
+ // 2. Array of any object (expect for Java Bean)
// 3. Collection<?>
// 4. Map<String, ?>
if (JsonUtils.getType(o) != null)
@@ -187,25 +217,29 @@
for (Method method : methods)
{
- String name = method.getName();
+ String methodName = method.getName();
/*
- * Method must be as follow: 1. Name starts from "get" plus at least one
- * character or starts from "is" plus one more character and return
- * boolean type; 2. Must be without parameters; 3. Not "getClass" method;
+ * Method must be as follow:
+ * 1. Name starts from "get" plus at least one character or starts from
+ * "is" plus at least one more character and return boolean type
+ * 2. Must be without parameters
+ * 3. Must not be in list of skipped methods
*/
+
String key = null;
- if (name.startsWith("get") && name.length() > 3 && method.getParameterTypes().length == 0
- && !"getClass".equals(name))
+ if (!SKIP_METHODS.contains(methodName) && method.getParameterTypes().length == 0)
{
- key = name.substring(3);
+ if (methodName.startsWith("get") && methodName.length() > 3)
+ {
+ key = methodName.substring(3);
+ }
+ else if (methodName.startsWith("is") && methodName.length() > 2
+ && (method.getReturnType() == Boolean.class || method.getReturnType() == boolean.class))
+ {
+ key = methodName.substring(2);
+ }
}
- else if (name.startsWith("is") && name.length() > 2
- && (method.getReturnType() == Boolean.class || method.getReturnType() == boolean.class)
- && method.getParameterTypes().length == 0)
- {
- key = name.substring(2);
- }
if (key != null)
{
@@ -221,7 +255,7 @@
// If :
// 1. Known types (primitive, String, array of primitive or String)
- // 2. Array of any object (expect for Java Bean)
+ // 2. Array of any object (expect for Java Bean)
// 3. Collection<?>
// 4. Map<String, ?>
if (JsonUtils.getType(invokeResult) != null)
@@ -281,7 +315,7 @@
return new StringValue(Character.toString((Character)object));
case STRING :
return new StringValue((String)object);
- case ENUM:
+ case ENUM :
return new StringValue(((Enum)object).name());
case ARRAY_BOOLEAN : {
JsonValue jsonArray = new ArrayValue();
More information about the exo-jcr-commits
mailing list