JavaBean dynamically adds and removes properties

1.cglib

BeanGenerator beanGenerator = new BeanGenerator();
beanGenerator.addProperty("id", Long.class);
beanGenerator.addProperty("username", String.class);
Object obj = beanGenerator.create();
BeanMap beanMap = BeanMap.create(obj);
BeanCopier copier = BeanCopier.create(User.class, obj.getClass(), false);
User user = new User();
user.setId(1L);
user.setUsername("name1");
user.setPassword("123");
copier.copy(user, obj, null);
System.out.println(beanMap.get("username"));
Class clazz = obj.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
    System.out.println(methods[i].getName());
}

输出结果:
name1
getId
getUsername
setId
setUsername

From the output, it can be seen that the final generated obj has only two attributes: id and username

2.org.apache.commons.beanutils

DynaProperty property = new DynaProperty("id", Long.class);
DynaProperty property1 = new DynaProperty("username", String.class);
BasicDynaClass basicDynaClass = new BasicDynaClass("user", null, new DynaProperty[]{property, property1});
BasicDynaBean basicDynaBean = new BasicDynaBean(basicDynaClass);
User user = new User();
user.setId(1L);
user.setUsername("name1");
user.setPassword("123");

BeanUtils.copyProperties(basicDynaBean, user);
Map<String, Object> map = basicDynaBean.getMap();
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
    String key = it.next();
    System.out.println(key + ":" + map.get(key));
}

输入结果:
id:1
username:name1

View the relationship between BasicDynaBean and BasicDynaClass

DynaBean source code

public interface DynaBean {

    public boolean contains(String name, String key);

    public Object get(String name);

    public Object get(String name, int index);

    public Object get(String name, String key);

    public DynaClass getDynaClass();

    public void remove(String name, String key);

    public void set(String name, Object value);

    public void set(String name, int index, Object value);

    public void set(String name, String key, Object value);


}

Mainly the definition of the interface

Let's take a look at how BasicDynaBean is implemented, look directly at public Object get(String name);


/**
 * Return the value of a simple property with the specified name.
 *
 * @param name Name of the property whose value is to be retrieved
 * @return The property's value
 *
 * @exception IllegalArgumentException if there is no property
 *  of the specified name
 */
public Object get(String name) {

    // Return any non-null value for the specified property
    Object value = values.get(name);
    if (value != null) {
        return (value);
    }

    // Return a null value for a non-primitive property
    Class<?> type = getDynaProperty(name).getType();
    if (!type.isPrimitive()) {
        return (value);
    }

    // Manufacture default values for primitive properties
    if (type == Boolean.TYPE) {
        return (Boolean.FALSE);
    } else if (type == Byte.TYPE) {
        return (new Byte((byte) 0));
    } else if (type == Character.TYPE) {
        return (new Character((char) 0));
    } else if (type == Double.TYPE) {
        return (new Double(0.0));
    } else if (type == Float.TYPE) {
        return (new Float((float) 0.0));
    } else if (type == Integer.TYPE) {
        return (new Integer(0));
    } else if (type == Long.TYPE) {
        return (new Long(0));
    } else if (type == Short.TYPE) {
        return (new Short((short) 0));
    } else {
        return (null);
    }

}

It can be seen from the above code that the value is taken in the values

    /**
     * The set of property values for this DynaBean, keyed by property name.
     */
    protected HashMap<String, Object> values = new HashMap<String, Object>();

In fact, it is implemented with HashMap.

3. Summary

When using cglib to dynamically delete and add attributes, although there is a getUsername method in obj, it cannot be called directly like obj.getUsername(). To get the value of username, you can only get it through beanMap.get("username").

org.apache.commons.beanutils is implemented using HashMap from the source code.

The two methods are not much different from using Map from an operational point of view. They only provide utility methods for copying properties.

For more information on copying properties, see:

java copy attribute

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325458055&siteId=291194637