1.請使用sun 內省api得到某一個bean的所有屬性,並操作bean的其中一個屬性(給屬性賦值,以及得到屬性的值) 。

package com.hbsi.beanutils;

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 Demo1 {

@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
  Person p=new Person();
  BeanUtils.setProperty(p,"name","張三");
  System.out.println(p.getName());
 
}

//beanutils工具對基本數據類型可以自動轉換類型
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
  //自定義轉換器
  ConvertUtils.register(new Converter(){

   @Override
   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="     ";
 
  Person p=new Person();
  BeanUtils.setProperty(p,"name",name);
  BeanUtils.setProperty(p,"age",age);
  BeanUtils.setProperty(p,"birthday",birthday);
 
  System.out.println(p.getName()+"..."+p.getAge()+"...");
 
 
}
//使用beanUtils中的轉換器完成數據轉換
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
 
  ConvertUtils.register(new DateLocaleConverter(), Date.class);
 
  Person p=new Person();
  BeanUtils.setProperty(p,"birthday","   ");
  //System.out.println(p.getBirthday());
 
}

}

/*class MyConver implements Converter{

@Override
public Object convert(Class arg0, Object arg1) {
  // TODO Auto-generated method stub
  return null;
}

}*/

package com.hbsi.beanutils;

import java.util.Date;

public class Person {

private String name;
private int age;
private Date birthday;

public Date getBirthday() {
  return birthday;
}
public void setBirthday(Date birthday) {
  this.birthday = birthday;
}
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
}

public String getAb(){
  return null;
}

}

package com.hbsi.introspector;

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 Demo1 {

/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub

}
//通過內省獲取person bean的所有屬性
@Test
public void test1() throws Exception{
  BeanInfo bi=Introspector.getBeanInfo(Person.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{
 
  Person p=new Person();
 
  BeanInfo bi=Introspector.getBeanInfo(Person.class);
 
  PropertyDescriptor[] pds=bi.getPropertyDescriptors();
 
  for(PropertyDescriptor pd:pds){
   String name=pd.getName();
   if(name.equals("name")){
    Method m=pd.getWriteMethod();
    m.invoke(p, "abc");
   }
  }
  System.out.println(p.getName());
}


//通過PropertyDescriptor類操作Bean的屬性  name屬性賦值:ffff   setName("abc")

@Test
public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
 
  Person p=new Person();
 
  PropertyDescriptor pd=new PropertyDescriptor("name", p.getClass());
 
  Method m=pd.getWriteMethod();
 
  m.invoke(p,"ffff");
 
  System.out.println(p.getName());
 
}

@Test
public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  Person p=new Person();
  p.setName("abcdef");
 
  PropertyDescriptor pd=new PropertyDescriptor("name",p.getClass());
 
  Method m=pd.getReadMethod();
 
  String str=(String)m.invoke(p, null);
  System.out.println(str);
}


}

package com.hbsi.introspector;

public class Person {

private String name;
private int age;
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
}

public String getAb(){
  return null;
}

}


發佈了55 篇原創文章 · 獲贊 21 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章