apache-commons-beanutils

Convert a JavaBean to key/value Map with nested name using commons-beans BeanUtils

心已入冬 提交于 2019-12-11 02:29:46
问题 I start using BeanUtils to convert a Properties files to a JavaBean. Using BeanUtils.populate, I'm able to do this nicely. But I can achieve the retro conversion from the JavaBean to the Map correctly (only simple values are store). See this sample based on the Employee Class form the BeanUtils documentation. import org.apache.commons.beanutils.BeanUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; public class

Copy property with different names between beans using BeanUtils

删除回忆录丶 提交于 2019-12-10 17:50:02
问题 I would like to copy the property values from Class A to Class B with BeanUtils which has same fields but with different names . Is it possible to provide a map of property name to differentName, age to differentAge etc., and achieve the copying? I am interested to know if this is possible by any means using only Apache Commons utilities (not any other tools). class ClassA{ private String name; private Integer age; ... // Setter and Getter methods } class ClassB{ private String differentName;

Java Collections.sort - help me remove the unchecked warning

我怕爱的太早我们不能终老 提交于 2019-12-10 02:01:01
问题 List<Question> questions = new ArrayList<Question>(); questions.addAll(getAllQuestions()); //returns a set of Questions Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator Under Java 1.5, the above works fine except that the 'new BeanComparator("questionId")' generates an unchecked warning. I do not like warnings. Is there a way I can provide the BeanComparator a type, or do I have to use @SuppressWarnings("unchecked") ? 回答1: Options

BeanUtils.cloneBean() deep copy

拜拜、爱过 提交于 2019-12-09 07:55:38
问题 If all the objects within the bean implement Serializable interface, will BeanUtils.cloneBean() do a deep copy? 回答1: No, cloneBean() does shallow copy only. If you want deep copy. You may refer this link which has technique to do deep copy. 回答2: Use SerializationUtils.clone method from the Apache Commons Lang for the deep copy . It copies the entire class hierarchy. SerializationUtils.clone(object); 回答3: There is also another java library which supports both shallow cloning and deep cloning.

Java: Merge 2 “beans” to produce a new one

跟風遠走 提交于 2019-12-07 23:59:28
I need to take all the fields and collections from Bean1 and Bean2, sometimes apply some business logic, and produce Bean3 (all beans are hibernate/domain objects of the same type with a reasonably complex graph). Any thoughts on how to do this? Done something similar in the past? My ideas: Dozer (http://dozer.sourceforge.net/) BeanUtils (http://commons.apache.org/beanutils/) Handrolled solution A.N.Other cool solution? Any recommendations? Dozer is a nice bean mapping tool. However, it won't perform any business logic, of course. I should not be a problem to implement a business logic and to

What's the best way to register a Converter for all subclasses of a particular class when using Commons BeanUtils?

淺唱寂寞╮ 提交于 2019-12-07 11:15:43
问题 For example, if I wished to register a Converter for all instances of java.util.Map, is there some way of doing this: new BeanUtilsBean().getConvertUtils().register(new MyConverter(), Map.class); where the MyConverter#convert() method would be called for any instance of a Map (for instance a HashMap)? The background to this is that I'm using BeanUtils to populate various different beans from a database. Some of their properties are enums that implement a particular interface, and to set their

Is it possible automatically instantiation of a nested Property with Commons Bean Utils?

橙三吉。 提交于 2019-12-07 04:29:39
问题 I'm using PropertyUtils.setProperty(object, name, value) method of Apache Commons Bean Utils: Giving these classes: public class A { B b; } public class B { C c; } public class C { } And this: A a = new A(); C c = new C(); PropertyUtils.setProperty(a, "b.c", c); //exception If I try that I get: org.apache.commons.beanutils.NestedNullException: Null property value for 'b.c' on bean class 'class A ' Is it possible to tell PropertyUtils that if a nested property has a null value try to

Performance of BeanUtils vs. ReflectionToStringBuilder (for use in Bean classes)

喜你入骨 提交于 2019-12-07 01:31:01
问题 I have a large number of Java bean classes in my web application, and I am trying to find a simple way to implement the toString() methods in these beans. The toString() method would be used for logging throughout the application, and should print the attribute-value pairs of all attributes in the bean. I am trying out two alternatives: 1. BeanUtils.describe() (Apache commons-beanutils) 2. ReflectionToStringBuilder.toString() (Apache commons-lang) Since this is a web application expected to

how find fields of all member variables contained in java bean

痞子三分冷 提交于 2019-12-06 15:33:51
问题 I want to make a GUI using Java in which a user can select a bean, edit its fields, and then add an instance of the created bean to a queue. My question though is about accessing the fields. I have a class MyCompositeObject that inherits from MyParentObject. The MyParentObject is composed of multiple beans, each being composed of more beans. The class MyCompositeObject is also composed of beans. I want to find all accessible fields from MyCompositeObject. Class MyParentObject { MyObjectOne

What's the best way to register a Converter for all subclasses of a particular class when using Commons BeanUtils?

梦想的初衷 提交于 2019-12-05 16:44:36
For example, if I wished to register a Converter for all instances of java.util.Map, is there some way of doing this: new BeanUtilsBean().getConvertUtils().register(new MyConverter(), Map.class); where the MyConverter#convert() method would be called for any instance of a Map (for instance a HashMap)? The background to this is that I'm using BeanUtils to populate various different beans from a database. Some of their properties are enums that implement a particular interface, and to set their values a custom routine is needed. I'd hoped to register a single converter class for all