[exo-jcr-commits] exo-jcr SVN: r2977 - ws/branches/2.1.x/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 11:08:32 EDT 2010


Author: aparfonov
Date: 2010-08-23 11:08:31 -0400 (Mon, 23 Aug 2010)
New Revision: 2977

Modified:
   ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
   ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
Log:
EXOJCR-890

Modified: ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
===================================================================
--- ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java	2010-08-23 14:28:10 UTC (rev 2976)
+++ ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java	2010-08-23 15:08:31 UTC (rev 2977)
@@ -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/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
===================================================================
--- ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java	2010-08-23 14:28:10 UTC (rev 2976)
+++ ws/branches/2.1.x/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java	2010-08-23 15:08:31 UTC (rev 2977)
@@ -37,6 +37,7 @@
 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;
@@ -48,6 +49,18 @@
 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}
     */
@@ -61,25 +74,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)
          {
@@ -150,7 +167,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