public static void main(String[] args) throws Exception {
MailForm bean = new MailForm();
Map propName = new HashMap();
propName.put("fromAddress", "su");
propName.put("money", "21");
Test t = new Test();
t.setProperties(bean, propName);
System.out.println(bean.getFromAddress() + " " + bean.getMoney());
}
public static void setProperties(Object bean, Map properties) {
try {
for (Iterator iter = properties.keySet().iterator(); iter.hasNext();) {
String propName = (String) iter.next();
PropertyDescriptor descriptor = new PropertyDescriptor(
propName, bean.getClass());
Class propertyType = descriptor.getPropertyType();
Object value = decode(propertyType, (String) properties
.get(propName));
descriptor.getWriteMethod()
.invoke(bean, new Object[] { value });
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Object decode(Class type, String value) throws Exception {
if (type.getName().equals("java.lang.String")) {
return value;
}
if (type.getName().equals("boolean")) {
return Boolean.valueOf(value);
}
if (type.getName().equals("int")) {
return Integer.valueOf(value);
}
if (type.getName().equals("long")) {
return Long.valueOf(value);
}
if (type.getName().equals("float")) {
return Float.valueOf(value);
}
if (type.getName().equals("double")) {
return Double.valueOf(value);
}
if (type.getName().equals("java.awt.Color")) {
StringTokenizer tokens = new StringTokenizer(value, ",");
int red = Integer.parseInt(tokens.nextToken());
int green = Integer.parseInt(tokens.nextToken());
int blue = Integer.parseInt(tokens.nextToken());
return new Color(red, green, blue);
}
if (type.getName().equals("java.lang.Class")) {
return Class.forName(value);
}
return null;
}
分享到:
相关推荐
主要为大家详细介绍了Java通过PropertyDescriptor反射调用set和get方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
//import java.beans.PropertyDescriptor; import com.googlecode.openbeans.BeanInfo; import com.googlecode.openbeans.IntrospectionException; import com.googlecode.openbeans.Introspector; import ...
PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz); Method mt = pd.getWriteMethod(); Type t [] = ms[i].getParameterTypes(); Object [] params = new Object[t.length]; for(int j...
在由JDK提供的默认的API中,有java.beans包,里面有诸如Introspector,BeanInfo,PropertyDescriptor等用来操作JavaBean的方法, 但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志...
C# WinForm 工作中遇到一个需要将界面表格数据按照设定的格式[表头|...完整版还有自动反射字段中文名称方便客户自己编辑 时间匆忙就懒得分离代码上传 了 原理很简单 字段自定义属性[PropertyDescriptor] 然后反射就好了
6.2. PropertyDescriptor 6.3. ElementDescriptor 6.4. ConstraintDescriptor 7. 与其他框架集成 7.1. OSGi 7.2. 与数据库集成校验 7.3. ORM集成 7.3.1. 基于Hibernate事件模型的校验 7.3.2. JPA 7.4. 展示层校验 8....
C#自定义控件,DataGridView多维表头.
Java基础类库,也就是Java doc里面看到的所有的类的class文件。
在android studio 中无法使用 BeanInfo,IntrospectionException,Introspector, PropertyDescriptor 这些类,只需要导入这个jar 库就可以了。 只不过,要把java.beans.BeanInfo; 替换为 ...
* @param propertyKey 元素名称 * @param descriptor 要应用装饰器的方法的描述符 */function Admin ( target : any , propertyKey : string | symbol , descriptor : PropertyDescriptor ) { let originalMethod = ...
对于对象内的属性,JavaScript提供了一个属性描述器接口PropertyDescriptor,大部分开发者并不需要直接使用它,但是很多框架和类库内部实现使用了它,如avalon.js,Vue.js,本篇介绍属性描述器及相关应用。...
PropertyDescriptor. Not all classes of this package are supported in Android. See javadoc of Android's java.beans package. There is a project called android-logging-log4j, which provides a ...
import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * */ /** * @ClassName: ReflectTest * @Description: TODO(这里用一句话描述这个类的作用...