List of usage examples for org.springframework.beans BeanUtils getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException
From source file:org.zkybase.cmdb.util.ReflectionUtils.java
public static <T, U> U copySimpleProperties(T from, U to) { try {/* w w w . j a v a2 s. c om*/ PropertyDescriptor[] fromDescs = BeanUtils.getPropertyDescriptors(from.getClass()); for (PropertyDescriptor fromDesc : fromDescs) { String propName = fromDesc.getName(); PropertyDescriptor toDesc = BeanUtils.getPropertyDescriptor(to.getClass(), propName); if (toDesc != null) { Method readMethod = fromDesc.getReadMethod(); Object value = readMethod.invoke(from); boolean write = (value instanceof Long || value instanceof String); if (write) { Method writeMethod = toDesc.getWriteMethod(); writeMethod.invoke(to, value); } } } return to; } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:com.glaf.core.util.Tools.java
@SuppressWarnings("unchecked") public static Map<String, Object> getDataMap(Object target) { Map<String, Object> dataMap = new TreeMap<String, Object>(); if (Map.class.isAssignableFrom(target.getClass())) { Map<String, Object> map = (Map<String, Object>) target; Set<Entry<String, Object>> entrySet = map.entrySet(); for (Entry<String, Object> entry : entrySet) { String key = entry.getKey(); Object value = entry.getValue(); if (key != null && value != null) { dataMap.put(key.toString(), value); }/* w w w . j av a2s. com*/ } } else { PropertyDescriptor[] propertyDescriptor = BeanUtils.getPropertyDescriptors(target.getClass()); for (int i = 0; i < propertyDescriptor.length; i++) { PropertyDescriptor descriptor = propertyDescriptor[i]; String propertyName = descriptor.getName(); if (propertyName.equalsIgnoreCase("class")) { continue; } try { Object value = PropertyUtils.getProperty(target, propertyName); dataMap.put(propertyName, value); } catch (Exception ex) { } } } return dataMap; }
From source file:grails.plugin.searchable.internal.support.DynamicMethodUtils.java
/** * Extract the property names from the given method name suffix accoring * to the given Class's properties// w w w . j a v a 2 s.c o m * * @param clazz the Class to resolve property names against * @param methodSuffix a method name suffix like 'NameAndAddressCity' * @return a Collection of property names like ['name', 'address'] */ public static Collection extractPropertyNames(Class clazz, String methodSuffix) { String joinedNames = methodSuffix; Set propertyNames = new HashSet(); PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz); for (int i = 0; i < propertyDescriptors.length; i++) { String name = propertyDescriptors[i].getName(); String capitalized = name.substring(0, 1).toUpperCase() + name.substring(1); if (joinedNames.indexOf(capitalized) > -1) { // uses indexOf instead of contains for Java 1.4 compatibility propertyNames.add(name); joinedNames = DefaultGroovyMethods.minus(joinedNames, capitalized); } } if (joinedNames.length() > 0) { propertyNames.addAll(extractPropertyNames(joinedNames)); } return propertyNames; }
From source file:org.jdal.vaadin.data.BeanWrapperItem.java
public BeanWrapperItem(Object bean, List<String> properties) { this.beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean); if (properties == null) { for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(bean.getClass())) this.properties.add(pd.getName()); } else {//from www . j a va 2 s . co m this.properties = properties; } }
From source file:org.opentele.server.core.util.CustomGroovyBeanJSONMarshaller.java
public void marshalObject(Object o, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); try {//w ww .java 2 s . com writer.object(); for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) { String name = property.getName(); Method readMethod = property.getReadMethod(); if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) { Object value = readMethod.invoke(o, (Object[]) null); writer.key(name); json.convertAnother(value); } } for (Field field : o.getClass().getDeclaredFields()) { int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) { writer.key(field.getName()); json.convertAnother(field.get(o)); } } writer.endObject(); } catch (ConverterException ce) { throw ce; } catch (Exception e) { throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e); } }
From source file:com.glaf.core.util.Tools.java
public static Map<String, Class<?>> getPropertyMap(Class<?> clazz) { Map<String, Class<?>> dataMap = new java.util.HashMap<String, Class<?>>(); PropertyDescriptor[] propertyDescriptor = BeanUtils.getPropertyDescriptors(clazz); for (int i = 0; i < propertyDescriptor.length; i++) { PropertyDescriptor descriptor = propertyDescriptor[i]; String propertyName = descriptor.getName(); if (propertyName.equalsIgnoreCase("class")) { continue; }//from w ww .j a v a 2s.c o m dataMap.put(propertyName, descriptor.getPropertyType()); } return dataMap; }
From source file:com.blogspot.chicchiricco.persistence.AbstractBaseBean.java
/** * @return fields to be excluded when computing equals() or hashcode() *///from w w w. j a v a 2 s . com private String[] getExcludeFields() { Set<String> excludeFields = new HashSet<String>(); PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(getClass()); for (int i = 0; i < propertyDescriptors.length; i++) { if (propertyDescriptors[i].getPropertyType().isInstance(Collections.EMPTY_SET) || propertyDescriptors[i].getPropertyType().isInstance(Collections.EMPTY_LIST)) { excludeFields.add(propertyDescriptors[i].getName()); } } return excludeFields.toArray(new String[] {}); }
From source file:org.jdal.dao.BeanFilter.java
/** * {@inheritDoc}//ww w . j a v a2 s .co m */ public Map<String, Object> getParameterMap() { PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(getClass()); Map<String, Object> map = new HashMap<String, Object>(); for (PropertyDescriptor pd : pds) { if (!ignoredProperties.contains(pd.getName())) try { map.put(pd.getName(), pd.getReadMethod().invoke(this, (Object[]) null)); } catch (Exception e) { log.error(e); } } return map; }
From source file:org.zkybase.cmdb.test.AbstractBeanTestCase.java
@Test public void accessors() throws Exception { PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(beanClass); for (PropertyDescriptor prop : props) { String propName = prop.getName(); if ("class".equals(propName)) { continue; }/*from w ww. java2 s.c om*/ log.debug("Testing property accessors: {}.{}", beanClass, prop.getName()); Method readMethod = prop.getReadMethod(); Method writeMethod = prop.getWriteMethod(); assertNull(readMethod.invoke(bean)); Object dummyValue = getDummyValue(prop.getPropertyType()); writeMethod.invoke(bean, dummyValue); assertSame(dummyValue, readMethod.invoke(bean)); } }
From source file:com.expedia.seiso.domain.service.impl.ItemMerger.java
/** * Merges a source item into a destination item so we can write the destination item to the database. * /* ww w . j a v a 2 s .c om*/ * @param src * Source item * @param dest * Destination item * @param mergeAssociations * Flag indicating whether to merge the associations. */ public void merge(Item src, Item dest, boolean mergeAssociations) { val itemClass = src.getClass(); val propDescs = BeanUtils.getPropertyDescriptors(itemClass); for (val propDesc : propDescs) { val propName = propDesc.getName(); if (isMergeable(propName)) { val propClass = propDesc.getPropertyType(); if (BeanUtils.isSimpleProperty(propClass)) { mergeSimpleProperty(src, dest, propDesc); } else if (Item.class.isAssignableFrom(propClass)) { if (mergeAssociations) { mergeSingleAssociation(src, dest, propClass, propName); } } else if (List.class.isAssignableFrom(propClass)) { // Skip lists. No warning need. } else { log.warn("Property '{}' has unrecognized class {}; skipping", propName, propClass.getSimpleName()); } } } }