List of usage examples for java.beans PropertyDescriptor PropertyDescriptor
PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y)
From source file:com.webpagebytes.cms.local.WPBLocalDataStoreDao.java
private int buildStatementForInsertUpdate(Object obj, Set<String> ignoreFields, PreparedStatement preparedStatement, Connection connection) throws SQLException, WPBSerializerException { Class<? extends Object> kind = obj.getClass(); Field[] fields = kind.getDeclaredFields(); int fieldIndex = 0; for (int i = 0; i < fields.length; i++) { Field field = fields[i];// w w w . j a va 2s.c o m field.setAccessible(true); boolean storeField = (field.getAnnotation(WPBAdminFieldKey.class) != null) || (field.getAnnotation(WPBAdminFieldStore.class) != null) || (field.getAnnotation(WPBAdminFieldTextStore.class) != null); if (storeField) { String fieldName = field.getName(); if (ignoreFields != null && ignoreFields.contains(fieldName)) { continue; } fieldIndex = fieldIndex + 1; Object value = null; try { PropertyDescriptor pd = new PropertyDescriptor(fieldName, kind); value = pd.getReadMethod().invoke(obj); } catch (Exception e) { throw new WPBSerializerException("Cannot get property value", e); } if (field.getType() == Long.class) { Long valueLong = (Long) value; if (valueLong != null) { preparedStatement.setLong(fieldIndex, valueLong); } else { preparedStatement.setNull(fieldIndex, Types.BIGINT); } } else if (field.getType() == String.class) { String valueString = (String) value; if (field.getAnnotation(WPBAdminFieldStore.class) != null || field.getAnnotation(WPBAdminFieldKey.class) != null) { if (valueString != null) { preparedStatement.setString(fieldIndex, valueString); } else { preparedStatement.setNull(fieldIndex, Types.VARCHAR); } } else if (field.getAnnotation(WPBAdminFieldTextStore.class) != null) { if (valueString != null) { Clob clob = connection.createClob(); clob.setString(1, valueString); preparedStatement.setClob(fieldIndex, clob); } else { preparedStatement.setNull(fieldIndex, Types.CLOB); } } } else if (field.getType() == Integer.class) { Integer valueInt = (Integer) value; if (valueInt != null) { preparedStatement.setInt(fieldIndex, valueInt); } else { preparedStatement.setNull(fieldIndex, Types.INTEGER); } } else if (field.getType() == Date.class) { Date date = (Date) value; if (date != null) { java.sql.Timestamp sqlDate = new java.sql.Timestamp(date.getTime()); preparedStatement.setTimestamp(fieldIndex, sqlDate); } else { preparedStatement.setNull(fieldIndex, Types.DATE); } } } } return fieldIndex; }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to FALSE * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone * It also tests the serialization and deserialization process * /*from ww w . jav a2 s .c om*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception { Field field = classWhenConfigIsFalse.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, classWhenConfigIsFalse); Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsFalse).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:de.xwic.appkit.webbase.toolkit.app.EditorToolkit.java
/** * Loads the field values into the controls. Entity is coming by internal editor model. *//*from w ww. jav a 2 s. c om*/ public void loadFieldValues() { Object obj = null; Class<?> clazz = null; if (null != baseObj) { obj = baseObj; clazz = obj.getClass(); } else if (null != model) { obj = model.getEntity(); clazz = ((IEntity) obj).type(); } for (Iterator<String> iterator = registeredControls.keySet().iterator(); iterator.hasNext();) { String controlId = iterator.next(); IControl control = registeredControls.get(controlId); String propName = getPropertyName(control); Object value = null; try { PropertyDescriptor propInfo = new PropertyDescriptor(propName, clazz); value = propInfo.getReadMethod().invoke(obj, (Object[]) null); } catch (Exception ex) { throw new RuntimeException("Property not found: " + propName, ex); } IToolkitControlHelper helper = allControls.get(control.getClass()); if (helper == null) { throw new RuntimeException("Could not find control helper: " + control.getClass()); } Object controlValue; ITypeConverter converter = registeredConverters.get(propName); if (converter != null) { //if we have a type converter registered for the property, convert the entity type to the control value type first controlValue = converter.convertLeft(value); } else { controlValue = value; } helper.loadContent(control, controlValue); } }
From source file:com.searchbox.collection.oppfin.EENCollection.java
private static void getFieldValues(Object target, String path, FieldMap fields) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException { if (JAXBElement.class.isAssignableFrom(target.getClass())) { target = ((JAXBElement) target).getValue(); }/* w w w. ja v a 2 s.com*/ if (Date.class.isAssignableFrom(target.getClass())) { LOGGER.debug("put Date:" + target); fields.put(path, target); } else if (Calendar.class.isAssignableFrom(target.getClass())) { fields.put(path, ((Calendar) target).getTime()); } else if (XMLGregorianCalendar.class.isAssignableFrom(target.getClass())) { fields.put(path, ((XMLGregorianCalendar) target).toGregorianCalendar().getTime()); } else if (!target.getClass().isArray() && target.getClass().getName().startsWith("java.")) { if (!target.toString().isEmpty()) { fields.put(path, target.toString()); } } else { for (java.lang.reflect.Field field : target.getClass().getDeclaredFields()) { if (field.getName().startsWith("_") || Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); Method reader = new PropertyDescriptor(field.getName(), target.getClass()).getReadMethod(); try { if (reader != null) { Object obj = reader.invoke(target); if (field.getType().isArray()) { for (Object object : (Object[]) obj) { getFieldValues(object, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } else if (java.util.Collection.class.isAssignableFrom(obj.getClass())) { for (Object object : (java.util.Collection) obj) { getFieldValues(object, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } else { getFieldValues(obj, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } } catch (Exception e) { ; } } } }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to FALSE * The field should have @JsonFormat annotation with pattern and timezone defined in json schema * It also tests the serialization and deserialization process * /*from w w w . j a v a 2 s .com*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception { Field field = classWhenConfigIsFalse.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, classWhenConfigIsFalse); Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsFalse).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:de.xwic.appkit.webbase.toolkit.app.EditorToolkit.java
/** * Saves the values from GUI controls into the entity of the editormodel. No update on DB is triggered. *//*from w w w . j a v a2s .co m*/ public void saveFieldValues() { Object obj = null; Class<?> clazz = null; if (null != baseObj) { obj = baseObj; clazz = obj.getClass(); } else if (null != model) { obj = model.getEntity(); clazz = ((IEntity) obj).type(); } for (Iterator<IControl> iterator = registeredControls.values().iterator(); iterator.hasNext();) { IControl control = iterator.next(); String propName = getPropertyName(control); PropertyDescriptor propInfo = null; try { propInfo = new PropertyDescriptor(propName, clazz); } catch (IntrospectionException ex) { throw new RuntimeException("Could find property: " + propName + " of entityclass: " + (obj == null ? "No entity!" : obj.getClass().getName()), ex); } if (!control.isVisible()) { continue; } if (control instanceof IHTMLElement) { IHTMLElement htmlElement = (IHTMLElement) control; if (!htmlElement.isEnabled()) { continue; } } IToolkitControlHelper helper = allControls.get(control.getClass()); if (helper == null) { throw new RuntimeException("Could not find control helper: " + control.getClass()); } Object value; Object controlValue = helper.getContent(control); ITypeConverter converter = registeredConverters.get(propName); if (converter != null) { //if we have a type converter registered for the property, convert the control value type to the entity value type first value = converter.convertRight(controlValue); } else { value = autoConvertToEntityType(propInfo, controlValue); } try { propInfo.getWriteMethod().invoke(obj, new Object[] { value }); } catch (Exception ex) { throw new RuntimeException("Could not write property: " + propName + " of entityclass: " + (obj == null ? "No entity!" : obj.getClass().getName()), ex); } } }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java
/** * Utility method for retrieving a string representation of the value referenced by a JAXB generated object. * <p/>/*from w ww . j av a2 s .c o m*/ * <p/> * This method assumes that <code>value</code> is a valid declared field of the object being passed to it, * and also contains the corresponding read method <code>getValue()</code>. * <p/> * <p/> * <b>This method will work with other non-JAXB generated objects that meet the requirements stated above, * but it is not recommended.</b> * * @param valueObject - an object that represents a JAXB generated value object * @return a string representation of <code>Object.getValue()</code> */ public static String getJAXBObjectValue(final Object valueObject) { String value = null; if (valueObject != null) { try { final Field valueField = valueObject.getClass().getDeclaredField("value"); final PropertyDescriptor valueObjectDescriptor = new PropertyDescriptor(valueField.getName(), valueObject.getClass()); value = valueObjectDescriptor.getReadMethod().invoke(valueObject, null).toString(); } catch (Exception e) { logger.error(e.getMessage()); return value; } } return value; }
From source file:de.xwic.appkit.core.dao.AbstractDAO.java
private Object getPropertyValue(IEntity entity, Property property, ValidationResult result) throws Exception { String keyPropName = entity.type().getName() + "." + property.getName(); try {/*from w w w . j av a 2 s .c o m*/ Method mRead = property.getDescriptor().getReadMethod(); if (mRead == null) { // the property is not defined on the entity class. Search for the property in the superclass // and use that. This is needed for cases where the entity is using the history and therefore // extending a base implementation PropertyDescriptor pd = new PropertyDescriptor(property.getName(), entity.getClass().getSuperclass()); mRead = pd.getReadMethod(); if (mRead == null) { throw new ConfigurationException("The property " + property.getName() + " can not be resolved on entity " + entity.getClass().getName()); } } Object value = mRead.invoke(entity, (Object[]) null); return value; } catch (Exception se) { Throwable e = se; while (e != null) { if (e instanceof SecurityException) { result.addWarning(keyPropName, ValidationResult.FIELD_REQUIRED_NOT_ACCESSABLE); break; } else if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; e = ite.getTargetException(); if (e == ite) { break; } } else if (e instanceof UndeclaredThrowableException) { UndeclaredThrowableException ute = (UndeclaredThrowableException) e; e = ute.getUndeclaredThrowable(); if (e == ute) { break; } } else { throw se; } } } return null; }
From source file:org.firebrandocm.dao.ClassMetadata.java
/** * Private helper that caches information for a property for further persistence consideration * * @param colAnnotation the column annotation * @param propertyName the property name * @param type the property type * @param indexed whether the property should be indexed in the data store * @param lazy if access to this property should be loaded on demand * @param counter if this property represents a counter * @param counterIncrease if this property represents a value for a counter arithmetic operation *//*from w ww. j a v a 2 s. co m*/ private void addProperty(Column colAnnotation, String propertyName, Class<?> type, boolean indexed, boolean lazy, boolean counter, boolean counterIncrease) throws ClassNotFoundException, IntrospectionException { propertiesTypesMap.put(propertyName, type); mutationProperties.add(propertyName); if (indexed) { indexedProperties.add(propertyName); log.debug(String.format("added indexed property %s", propertyName)); } if (lazy) { PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, target); lazyAccesors.put(descriptor.getReadMethod(), propertyName); lazyProperties.add(propertyName); } if (counter) { counterProperties.add(propertyName); } if (!counterIncrease) { selectionProperties.add(propertyName); addColumnToColumnFamilyDefinition(colAnnotation, propertyName, indexed); } log.debug(String.format("added property %s", propertyName)); }
From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java
@SuppressWarnings("rawtypes") private Column2Property getIdFromObject(Class clazz) throws Exception { // ???/*from www .j ava2s . com*/ for (Field field : clazz.getDeclaredFields()) { String columnName = null; Annotation[] annotations = field.getDeclaredAnnotations(); for (Annotation a : annotations) { if (a instanceof Id) { PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); Column2Property c = new Column2Property(); c.propertyName = pd.getName(); c.setterMethodName = pd.getWriteMethod().getName(); c.getterMethodName = pd.getReadMethod().getName(); c.columnName = columnName; return c; } } } return null; }