List of usage examples for org.springframework.beans BeanWrapper getPropertyType
@Nullable
Class<?> getPropertyType(String propertyName) throws BeansException;
From source file:org.jdbcluster.dao.Dao.java
/** * creates new instance of a Dao using given Dao class * @param daoClass class of Object to create * @return created dao instance/* ww w . j a v a 2 s . c om*/ */ public static Object newInstance(Class<?> daoClass) { Object dao = JDBClusterUtil.createClassObject(daoClass); //get property of DAO and initial value from jdbcluster.dao.conf.xml HashMap<String, String> hm; synchronized (classToPropsMap) { hm = classToPropsMap.get(daoClass); if (hm == null) hm = putCacheMap(daoClass); } BeanWrapper beanWrapper = new BeanWrapperImpl(); beanWrapper.setWrappedInstance(dao); for (String prop : hm.keySet()) { String value = hm.get(prop); //get property of DAO Class<?> propClass = beanWrapper.getPropertyType(prop); //convert to Type if necessary Object o = beanWrapper.convertIfNecessary(value, propClass); //set the value of the predefined property read from jdbcluster.dao.conf.xml beanWrapper.setPropertyValue(new PropertyValue(prop, o)); } return dao; }
From source file:com.aw.support.reflection.MethodInvoker.java
public static List getAllAttributes(Object target) { logger.info("searching attributes " + target.getClass().getName()); List attributes = new ArrayList(); Class cls = target.getClass(); Field[] fields = cls.getDeclaredFields(); Field[] superFields = cls.getSuperclass().getDeclaredFields(); attributes.addAll(procesarFields("", fields)); attributes.addAll(procesarFields("", superFields)); // analizamos si tiene domain.. BeanWrapper beanWrapper = new BeanWrapperImpl(target); try {// ww w. j av a 2 s .c o m Class claz = beanWrapper.getPropertyType("domain"); if (claz != null) { Field[] fieldsDomain = claz.getDeclaredFields(); Field[] superFieldsDomain = claz.getSuperclass().getDeclaredFields(); attributes.addAll(procesarFields("domain.", fieldsDomain)); attributes.addAll(procesarFields("domain.", superFieldsDomain)); } } catch (Exception e) { logger.error("No tiene attributo domain"); } return attributes; }
From source file:net.solarnetwork.node.util.ClassUtils.java
/** * Get a Map of non-null <em>simple</em> bean properties for an object. * //from www . j a v a2s. c om * @param o * the object to inspect * @param ignore * a set of property names to ignore (optional) * @return Map (never <em>null</em>) * @since 1.1 */ public static Map<String, Object> getSimpleBeanProperties(Object o, Set<String> ignore) { if (ignore == null) { ignore = DEFAULT_BEAN_PROP_NAME_IGNORE; } Map<String, Object> result = new LinkedHashMap<String, Object>(); BeanWrapper bean = new BeanWrapperImpl(o); PropertyDescriptor[] props = bean.getPropertyDescriptors(); for (PropertyDescriptor prop : props) { if (prop.getReadMethod() == null) { continue; } String propName = prop.getName(); if (ignore != null && ignore.contains(propName)) { continue; } Class<?> propType = bean.getPropertyType(propName); if (!(propType.isPrimitive() || propType.isEnum() || String.class.isAssignableFrom(propType) || Number.class.isAssignableFrom(propType) || Character.class.isAssignableFrom(propType) || Byte.class.isAssignableFrom(propType) || Date.class.isAssignableFrom(propType))) { continue; } Object propValue = bean.getPropertyValue(propName); if (propValue == null) { continue; } if (propType.isEnum()) { propValue = propValue.toString(); } else if (Date.class.isAssignableFrom(propType)) { propValue = ((Date) propValue).getTime(); } result.put(propName, propValue); } return result; }
From source file:hu.petabyte.redflags.engine.util.MappingUtils.java
/** * Sets the value of a bean's property, using the given wrapper. The * property can be a deeper one, e.g. "a.b.c.d". If any of a, b or c * properties is null, the method will call an empty constructor for its * static type. If any error occurs, setDeepProperty will write it to the * LOG and returns false./*from www . ja v a 2 s. c o m*/ * * @param wrapper * Wrapper of the root object. * @param property * The property needs to be set. * @param value * The new value of the property. * @return True if property setting was successful, false on error. */ public static synchronized boolean setDeepProperty(BeanWrapper wrapper, String property, Object value) { try { // this will help calling a constructor: ExpressionParser parser = new SpelExpressionParser(); // go thru property path elements: int offset = 0; while ((offset = property.indexOf(".", offset + 1)) > -1) { String currentProperty = property.substring(0, offset); // if current property is null: if (null == wrapper.getPropertyValue(currentProperty)) { String className = wrapper.getPropertyType(currentProperty).getName(); // build up a constructor call: Expression exp = parser.parseExpression(String.format("new %s()", className)); // LIMITATIONS: // 1) uses static type // 2) needs defined empty constructor // run it: Object newObject = exp.getValue(); // and set the property: wrapper.setPropertyValue(currentProperty, newObject); } } // finally, set the destination property: wrapper.setPropertyValue(property, value); return true; } catch (Exception ex) { LOG.error("Could not set property '{}'", property); LOG.debug("Exception: ", ex); return false; } }
From source file:org.codehaus.griffon.commons.GriffonClassUtils.java
/** * Returns the type of the given property contained within the specified class * * @param clazz The class which contains the property * @param propertyName The name of the property * * @return The property type or null if none exists *///from www . ja v a 2s . c o m public static Class getPropertyType(Class clazz, String propertyName) { if (clazz == null || StringUtils.isBlank(propertyName)) return null; try { BeanWrapper wrapper = new BeanWrapperImpl(clazz); if (wrapper.isReadableProperty(propertyName)) { return wrapper.getPropertyType(propertyName); } else { return null; } } catch (Exception e) { // if there are any errors in instantiating just return null for the moment return null; } }
From source file:org.jdal.ui.bind.AbstractBinder.java
/** * Set value on binded object using the property name. * @param value the value to set//w w w .j a v a2s .co m */ protected void setValue(Object value) { BeanWrapper wrapper = getBeanWrapper(); Object convertedValue = convertIfNecessary(value, wrapper.getPropertyType(this.propertyName)); try { wrapper.setPropertyValue(propertyName, convertedValue); oldValue = value; } catch (PropertyAccessException pae) { log.error(pae); errorProcessor.processPropertyAccessException(component, pae, bindingResult); } }
From source file:com.aw.core.util.QTTablaABeanMapper.java
public Object buildNewBean(Class type, DataRowProvider rs) { Object bean = instantiateBean(type); BeanWrapper wrap = new BeanWrapperImpl(bean); for (Iterator iterator = colNameToSetters.keySet().iterator(); iterator.hasNext();) { String colName = (String) iterator.next(); String setter = (String) colNameToSetters.get(colName); //vaPrecioCostoVVFUnit Object value = null;// www . j a va 2 s . c o m try { value = rs.getObject(colName); if (wrap.isWritableProperty(setter)) { //logger.debug("Setting "+value+ " "+colName+"-->"+setter); Object convertedValue = ObjectConverter.convert(value, wrap.getPropertyType(setter)); wrap.setPropertyValue(setter, convertedValue); //wrap.setPropertyValue(setter, value); } else { logger.debug("Not set " + value + " " + colName + "-->(setter R/O):" + setter); } } catch (Throwable e) { logger.error("Error: No se puede settear propertyName:" + setter + " db.ColName:" + colName + " con valor:" + value); throw AWBusinessException.wrapUnhandledException(logger, e); } } return bean; }
From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java
/** * fields// w w w . jav a 2 s. c o m * <p> * <code>isValidated()</code><code>true</code>group * </p> */ public void mapTo(Object object) { if (isValidated() || object == null) { return; } if (log.isDebugEnabled()) { log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(), ObjectUtil.identityToString(object)); } BeanWrapper bean = new BeanWrapperImpl(object); getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean); for (Field field : getFields()) { String propertyName = field.getFieldConfig().getPropertyName(); if (bean.isReadableProperty(propertyName)) { Object propertyValue = bean.getPropertyValue(propertyName); Class<?> propertyType = bean.getPropertyType(propertyName); PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName); if (editor == null) { editor = BeanUtils.findEditorByConvention(propertyType); } if (editor == null) { if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) { field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class)); } else { field.setValue(bean.convertIfNecessary(propertyValue, String.class)); } } else { editor.setValue(propertyValue); field.setValue(editor.getAsText()); } } else { log.debug("No readable property \"{}\" found in type {}", propertyName, object.getClass().getName()); } } }
From source file:org.jdto.spring.BeanWrapperBeanModifier.java
/** * Set a property using the property path invoking a spring framework {@link BeanWrapper}. * @param propertyPath/*w w w . ja v a 2 s .com*/ * @param value * @param instance */ @Override public void doWritePropertyValue(String propertyPath, Object value, Object instance) { BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance); //check and make up for missing association parts. StringBuilder builder = new StringBuilder(); String[] subProps = StringUtils.split(propertyPath, '.'); //go through all the parts but one for (int i = 0; i < subProps.length - 1; i++) { String prop = subProps[i]; if (i > 0) { builder.append("."); } builder.append(prop); Object partialValue = beanWrapper.getPropertyValue(builder.toString()); if (partialValue == null) { //make up for it Class propCls = beanWrapper.getPropertyType(builder.toString()); Object madeUpValue = BeanClassUtils.createInstance(propCls); if (madeUpValue != null) { if (beanWrapper.isWritableProperty(builder.toString())) { beanWrapper.setPropertyValue(builder.toString(), madeUpValue); } } } } if (!beanWrapper.isWritableProperty(propertyPath)) { logger.info("Cannot write property path " + propertyPath + " of bean", instance); return; } //this can be improved by registering property editors on the bean wrapper //at moment this approach is not so simple as the current functionality. //nevertheless on the future this situation may change. Class expectedType = beanWrapper.getPropertyType(propertyPath); value = ValueConversionHelper.applyCompatibilityLogic(expectedType, value); beanWrapper.setPropertyValue(propertyPath, value); }
From source file:org.gvnix.web.datatables.util.QuerydslUtils.java
/** * Obtains the descriptor of the filtered field * /*from w ww.j a va 2 s. c om*/ * @param fieldName * @param entityType * @return */ public static <T> TypeDescriptor getTypeDescriptor(String fieldName, Class<T> entityType) { String fieldNameToFindType = fieldName; BeanWrapper beanWrapper = getBeanWrapper(entityType); TypeDescriptor fieldDescriptor = null; Class<?> propType = null; // Find recursive the las beanWrapper if (fieldName.contains(SEPARATOR_FIELDS)) { String[] fieldNameSplitted = StringUtils.split(fieldName, SEPARATOR_FIELDS); for (int i = 0; i < fieldNameSplitted.length - 1; i++) { propType = beanWrapper.getPropertyType(fieldNameSplitted[i]); if (propType == null) { throw new IllegalArgumentException(String.format("Property %s not found in %s (request %s.%s)", fieldNameSplitted[i], beanWrapper.getWrappedClass(), entityType, fieldName)); } beanWrapper = getBeanWrapper(propType); } fieldNameToFindType = fieldNameSplitted[fieldNameSplitted.length - 1]; } fieldDescriptor = beanWrapper.getPropertyTypeDescriptor(fieldNameToFindType); return fieldDescriptor; }