下面是关于DynaBean的一个简单实现:
1、属性类:
package test;
public class MyDynaProp {
private String name;
private Class<?> type;
public MyDynaProp(String name, Class<?> type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
}
2、Class接口:
package test;
public interface MyDynaClass {
public MyDynaBean newInstance() throws Exception;
public MyDynaProp getDynaProp(String name);
public MyDynaProp[] getProps();
}
3、Class接口基本实现类:
package test;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
public class MyBasicDynaClass implements MyDynaClass {
private String name;
private MyDynaProp[] props =new MyDynaProp[0];
protected Map<String, MyDynaProp> propMap = new HashMap<String, MyDynaProp>();
protected static Class<?>[] constructorTypes = { MyDynaClass.class };
protected Object[] constructorValues = { this };
protected Class<?> dynaBeanClass = MyBasicDynaBean.class;
public MyBasicDynaClass(String name, MyDynaProp props[]) {
this.name = name;
this.props = props;
this.propMap.clear();
for(MyDynaProp prop:props){
this.propMap.put(prop.getName(), prop);
}
}
public String getName() {
return name;
}
public MyDynaProp[] getProps() {
return props;
}
public MyDynaBean newInstance() throws Exception {
Constructor<?> constructor = this.dynaBeanClass.getConstructor(constructorTypes);
return (MyDynaBean) constructor.newInstance(constructorValues);
}
public MyDynaProp getDynaProp(String name) {
return this.propMap.get(name);
}
}
4、Bean接口:
package test;
public interface MyDynaBean {
public void set(String name, Object value);
public Object get(String name);
}
5、Bean接口实现类:
package test;
import java.util.HashMap;
import java.util.Map;
public class MyBasicDynaBean implements MyDynaBean {
protected MyDynaClass dynaClass = null;
protected Map<String, Object> values = new HashMap<String, Object>();
public MyBasicDynaBean(MyDynaClass cls) {
dynaClass = cls;
}
public Object get(String name) {
return this.values.get(name);
}
public void set(String name, Object value) {
MyDynaProp dynaProp = this.dynaClass.getDynaProp(name);
if (dynaProp == null || !this.isAssignable(dynaProp.getType(), value.getClass())) {
throw new IllegalArgumentException("IllegalArgumentException");
}
this.values.put(name, value);
}
/** *//** 验证是否可赋值 */
protected boolean isAssignable(Class<?> dest, Class<?> source) {
boolean value = dest.isAssignableFrom(source) || (dest == Integer.TYPE && source == Integer.class)
|| (dest == Boolean.TYPE && source == Boolean.class);
return value;
}
public String toString() {
MyDynaProp props[] = this.dynaClass.getProps();
StringBuffer sbValue = new StringBuffer();
for (MyDynaProp prop : props) {
sbValue.append(prop.getName()).append(":").append(values.get(prop.getName())).append(",");
}
return sbValue.toString();
}
}
6、测试:
package test;
import java.util.Date;
public class Test {
public static void main(String[] args) {
MyDynaProp[] props = { new MyDynaProp("name", String.class), new MyDynaProp("age", Integer.TYPE),
new MyDynaProp("date", Date.class), new MyDynaProp("isHero", Boolean.TYPE) };
MyBasicDynaClass cls = new MyBasicDynaClass("user", props);
try {
MyDynaBean bean = cls.newInstance();
bean.set("name", "SuperStar");
bean.set("age", 12);
bean.set("date", new Date());
bean.set("isHero", true);
System.out.println(bean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出如下:
name:SuperStar,age:12,date:Mon May 04 21:31:19 CST 2009,isHero:true,