1.请使用sun 内省api得到某一个bean的所有属性,并操作bean的其中一个属性(给属性赋值,以及得到属性的值) 。

来源:互联网 发布:淘宝用微信怎么支付 编辑:程序博客网 时间:2024/06/07 00:19

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;
}

}


原创粉丝点击