### Question:
JsonUtil { 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 { Method method = getGetters(value).get(propertyName); if (method != null) { try { return method.invoke(value); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } } return null; } private  JsonUtil(); static Object getProperty(Object value, String propertyName);  }### Answer:
@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(); assertNull(JsonUtil.getProperty(pojo, "class")); assertNull(JsonUtil.getProperty(pojo, "declaringClass")); }