Here is a method without any Spring Dependencies. You supply a bean object and a Map of property names to property values. It uses the JavaBeans introspector mechanism, so it should be close to Sun standards :
public static void assignProperties(
final Object bean,
final Map<String, Object> properties
){
try{
final BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
for(final PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()){
final String propName = descriptor.getName();
if(properties.containsKey(propName)){
descriptor.getWriteMethod().invoke(
bean,
properties.get(propName)
);
}
}
} catch(final IntrospectionException e){
// Bean introspection failed
throw new IllegalStateException(e);
} catch(final IllegalArgumentException e){
// bad method parameters
throw new IllegalStateException(e);
} catch(final IllegalAccessException e){
// method not accessible
throw new IllegalStateException(e);
} catch(final InvocationTargetException e){
// method throws an exception
throw new IllegalStateException(e);
}
}
Reference: