List of usage examples for org.springframework.beans BeanUtils getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException
From source file:de.extra.client.core.annotation.PropertyPlaceholderPluginConfigurer.java
private void setPluginProperties(final ConfigurableListableBeanFactory beanFactory, final Properties properties, final String beanName, final Class<?> clazz) { final PluginConfiguration annotationConfigutation = clazz.getAnnotation(PluginConfiguration.class); final MutablePropertyValues mutablePropertyValues = beanFactory.getBeanDefinition(beanName) .getPropertyValues();// ww w.ja va 2 s .c om for (final PropertyDescriptor property : BeanUtils.getPropertyDescriptors(clazz)) { final Method setter = property.getWriteMethod(); PluginValue valueAnnotation = null; if (setter != null && setter.isAnnotationPresent(PluginValue.class)) { valueAnnotation = setter.getAnnotation(PluginValue.class); } if (valueAnnotation != null) { final String key = extractKey(annotationConfigutation, valueAnnotation, clazz); final String value = resolvePlaceholder(key, properties, SYSTEM_PROPERTIES_MODE_FALLBACK); if (StringUtils.isEmpty(value)) { throw new BeanCreationException(beanName, "No such property=[" + key + "] found in properties."); } if (LOG.isDebugEnabled()) { LOG.debug("setting property=[" + clazz.getName() + "." + property.getName() + "] value=[" + key + "=" + value + "]"); } mutablePropertyValues.addPropertyValue(property.getName(), value); } } for (final Field field : clazz.getDeclaredFields()) { if (LOG.isDebugEnabled()) { LOG.debug("examining field=[" + clazz.getName() + "." + field.getName() + "]"); } if (field.isAnnotationPresent(PluginValue.class)) { final PluginValue valueAnnotation = field.getAnnotation(PluginValue.class); final PropertyDescriptor property = BeanUtils.getPropertyDescriptor(clazz, field.getName()); if (property == null || property.getWriteMethod() == null) { throw new BeanCreationException(beanName, "setter for property=[" + clazz.getName() + "." + field.getName() + "] not available."); } final String key = extractKey(annotationConfigutation, valueAnnotation, clazz); String value = resolvePlaceholder(key, properties, SYSTEM_PROPERTIES_MODE_FALLBACK); if (value == null) { // DEFAULT Value suchen final int separatorIndex = key.indexOf(VALUE_SEPARATOR); if (separatorIndex != -1) { final String actualPlaceholder = key.substring(0, separatorIndex); final String defaultValue = key.substring(separatorIndex + VALUE_SEPARATOR.length()); value = resolvePlaceholder(actualPlaceholder, properties, SYSTEM_PROPERTIES_MODE_FALLBACK); if (value == null) { value = defaultValue; } } } if (value != null) { if (LOG.isDebugEnabled()) { LOG.debug("setting property=[" + clazz.getName() + "." + field.getName() + "] value=[" + key + "=" + value + "]"); } mutablePropertyValues.addPropertyValue(field.getName(), value); } else if (!ignoreNullValues) { throw new BeanCreationException(beanName, "No such property=[" + key + "] found in properties."); } } } }
From source file:org.hellojavaer.testcase.generator.TestCaseGenerator.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static <T> void checkBeanValidity(Class<T> clazz, List<String> excludeFieldList, int recursiveCycleLimit) { PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(clazz); boolean validBean = false; for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null && pd.getReadMethod() != null && // (excludeFieldList != null && !excludeFieldList.contains(pd.getName()) || excludeFieldList == null) // ) {//from www .j a v a 2 s. co m validBean = true; // just set write ,read for user control if (!Modifier.isPublic(pd.getWriteMethod().getDeclaringClass().getModifiers())) { pd.getWriteMethod().setAccessible(true); } Class fieldClazz = pd.getPropertyType(); if (!TypeUtil.isBaseType(fieldClazz)) { try { // ???? if (recursiveCycleLimit == 0 && fieldClazz == clazz) { throw new RuntimeException("recursive cycle limit is 0! field[" + pd.getName() + "] may cause recursive, please add this field[" + pd.getName() + "] to exclude list or set recursiveCycleLimit more than 0 ."); } else if (!fieldClazz.isAssignableFrom(clazz)) { checkBeanValidity(fieldClazz, null, 999999999); } } catch (Exception e) { throw new RuntimeException("Unknown Class " + fieldClazz.getName() + " for field " + pd.getName() + " ! please add this field[" + pd.getName() + "] to exclude list.", e); } } } } if (!validBean) { throw new RuntimeException( "Invalid Bean Class[" + clazz.getName() + "], for it has not getter setter methods!"); } }
From source file:com.zero.service.impl.BaseServiceImpl.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private void copyProperties(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass()); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); }//from ww w .jav a 2 s . com Object sourceValue = readMethod.invoke(source); Object targetValue = readMethod.invoke(target); if (sourceValue != null && targetValue != null && targetValue instanceof Collection) { Collection collection = (Collection) targetValue; collection.clear(); collection.addAll((Collection) sourceValue); } else { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, sourceValue); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:com.github.dactiv.common.bundle.BeanResourceBundle.java
/** * ?bean?mapkey/*w w w. ja v a 2 s. co m*/ */ @Override public Enumeration<String> getKeys() { Vector<String> vector = new Vector<String>(); PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(bean.getClass()); //???bean for (PropertyDescriptor pd : propertyDescriptors) { //get/set??? if ((pd.getWriteMethod() == null) || pd.getReadMethod() == null) { continue; } String key = pd.getName(); /* * ?map?: * 1.include * 2.?exclude * 3.ignoreNullValuetrue,?null */ if (isIncludeProperty(key) && !isExcludeProperty(key)) { if (ignoreNullValue && ReflectionUtils.invokeGetterMethod(bean, key) == null) { continue; } vector.addElement(key); } } //?mapkey return vector.elements(); }
From source file:net.paoding.rose.jade.core.BeanPropertyRowMapper.java
/** * Initialize the mapping metadata for the given class. * /*from w ww .j a v a 2 s.c om*/ */ protected void initialize() { this.mappedFields = new HashMap<String, PropertyDescriptor>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; if (pd.getWriteMethod() != null) { this.mappedProperties.add(pd.getName()); this.mappedFields.put(pd.getName().toLowerCase(), pd); for (String underscoredName : underscoreName(pd.getName())) { if (!pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } } }
From source file:com.gzj.tulip.jade.rowmapper.BeanPropertyRowMapper.java
/** * Initialize the mapping metadata for the given class. * /*from w w w . ja v a 2s . com*/ * @param mappedClass the mapped class. */ protected void initialize() { this.mappedFields = new HashMap<String, PropertyDescriptor>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); if (checkProperties) { mappedProperties = new HashSet<String>(); } for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; if (pd.getWriteMethod() != null) { if (checkProperties) { this.mappedProperties.add(pd.getName()); } this.mappedFields.put(pd.getName().toLowerCase(), pd); for (String underscoredName : underscoreName(pd.getName())) { if (underscoredName != null && !pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } } }
From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java
/** * Initialize the mapping metadata for the given class. * @param mappedClass the mapped class./*from w w w . j a v a2 s. co m*/ */ protected void initialize(Class mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; if (pd.getWriteMethod() != null) { this.mappedFields.put(pd.getName().toLowerCase(), pd); String underscoredName = underscoreName(pd.getName()); if (!pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } }
From source file:net.groupbuy.service.impl.BaseServiceImpl.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass()); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); }//w ww . ja v a 2 s.c o m Object sourceValue = readMethod.invoke(source); Object targetValue = readMethod.invoke(target); if (sourceValue != null && targetValue != null && targetValue instanceof Collection) { Collection collection = (Collection) targetValue; collection.clear(); collection.addAll((Collection) sourceValue); } else { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, sourceValue); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:org.mule.modules.zuora.ZuoraModule.java
@MetaDataRetriever public MetaData getMetadata(MetaDataKey key) throws Exception { Class<?> cls = getClassForType(key.getId()); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(cls); Map<String, MetaDataModel> fieldMap = new HashMap<String, MetaDataModel>(); for (PropertyDescriptor pd : pds) { if (pd.getReadMethod() != null && pd.getWriteMethod() != null) { fieldMap.put(pd.getName(), getFieldMetadata(pd.getPropertyType())); }//from w w w . j a v a2 s . c o m } DefaultDefinedMapMetaDataModel mapModel = new DefaultDefinedMapMetaDataModel(fieldMap, key.getId()); return new DefaultMetaData(mapModel); }
From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurer.java
private void processAnnotatedProperties(Properties properties, String name, MutablePropertyValues mpv, Class<?> clazz) {/* w w w. j a v a 2 s.c om*/ // TODO support proxies if (clazz != null && clazz.getPackage() != null) { if (basePackage != null && !clazz.getPackage().getName().startsWith(basePackage)) { return; } log.info("Configuring properties for bean=" + name + "[" + clazz + "]"); for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(clazz)) { if (log.isLoggable(Level.FINE)) log.fine("examining property=[" + clazz.getName() + "." + property.getName() + "]"); Method setter = property.getWriteMethod(); Method getter = property.getReadMethod(); Property annotation = null; if (setter != null && setter.isAnnotationPresent(Property.class)) { annotation = setter.getAnnotation(Property.class); } else if (setter != null && getter != null && getter.isAnnotationPresent(Property.class)) { annotation = getter.getAnnotation(Property.class); } else if (setter == null && getter != null && getter.isAnnotationPresent(Property.class)) { throwBeanConfigurationException(clazz, property.getName()); } if (annotation != null) { setProperty(properties, name, mpv, clazz, property, annotation); } } for (Field field : clazz.getDeclaredFields()) { if (log.isLoggable(Level.FINE)) log.fine("examining field=[" + clazz.getName() + "." + field.getName() + "]"); if (field.isAnnotationPresent(Property.class)) { Property annotation = field.getAnnotation(Property.class); PropertyDescriptor property = BeanUtils.getPropertyDescriptor(clazz, field.getName()); if (property == null || property.getWriteMethod() == null) { throwBeanConfigurationException(clazz, field.getName()); } setProperty(properties, name, mpv, clazz, property, annotation); } } } }