Java的内省技术

 

       开发框架时,经常要使用java对象的属性封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以SUN开发了一套API专门用于操作对象的属性。当然也可以使用第三方API,例如BeanUtils.

一、实体类 

package cn.moving.introspector;

import java.util.Date;

public class Person {

	private String name;
	private int age;
	private String password;

	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean getSex() {
		return true;
	}
}

二、通过Java的Introspector类实现内省

package cn.moving.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 {
/**
 * 访问JavaBean属性的两种方式:1、直接调用get和set方法;2、使用内省技术。
 * 内省技术也有:两种方式:
 * 1、通过PropertyDescriptor类操作bean的属性
 * 2、通过Introspector类获得Bean对象的BeanInfo,
 * 	然后通过BeanInfo来获取属性描述器(PropertyDescriptor)
 * 通过属性描述器可以获取某个属性的get和set方法,然后通过反射机制调用这些方法	
 * @throws IntrospectionException 
 */
	
	@Test
	//得到bean的所有属性
	public void test1() throws IntrospectionException{
		
		// BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
	 BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
		 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
		 for (PropertyDescriptor pd : pds) {
			System.out.println(pd.getName());
		};
	}
	
	@Test
	//操纵bean的指定属性age
	public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
		Method method = pd.getWriteMethod();
		Person p = new Person();
		method.invoke(p, 22);
		System.out.println(p.getAge());
		//获取属性的值
		method = pd.getReadMethod();
		System.out.println(method.invoke(p, null));;
		
	}
	
	//由于属性是封闭的,想了解内部属性,就要获得属性的类型,在赋值,取值
	@Test
	
	public void test3() throws IntrospectionException, RuntimeException, IllegalAccessException, InvocationTargetException{
		PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
		System.out.println(pd.getPropertyType());
		Method method = pd.getWriteMethod();
		Person p = new Person();
		method.invoke(p, "maomao");
		System.out.println(p.getName());
		
		method = pd.getReadMethod();
		System.out.println(method.invoke(p, null));;
	}
}

三、通过Apach的BeanUtils实现内省

package cn.moving.introspector;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
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 Beanutils {

	@Test
	public void test1() throws IllegalAccessException,
			InvocationTargetException {
		Person p = new Person();
		BeanUtils.setProperty(p, "age", 22);
		System.out.println(p.getAge());
	}

	@Test
	public void test2() throws IllegalAccessException,
			InvocationTargetException {
		String name = "ZhangWeiChen";
		String password = "123456";
		int age = 22;
		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);
		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
	}

	@Test
	public void test3() throws Exception, InvocationTargetException {
		String birthday = "1990-12-14";
		ConvertUtils.register(new Converter() {

			@Override
			public Object convert(Class type, Object value) {
				if (value == null)
					return null;
				if (!(value instanceof String)) {
					throw new RuntimeException("只支持String类型数据转换");
				}
				String str = (String) value;
				if (str.trim().equals(""))
					return null;
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
						Locale.US);
				try {
					return sdf.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
			}

		}, Date.class);
		Person p = new Person();
		BeanUtils.setProperty(p, "birthday", birthday);
		System.out.println(p.getBirthday());
		System.out.println(BeanUtils.getProperty(p, "birthday"));
	}

	@Test
	public void test4() throws Exception {
		Person p = new Person();
		Map map = new HashMap();
		map.put("name", "maomao");
		map.put("password", "123456");
		map.put("birthday", "1990-12-14");
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		BeanUtils.populate(p, map);
	}

}


 

你可能感兴趣的:(Java的内省技术)