List of usage examples for java.beans PropertyDescriptor PropertyDescriptor
PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y)
From source file:org.egov.infra.web.struts.actions.BaseFormAction.java
private void setRelationship(final String relationshipName, final Class class1) throws IntrospectionException { final String[] ids = parameters.get(relationshipName); if (ids != null && ids.length > 0) { final String id = ids[0]; if (isNotBlank(id) && Long.valueOf(id) > 0) { final PropertyDescriptor propDiscriptor = new PropertyDescriptor("id", class1); if (class1 != null && "Fund".equals(class1.getSimpleName())) setValue(relationshipName, getPersistenceService().load(Integer.valueOf(id), class1)); else if (propDiscriptor.getPropertyType().isAssignableFrom(Long.class)) setValue(relationshipName, getPersistenceService().getSession().get(class1, Long.valueOf(id))); else/*from w w w . j a v a 2 s. c o m*/ setValue(relationshipName, getPersistenceService().load(Integer.valueOf(id), class1)); } } }
From source file:org.jsonschema2pojo.integration.JavaNameIT.java
@Test public void serializedPropertiesHaveCorrectNames() throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException, ClassNotFoundException { ClassLoader javaNameClassLoader = schemaRule.generateAndCompile("/schema/javaName/javaName.json", "com.example.javaname"); Class<?> classWithJavaNames = javaNameClassLoader.loadClass("com.example.javaname.JavaName"); Object instance = classWithJavaNames.newInstance(); new PropertyDescriptor("javaProperty", classWithJavaNames).getWriteMethod().invoke(instance, "abc"); new PropertyDescriptor("propertyWithoutJavaName", classWithJavaNames).getWriteMethod().invoke(instance, "abc"); JsonNode serialized = mapper.valueToTree(instance); assertThat(serialized.has("propertyWithJavaName"), is(true)); assertThat(serialized.has("propertyWithoutJavaName"), is(true)); }
From source file:org.mule.module.netsuite.api.model.expression.filter.FilterExpressionBuilder.java
private PropertyDescriptor newDescriptor(String propertyName, Object object) { try {// w ww. j a va 2 s .c om return new PropertyDescriptor(propertyName, object.getClass()); } catch (IntrospectionException e) { throw PropertyAccess.propertyNotFound(propertyName, object); } }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to TRUE * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone * It also tests the serialization and deserialization process * /*from www . java 2s . co m*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception { Field field = classWhenConfigIsTrue.getDeclaredField("customFormatDefaultTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd'T'HH:mm:ss", annotation.pattern()); // Assert that the timezones match assertEquals("UTC", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("UTC")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatDefaultTZ", "2016-11-06T00:00:00"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue); Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsTrue).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06T00:00:00", jsonVersion.get("customFormatDefaultTZ").asText()); }
From source file:org.jsonschema2pojo.integration.PropertiesIT.java
@Test public void propertyNamesAreLowerCamelCase() throws Exception { ClassLoader resultsClassLoader = schemaRule .generateAndCompile("/schema/properties/propertiesAreUpperCamelCase.json", "com.example"); Class<?> generatedType = resultsClassLoader.loadClass("com.example.UpperCase"); Object instance = generatedType.newInstance(); new PropertyDescriptor("property1", generatedType).getWriteMethod().invoke(instance, "1"); new PropertyDescriptor("propertyTwo", generatedType).getWriteMethod().invoke(instance, 2); new PropertyDescriptor("propertyThreeWithSpace", generatedType).getWriteMethod().invoke(instance, "3"); new PropertyDescriptor("propertyFour", generatedType).getWriteMethod().invoke(instance, "4"); JsonNode jsonified = mapper.valueToTree(instance); assertNotNull(generatedType.getDeclaredField("property1")); assertNotNull(generatedType.getDeclaredField("propertyTwo")); assertNotNull(generatedType.getDeclaredField("propertyThreeWithSpace")); assertNotNull(generatedType.getDeclaredField("propertyFour")); assertThat(jsonified.has("Property1"), is(true)); assertThat(jsonified.has("PropertyTwo"), is(true)); assertThat(jsonified.has(" PropertyThreeWithSpace"), is(true)); assertThat(jsonified.has("propertyFour"), is(true)); }
From source file:org.jsonschema2pojo.integration.MediaIT.java
public static void roundTripAssertions(ObjectMapper objectMapper, String propertyName, String jsonValue, Object javaValue) throws Exception { ObjectNode node = objectMapper.createObjectNode(); node.put(propertyName, jsonValue);/*from w w w . j ava 2s . c o m*/ Object pojo = objectMapper.treeToValue(node, classWithMediaProperties); Method getter = new PropertyDescriptor(propertyName, classWithMediaProperties).getReadMethod(); assertThat(getter.invoke(pojo), is(equalTo(javaValue))); JsonNode jsonVersion = objectMapper.valueToTree(pojo); assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue))); }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to TRUE * The field should have @JsonFormat annotation with pattern and timezone defined in json schema * It also tests the serialization and deserialization process * // w ww . j ava 2s . c o m * @throws Exception */ @Test public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception { Field field = classWhenConfigIsTrue.getDeclaredField("customFormatCustomTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd", annotation.pattern()); // Assert that the timezones match assertEquals("PST", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("PST")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatCustomTZ", "2016-11-06"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue); Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsTrue).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateFormatter.parse("2016-11-06").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06", jsonVersion.get("customFormatCustomTZ").asText()); }
From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private Object fetchRowObject(ResultSet rs, Class clazz) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); Object obj = null;/*from w w w . j a v a 2s .c o m*/ try { obj = ConstructorUtils.invokeConstructor(clazz, null); } catch (Exception e) { e.printStackTrace(); return null; } if (obj == null) { throw new SQLException("Cannot instance object; " + clazz.getName()); } int columnCount = meta.getColumnCount(); String columnName, propertyName; Method m; PropertyDescriptor pd; Object value; List<Column2Property> setterColumnsNames = getColumnsFromObj(obj, null); for (int i = 1; i <= columnCount; i++) { propertyName = null; value = null; columnName = meta.getColumnName(i); for (Column2Property c : setterColumnsNames) { if (c.columnName.equals(columnName)) { propertyName = c.propertyName; } } if (propertyName == null) continue; try { pd = new PropertyDescriptor(propertyName, clazz); } catch (Exception e) { e.printStackTrace(); continue; } if (pd != null) { m = pd.getWriteMethod(); Class[] classes = m.getParameterTypes(); Class c = classes[0]; try { value = getColumnValue(rs, meta, i, c); } catch (Exception e) { e.printStackTrace(); continue; } if (null != value) { try { m.invoke(obj, value); } catch (Exception e) { e.printStackTrace(); continue; } } } } return obj; }
From source file:index.IndexUtils.java
/** * ??//from w ww .j av a 2 s. com * @param doc * @param entityPath * @return * @throws ClassNotFoundException * @throws java.beans.IntrospectionException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws java.lang.reflect.InvocationTargetException * @throws InstantiationException * @throws NoSuchMethodException */ public static Object getIndexResult(Document doc, String entityPath) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { // ?? Class c2 = Class.forName(entityPath); // Object obj = c2.getConstructor(new Class[] {}).newInstance(); // Field[] fields = c2.getDeclaredFields(); // ?? for (Field field : fields) { // ?? PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c2); // setget Method method = pd.getWriteMethod(); // method.invoke(obj, new Object[] { doc.get(field.getName()) }); } return obj; }
From source file:org.apache.openjpa.azure.util.AzureUtils.java
public static Object getObjectIdValue(final Object oid, final String key) { Object value = null;//from w w w . j a va 2 s . com if (StringUtils.isNotBlank(key)) { try { if (oid instanceof ObjectId) { final Object idObject = ((ObjectId) oid).getIdObject(); value = new PropertyDescriptor(key, idObject.getClass()).getReadMethod().invoke(idObject); } else { final Embeddable embeddable = oid.getClass().getAnnotation(Embeddable.class); if (embeddable == null) { value = oid; } else { value = new PropertyDescriptor(key, oid.getClass()).getReadMethod().invoke(oid); } } } catch (Exception ignore) { // ignore } } return value; }