欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaBean动态添加删除属性

程序员文章站 2022-04-14 18:58:13
1.cglib BeanGenerator beanGenerator = new BeanGenerator(); beanGenerator.addProperty("id", Long.class); beanGenerator.addProperty("username", String.c ......

 

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

从输出结果可以看出最后生成的obj只有id和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", nullnewdynaproperty[]{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

查看basicdynabean与basicdynaclass之间的关系

JavaBean动态添加删除属性

 

dynabean的源码

 

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);

}

主要是接口的定义

再来看看basicdynabean是怎么实现的,直接看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);

}

}

从以上代码可以看出是在values里取值的

/**

* the set of property values for this dynabean, keyed by property name.

*/

protected hashmap<string, object> values = new hashmap<string, object>();

其实是用hashmap来实现的.

JavaBean动态添加删除属性

 

3.总结

用cglib动态删除添加属性时,虽然obj里有getusername这个方法,却不能obj.getusername()这样直接调用,想得到username的值只能通过beanmap.get("username")获取.

org.apache.commons.beanutils从源码来看是使用hashmap来实现的.

两种方式从操作角度来说和使用map的区别不大.只是它们都提供了复制属性的工具方法.