注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

Jakarta commons cookbook 笔记(5)  

2013-03-21 19:45:33|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

BeanUtils and PropertyUtils 有大量对Java Bean的处理方法,使用起来非常方便。

package com.hjzheng.text.bean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.BeanMap;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.AllPredicate;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.lang.ArrayUtils;

public class TestBeanUtils {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
PhoneNumber pn1 = new PhoneNumber();
pn1.setCode(29);
pn1.setNumber("322765382");

PhoneNumber pn2 = new PhoneNumber();
pn2.setCode(29);
pn2.setNumber("445765382");

PhoneNumber pn3 = new PhoneNumber();
pn3.setCode(29);
pn3.setNumber("123765382");

Phone p1 = new Phone();
p1.setPhoneNumber(pn1);
p1.setPhoneName("HTC G12");
p1.setPhoneType("G12");
p1.setPhoneVendor("HTC");

Phone p2 = new Phone();
p2.setPhoneNumber(pn2);
p2.setPhoneName("LG F160L");
p2.setPhoneType("F160L");
p2.setPhoneVendor("LG");

Phone p3 = new Phone();
p3.setPhoneNumber(pn3);
p3.setPhoneName("NNNNN");

List<Phone> phones = new ArrayList<Phone>();
phones.add(p1);
phones.add(p2);
phones.add(p3);

Person p = new Person();
p.setFirstname("HaoJu");
p.setLastname("Zheng");
p.setPhones(phones);

NoteBook nb = new NoteBook();
nb.setName("youdao");
nb.setType("Web");

NoteBook nb2 = new NoteBook();
nb2.setName("youdao");
nb2.setType("Client");

Map<String,NoteBook> ns = new HashMap<String, NoteBook>();
ns.put("n1", nb);
ns.put("n2", nb2);

p.setNoteBooks(ns);

System.out.println(BeanUtils.getProperty(p, "phones[1].phoneName"));
System.out.println(BeanUtils.getProperty(p, "phones[1].phoneType"));
System.out.println(BeanUtils.getProperty(p, "phones[1].phoneNumber.number"));
System.out.println(BeanUtils.getProperty(p, "noteBooks(n1).type"));
BeanUtils.setProperty(p, "noteBooks(n2).name", "douban");
System.out.println(BeanUtils.getProperty(p, "noteBooks(n2).name"));

//BeanUtils getProperty and setProperty for list or array: list[0] for Map: map(key) for nest bean: attr1.attr2.attr3


//get bean type name, readMethod and writeMethod
PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(p, "phones[1].phoneNumber.number");
System.out.println(pd.getDisplayName());
System.out.println(pd.getName());
System.out.println(pd.getPropertyType());
Method readMethod = pd.getReadMethod();

//比较beans BeanComparator
BeanComparator bc = new BeanComparator("phoneName");
Collections.sort(phones, bc);
System.out.println(ArrayUtils.toString(phones));

//PropertyUtils.setProperty(p, "phones[0].phoneName", "Test");
//对复杂对象copy引用

//copy bean
Person pCopy = new Person();
BeanUtils.copyProperties(pCopy, p);
System.out.println(pCopy.getPhones().get(0).getPhoneName());
/*
* 除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,
* 作用与 BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能.
* copyProperties 还可以从Map中拷入bean,需要key和attribute Name对应
* */

Person pClone = (Person)BeanUtils.cloneBean(p);
PropertyUtils.copyProperties(pCopy, pClone);

System.out.println(pClone.getPhones().get(0).getPhoneName());

//检查属性的可访问性
System.out.println(PropertyUtils.isReadable(p, "phones[0].phoneName"));
System.out.println(PropertyUtils.isWriteable(p, "phones[0].phoneName"));

//使用Predicates验证Beans

Predicate nameHTC = new BeanPredicate("phones[0].phoneName", new EqualPredicate("HTC G12"));

Predicate[] preds = new Predicate[]{nameHTC};
Predicate allPreds = new AllPredicate(preds);
boolean bool = allPreds.evaluate(p);
System.out.println(bool);

//创建包含bean属性的Map
Map map = PropertyUtils.describe(p);
System.out.println(map.get("firstname"));
System.out.println(((List) map.get("phones")).get(0));
System.out.println(map.get("lastname"));

//BeanMap
BeanMap bm = new BeanMap(p);
Set keys = bm.keySet();
Iterator it = keys.iterator();
while (it.hasNext()){
String name = (String)it.next();
System.out.println(name + ": " + bm.get(name) + ": " + bm.getType(name));
}

//Dynamic Bean
DynaProperty[] beanProperties = new DynaProperty[]{
new DynaProperty("name", String.class),
new DynaProperty("langauge", String.class),
new DynaProperty("age", Integer.class)
};

BasicDynaClass stuffClass = new BasicDynaClass("stuff", BasicDynaBean.class, beanProperties);
DynaBean dyb = stuffClass.newInstance();
dyb.set("name", "hjzheng");
System.out.println(dyb.get("name"));

}
}

  评论这张
 
阅读(551)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017