Home > 类库下载 > java类库 > JavaBean dynamically adds and deletes attributes

JavaBean dynamically adds and deletes attributes

高洛峰
Release: 2016-10-15 10:29:45
Original
2808 people have browsed it

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
Copy after login

It can be seen from the output that the finally generated obj only has two attributes: id and username

2.org.apache.commons.beanutils

DynaProperty property = new DynaPropert

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:1username:name1
Copy after login

View BasicDynaBean and BasicDynaClass The relationship between

JavaBean dynamically adds and deletes attributes

DynaBean’s 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);


}
Copy after login

is mainly the definition of the interface

Let’s take a look at how BasicDynaBean is implemented, directly look 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&#39;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);
    }

}
Copy after login

It can be seen from the above code The value is obtained from values

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

It is actually implemented using HashMap.

3. Summary

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

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

The two methods are the same from an operational perspective. There is not much difference in using Map. It’s just that they both provide tool methods for copying attributes.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template