`
sugongqing
  • 浏览: 77364 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

PropertyDescriptor

    博客分类:
  • java
阅读更多
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方法

    主要为大家详细介绍了Java通过PropertyDescriptor反射调用set和get方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    openbeans1.0.zip

    //import java.beans.PropertyDescriptor; import com.googlecode.openbeans.BeanInfo; import com.googlecode.openbeans.IntrospectionException; import com.googlecode.openbeans.Introspector; import ...

    根据数据库表自动产生javabean

    PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz); Method mt = pd.getWriteMethod(); Type t [] = ms[i].getParameterTypes(); Object [] params = new Object[t.length]; for(int j...

    beanutils-1.9.3-bin

    在由JDK提供的默认的API中,有java.beans包,里面有诸如Introspector,BeanInfo,PropertyDescriptor等用来操作JavaBean的方法, 但是由Apache公司开发的BeanUtils会更常用,同时,BeanUtils还需要配合第三方日志...

    C# WinForm 利用NPOI 自定义[表头|列表|表尾]设计导出Execl格式源码【原创】【可直接运行】

    C# WinForm 工作中遇到一个需要将界面表格数据按照设定的格式[表头|...完整版还有自动反射字段中文名称方便客户自己编辑 时间匆忙就懒得分离代码上传 了 原理很简单 字段自定义属性[PropertyDescriptor] 然后反射就好了

    HibernateValidatorJSR303的参考实现使用指南.pdf

    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# dataGridVeiw多维表头

    C#自定义控件,DataGridView多维表头.

    java基础类库文件rt.jar

    Java基础类库,也就是Java doc里面看到的所有的类的class文件。

    openbeans.rar

    在android studio 中无法使用 BeanInfo,IntrospectionException,Introspector, PropertyDescriptor 这些类,只需要导入这个jar 库就可以了。 只不过,要把java.beans.BeanInfo; 替换为 ...

    learnTS:TypeScript项目开发

    * @param propertyKey 元素名称 * @param descriptor 要应用装饰器的方法的描述符 */function Admin ( target : any , propertyKey : string | symbol , descriptor : PropertyDescriptor ) { let originalMethod = ...

    从JavaScript属性描述器剖析Vue.js响应式视图

    对于对象内的属性,JavaScript提供了一个属性描述器接口PropertyDescriptor,大部分开发者并不需要直接使用它,但是很多框架和类库内部实现使用了它,如avalon.js,Vue.js,本篇介绍属性描述器及相关应用。...

    android-logging-log4j-1.0.2.jar

    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 ...

    Java中的反射机制

    import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * */ /** * @ClassName: ReflectTest * @Description: TODO(这里用一句话描述这个类的作用...

Global site tag (gtag.js) - Google Analytics