import java.beans.* for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) { if (pd.getReadMethod() != null && !"class".equals(pd.getName())) System.out.println(pd.getReadMethod().invoke(foo)); }
public static Map<String, Object> introspect(Object obj) throws Exception { Map<String, Object> result = new HashMap<String, Object>(); BeanInfo info = Introspector.getBeanInfo(obj.getClass()); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { Method reader = pd.getReadMethod(); if (reader != null) result.put(pd.getName(), reader.invoke(obj)); } return result; }
public static void copyBeanProperties( final Object source, final Object target, final Collection<String> includes){ final Collection<String> excludes = new ArrayList<String>(); final PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(source.getClass()); for(final PropertyDescriptor propertyDescriptor : propertyDescriptors){ String propName = propertyDescriptor.getName(); if(!includes.contains(propName)){ excludes.add(propName); } } BeanUtils.copyProperties( source, target, excludes.toArray(new String[excludes.size()])); }
BeanInfo info = Introspector.getBeanInfo(getMaxRateCode.getClass()); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println(pd.getName()+": "+pd.getReadMethod().invoke(getMaxRateCode));
MyBean bean = new MyBean(); BeanInfo beanInfo = Introspector.getBeanInfo(MyBean.class); for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) { String propertyName = propertyDesc.getName(); Object value = propertyDesc.getReadMethod().invoke(bean); }
public static String[] getNullPropertyNames (Object source) { final BeanWrapper src = new BeanWrapperImpl(source); java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set<String> emptyNames = new HashSet<String>(); for(java.beans.PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName()); } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); } // then use Spring BeanUtils to copy and ignore null public static void myCopyProperties(Object src, Object target) { BeanUtils.copyProperties(src, target, getNullPropertyNames(src)) }
public static Map<String, Object> mapProperties(Object bean) throws Exception { Map<String, Object> properties = new HashMap<>(); for (PropertyDescriptor property : Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors()) { String name = property.getName(); Object value = property.getReadMethod().invoke(bean); properties.put(name, value); } return properties; }
protected PropertyDescriptor getPropertyDescriptor(String name) { if (props == null) introspect(); for (int i = 0; i < props.length; i++) { if (name.equals(props[i].getName())) { return props[i]; } } return null; }
public class SimpleBean { private final String name = "SimpleBean"; private int size; public String getName() { return this.name; } public int getSize() { return this.size; } public void setSize( int size ) { this.size = size; } public static void main( String[] args ) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo( SimpleBean.class ); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println( pd.getName() ); } }
System.out.println( pd.getName() );
public static String findGetterName(Class clazz, String name) throws IntrospectionException, NoSuchFieldException, NoSuchMethodException { Method getter = findGetter(clazz, name); if (getter == null) throw new NoSuchMethodException(clazz+" has no "+name+" getter"); return getter.getName(); } public static Method findGetter(Class clazz, String name) throws IntrospectionException, NoSuchFieldException { BeanInfo info = Introspector.getBeanInfo(clazz); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) if (name.equals(pd.getName())) return pd.getReadMethod(); throw new NoSuchFieldException(clazz+" has no field "+name); } public static String findSetterName(Class clazz, String name) throws IntrospectionException, NoSuchFieldException, NoSuchMethodException { Method setter = findSetter(clazz, name); if (setter == null) throw new NoSuchMethodException(clazz+" has no "+name+" setter"); return setter.getName(); } public static Method findSetter(Class clazz, String name) throws IntrospectionException, NoSuchFieldException { BeanInfo info = Introspector.getBeanInfo(clazz); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) if (name.equals(pd.getName())) return pd.getWriteMethod(); throw new NoSuchFieldException(clazz+" has no field "+name); }
/** Set the system name. Fires a property change event. * @param s the new name * @exception IllegalArgumentException if the new name cannot represent * a valid node name */ @Override public void setName(String s) { String name = super.getName(); if ((name == null) || !name.equals(s)) { super.setName(s); fireNameChange(name, s); } }
public static String getFieldName(Method method) { try { Class<?> clazz=method.getDeclaringClass(); BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] props = info.getPropertyDescriptors(); for (PropertyDescriptor pd : props) { if(method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) { System.out.println(pd.getDisplayName()); return pd.getName(); } } } catch (IntrospectionException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
@RequestMapping(value = "/rest/user", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<?> updateUser(@RequestBody User user) { User existing = userRepository.read(user.getId()); copyNonNullProperties(user, existing); userRepository.save(user); // ... } public static void copyNonNullProperties(Object src, Object target) { BeanUtils.copyProperties(src, target, getNullPropertyNames(src)); } public static String[] getNullPropertyNames (Object source) { final BeanWrapper src = new BeanWrapperImpl(source); java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); Set<String> emptyNames = new HashSet<String>(); for(java.beans.PropertyDescriptor pd : pds) { Object srcValue = src.getPropertyValue(pd.getName()); if (srcValue == null) emptyNames.add(pd.getName()); } String[] result = new String[emptyNames.size()]; return emptyNames.toArray(result); }
public void getProperties(PropertyCallback callback, String prefix) { for (int i = 0; i < props.length; i++) { Method getter = props[i].getReadMethod(); if (getter == null) continue; if (!isHandledType(getter.getReturnType())) { //System.err.println("Ignoring " + props[i].getName() +" " + getter.getReturnType()); continue; } String name = props[i].getName(); try { Object result = getter.invoke(obj, NULL_ARG); //System.err.println("PROP " + name +": " + result); if (result != null) { callback.foundProperty(obj, prefix, name, result); } } catch (Exception ex) { LogLog.warn("Failed to get value of property " + name); } } }
public static void copyBeanProperties(final Object beanA, final Object beanB){ final BeanWrapper wrapperA = new BeanWrapperImpl(beanA); final BeanWrapper wrapperB = new BeanWrapperImpl(beanB); try{ for(final PropertyDescriptor descriptor : wrapperB .getPropertyDescriptors()){ final String propertyName = descriptor.getName(); if(wrapperB.getPropertyValue(propertyName) == null){ wrapperB.setPropertyValue(propertyName, wrapperA.getPropertyValue(propertyName)); } } } catch(final /* unchecked */ InvalidPropertyException e){ throw new IllegalArgumentException("Incompatible types: " + beanA + ", " + beanB, e); } }
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(beanClass).getPropertyDescriptors(); List<String> propertyNames = new ArrayList<String>(propertyDescriptors.length); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { propertyNames.add(propertyDescriptor.getName()); }
final BeanWrapper sourceBean = new BeanWrapperImpl(MyType.class); final PropertyDescriptor[] propertyDescriptors = sourceBean.getPropertyDescriptors(); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { logger.info(propertyDescriptor.getName() + ":" + propertyDescriptor.getPropertyType()); }
BeanInfo info = Introspector.getBeanInfo(Item.class); Item itm = new Item(); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { System.out.println(pd.getName() + " : " + pd.getReadMethod()); }
protected PropertyDescriptor getPropertyDescriptor(String name) { if (props == null) introspect(); for (int i = 0; i < props.length; i++) { if (name.equals(props[i].getName())) { return props[i]; } } return null; }