You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by aw...@apache.org on 2009/04/07 20:37:47 UTC

svn commit: r762897 - in /incubator/shindig/trunk/java: common/src/main/java/org/apache/shindig/common/ common/src/test/java/org/apache/shindig/common/ gadgets/src/main/java/org/apache/shindig/gadgets/preload/

Author: awiner
Date: Tue Apr  7 18:37:46 2009
New Revision: 762897

URL: http://svn.apache.org/viewvc?rev=762897&view=rev
Log:
Move getProperty() method off of JsonSerializer and onto new JsonUtil class.
Add a test of excluded properties.

Added:
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java   (with props)
    incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java   (with props)
Modified:
    incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
    incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelineExecutor.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelinedDataPreloader.java

Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java?rev=762897&r1=762896&r2=762897&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java Tue Apr  7 18:37:46 2009
@@ -19,12 +19,6 @@
 package org.apache.shindig.common;
 
 import org.apache.shindig.common.util.DateUtil;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.MapMaker;
-import com.google.common.collect.Maps;
-
 import org.joda.time.DateTime;
 import org.json.JSONArray;
 import org.json.JSONObject;
@@ -36,7 +30,6 @@
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * Serializes a JSONObject.
@@ -58,11 +51,6 @@
     '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
   };
 
-  private static final Set<String> EXCLUDE_METHODS
-      = ImmutableSet.of("getClass", "getDeclaringClass");
-
-  private static final Map<Class<?>, Map<String, Method>> getters = new MapMaker().makeMap();
-
   private JsonSerializer() {}
 
   public static String serialize(Object object) {
@@ -187,7 +175,7 @@
    * @throws IOException If {@link Appendable#append(char)} throws an exception.
    */
   public static void appendPojo(Appendable buf, Object pojo) throws IOException {
-    Map<String, Method> methods = getGetters(pojo);
+    Map<String, Method> methods = JsonUtil.getGetters(pojo);
     buf.append('{');
     boolean firstDone = false;
     for (Map.Entry<String, Method> entry : methods.entrySet()) {
@@ -429,73 +417,4 @@
     }
     buf.append('"');
   }
-
-  /**
-   * Gets a property of an Object.  Will return a property value if
-   * serializing the value would produce a JSON object containing that 
-   * property, otherwise returns null.
-   */
-  public static Object getProperty(Object value, String propertyName) {
-    Preconditions.checkNotNull(value);
-    Preconditions.checkNotNull(propertyName);
-
-    if (value instanceof JSONObject) {
-      return ((JSONObject) value).opt(propertyName);
-    } else if (value instanceof Map) {
-      return ((Map<?, ?>) value).get(propertyName);
-    } else {
-      // Try getter conversion
-      Method method = getGetters(value).get(propertyName);
-      if (method != null) {
-        try {
-          return method.invoke(value);
-        } catch (IllegalArgumentException e) {
-          // Shouldn't be possible.
-          throw new RuntimeException(e);
-        } catch (IllegalAccessException e) {
-          // Bad class.
-          throw new RuntimeException(e);
-        } catch (InvocationTargetException e) {
-          // Bad class.
-          throw new RuntimeException(e);
-        }
-      }
-    }
-    
-    return null;
-  }
-  
-  private static Map<String, Method> getGetters(Object pojo) {
-    Class<?> clazz = pojo.getClass();
-
-    Map<String, Method> methods = getters.get(clazz);
-    if (methods != null) {
-      return methods;
-    }
-    // Ensure consistent method ordering by using a linked hash map.
-    methods = Maps.newHashMap();
-
-    for (Method method : clazz.getMethods()) {
-      String name = getPropertyName(method);
-      if (name != null) {
-        methods.put(name, method);
-      }
-    }
-
-    getters.put(clazz, methods);
-    return methods;
-  }
-
-  private static String getPropertyName(Method method) {
-    JsonProperty property = method.getAnnotation(JsonProperty.class);
-    if (property == null) {
-      String name = method.getName();
-      if (name.startsWith("get") && (!EXCLUDE_METHODS.contains(name))) {
-        return name.substring(3, 4).toLowerCase() + name.substring(4);
-      }
-      return null;
-    } else {
-      return property.value();
-    }
-  }
 }

Added: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java?rev=762897&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java (added)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java Tue Apr  7 18:37:46 2009
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.shindig.common;
+
+import org.json.JSONObject;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.MapMaker;
+import com.google.common.collect.Maps;
+
+/**
+ * JSON utilities that are not specific to either serialization or conversion.
+ */
+public class JsonUtil {
+  private static final Set<String> EXCLUDE_METHODS
+      = ImmutableSet.of("getClass", "getDeclaringClass");
+
+  private static final Map<Class<?>, Map<String, Method>> getters = new MapMaker().makeMap();
+
+  /**
+   * Gets a property of an Object.  Will return a property value if
+   * serializing the value would produce a JSON object containing that 
+   * property, otherwise returns null.
+   */
+  public static Object getProperty(Object value, String propertyName) {
+    Preconditions.checkNotNull(value);
+    Preconditions.checkNotNull(propertyName);
+
+    if (value instanceof JSONObject) {
+      return ((JSONObject) value).opt(propertyName);
+    } else if (value instanceof Map) {
+      return ((Map<?, ?>) value).get(propertyName);
+    } else {
+      // Try getter conversion
+      Method method = getGetters(value).get(propertyName);
+      if (method != null) {
+        try {
+          return method.invoke(value);
+        } catch (IllegalArgumentException e) {
+          // Shouldn't be possible.
+          throw new RuntimeException(e);
+        } catch (IllegalAccessException e) {
+          // Bad class.
+          throw new RuntimeException(e);
+        } catch (InvocationTargetException e) {
+          // Bad class.
+          throw new RuntimeException(e);
+        }
+      }
+    }
+    
+    return null;
+  }
+  
+  static Map<String, Method> getGetters(Object pojo) {
+    Class<?> clazz = pojo.getClass();
+
+    Map<String, Method> methods = getters.get(clazz);
+    if (methods != null) {
+      return methods;
+    }
+    // Ensure consistent method ordering by using a linked hash map.
+    methods = Maps.newHashMap();
+
+    for (Method method : clazz.getMethods()) {
+      String name = getPropertyName(method);
+      if (name != null) {
+        methods.put(name, method);
+      }
+    }
+
+    getters.put(clazz, methods);
+    return methods;
+  }
+
+  private static String getPropertyName(Method method) {
+    JsonProperty property = method.getAnnotation(JsonProperty.class);
+    if (property == null) {
+      String name = method.getName();
+      if (name.startsWith("get") && (!EXCLUDE_METHODS.contains(name))) {
+        return name.substring(3, 4).toLowerCase() + name.substring(4);
+      }
+      return null;
+    } else {
+      return property.value();
+    }
+  }
+}

Propchange: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java?rev=762897&r1=762896&r2=762897&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java (original)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java Tue Apr  7 18:37:46 2009
@@ -20,10 +20,6 @@
 
 import static org.apache.shindig.common.JsonAssert.assertJsonEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
 
 import org.apache.commons.lang.StringUtils;
 import org.json.JSONArray;
@@ -37,6 +33,9 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
 /**
  * Tests for JsonSerializer.
  *
@@ -189,31 +188,6 @@
     assertEquals("Hello<world>foo < bar", obj.get("foo"));
   }
 
-  @Test
-  public void getPropertyOfJsonObject() throws Exception {
-    JSONObject json = new JSONObject("{a: 1, b: '2'}");
-    assertEquals(1, JsonSerializer.getProperty(json, "a"));
-    assertEquals("2", JsonSerializer.getProperty(json, "b"));
-    assertNull(JsonSerializer.getProperty(json, "c"));
-  }
-  
-  @Test
-  public void getPropertyOfMap() throws Exception {
-    Map<String, Object> map = ImmutableMap.of("a", (Object) 1, "b", "2");
-        assertEquals(1, JsonSerializer.getProperty(map, "a"));
-    assertEquals("2", JsonSerializer.getProperty(map, "b"));
-    assertNull(JsonSerializer.getProperty(map, "c"));
-  }
-
-  @Test
-  public void getPropertyOfPojo() throws Exception {
-    JsonPojo pojo = new JsonPojo();
-    assertEquals("string-value", JsonSerializer.getProperty(pojo, "string"));
-    assertEquals(100, JsonSerializer.getProperty(pojo, "integer"));
-    assertEquals(3, JsonSerializer.getProperty(pojo, "simple!"));
-    assertNull(JsonSerializer.getProperty(pojo, "not"));
-  }
-
   private static String avg(long start, long end, long runs) {
     double delta = end - start;
     return String.format("%f5", delta / runs);

Added: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java?rev=762897&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java (added)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java Tue Apr  7 18:37:46 2009
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.shindig.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.shindig.common.JsonSerializerTest.JsonPojo;
+import org.json.JSONObject;
+import org.junit.Test;
+
+import java.util.Map;
+
+import com.google.common.collect.ImmutableMap;
+
+public class JsonUtilTest {
+  @Test
+  public void getPropertyOfJsonObject() throws Exception {
+    JSONObject json = new JSONObject("{a: 1, b: '2'}");
+    assertEquals(1, JsonUtil.getProperty(json, "a"));
+    assertEquals("2", JsonUtil.getProperty(json, "b"));
+    assertNull(JsonUtil.getProperty(json, "c"));
+  }
+
+  @Test
+  public void getPropertyOfMap() throws Exception {
+    Map<String, Object> map = ImmutableMap.of("a", (Object) 1, "b", "2");
+        assertEquals(1, JsonUtil.getProperty(map, "a"));
+    assertEquals("2", JsonUtil.getProperty(map, "b"));
+    assertNull(JsonUtil.getProperty(map, "c"));
+  }
+
+  @Test
+  public void getPropertyOfPojo() throws Exception {
+    JsonPojo pojo = new JsonPojo();
+    assertEquals("string-value", JsonUtil.getProperty(pojo, "string"));
+    assertEquals(100, JsonUtil.getProperty(pojo, "integer"));
+    assertEquals(3, JsonUtil.getProperty(pojo, "simple!"));
+    assertNull(JsonUtil.getProperty(pojo, "not"));
+  }
+
+  @Test
+  public void excludedPropertiesOfPojo() throws Exception {
+    JsonPojo pojo = new JsonPojo();
+    // These exist as getters on all objects, but not as properties
+    assertNull(JsonUtil.getProperty(pojo, "class"));
+    assertNull(JsonUtil.getProperty(pojo, "declaringClass")); 
+  }
+}

Propchange: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelineExecutor.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelineExecutor.java?rev=762897&r1=762896&r2=762897&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelineExecutor.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelineExecutor.java Tue Apr  7 18:37:46 2009
@@ -18,7 +18,7 @@
  */
 package org.apache.shindig.gadgets.preload;
 
-import org.apache.shindig.common.JsonSerializer;
+import org.apache.shindig.common.JsonUtil;
 import org.apache.shindig.expressions.Expressions;
 import org.apache.shindig.expressions.RootELResolver;
 import org.apache.shindig.gadgets.GadgetContext;
@@ -127,13 +127,13 @@
           for (Object entry : preloaded.toJson()) {
             results.add(entry);
             
-            String id = (String) JsonSerializer.getProperty(entry, "id");
+            String id = (String) JsonUtil.getProperty(entry, "id");
 
-            Object data = JsonSerializer.getProperty(entry, "data");
+            Object data = JsonUtil.getProperty(entry, "data");
             if (data != null) {
               elResults.put(id, data);
             } else {
-              Object error = JsonSerializer.getProperty(entry, "error");
+              Object error = JsonUtil.getProperty(entry, "error");
               if (error != null) {
                 elResults.put(id, error);
               }

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelinedDataPreloader.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelinedDataPreloader.java?rev=762897&r1=762896&r2=762897&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelinedDataPreloader.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/preload/PipelinedDataPreloader.java Tue Apr  7 18:37:46 2009
@@ -20,6 +20,7 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.shindig.common.JsonSerializer;
+import org.apache.shindig.common.JsonUtil;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.common.uri.UriBuilder;
 import org.apache.shindig.config.ContainerConfig;
@@ -174,7 +175,7 @@
       for (Object request : requests) {
         JSONObject itemResponse = new JSONObject();
         itemResponse.put("error", error);
-        itemResponse.put("id", JsonSerializer.getProperty(request, "id"));
+        itemResponse.put("id", JsonUtil.getProperty(request, "id"));
         data.add(itemResponse);
       }
     }