package com.hbsi.homework;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class IntrospectorDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//通过内省获取person bean的所有属性
@Test
public void test1() throws IntrospectionException{
BeanInfo bi=Introspector.getBeanInfo(Student.class,Object.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
System.out.println(name);
}
}
//通过内省给person的name属性赋值:abc setName("abc")
@Test
public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Student s=new Student();
BeanInfo bi=Introspector.getBeanInfo(Student.class);
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
String name=pd.getName();
if(name.equals("name")){
Method m=pd.getWriteMethod();
m.invoke(s, "abc");
}
}
System.out.println(s.getName());
}
//通过PropertyDescriptor类操作Bean的属性 name属性赋值:aaaa setName("aaaa")
@Test
public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Student s=new Student();
PropertyDescriptor pd=new PropertyDescriptor("name", s.getClass());
Method m=pd.getWriteMethod();
m.invoke(s,"aaaa");
System.out.println(s.getName());
}
@Test
public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Student s=new Student();
s.setName("abcdef");
PropertyDescriptor pd=new PropertyDescriptor("name",s.getClass());
Method m=pd.getReadMethod();
String str=(String)m.invoke(s, null);
System.out.println(str);
}
}
package com.hbsi.homework;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class BeanutilsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
Student s=new Student();
BeanUtils.setProperty(s,"name","张三");
System.out.println(s.getName());
}
//使用beanUtils中的转换器完成数据转换
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Student s=new Student();
BeanUtils.setProperty(s,"birthday","1992-10-09");
System.out.println(s.getBirthday().toLocaleString());
}
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
//自定义转换器
ConvertUtils.register(new Converter(){
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只能转String数据");
}
String s=(String)value;
if(s.trim().equals("")){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d=sdf.parse(s);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new ConversionException("转换错误");
}
}
}, Date.class);
String name="张三";
String age="23";
String birthday="1992-10-09";
Student s=new Student();
BeanUtils.setProperty(s,"name",name);
BeanUtils.setProperty(s,"age",age);
BeanUtils.setProperty(s,"birthday",birthday);
System.out.println("姓名:"+s.getName()+" 年龄:"+s.getAge()+" 出生日期:"+s.getBirthday().toLocaleString());
}
}