开发框架时,经常要使用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内省思维导图,全方面介绍java内省的功能以及用法用处。
主要介绍了Java 内省(Introspector)深入理解的相关资料,需要的朋友可以参考下
NULL 博文链接:https://newjueqi.iteye.com/blog/397132
主要介绍了Java内省实例解析,具有一定借鉴价值,需要的朋友可以参考下
JAVA的内省机制(introspector)与反射机制(reflection).docx
自动拆箱与装箱,泛型,内省,注释,加载器,代理等。
主要介绍了Java 内省introspector相关原理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
虚拟机内省技术研究,杨先强,双锴,随着云计算的流行,虚拟机内省(Virtual Machine Introspection, VMI)技术及工具逐渐被用来监控IaaS云中虚拟机的行为。在这些技术的帮助下,�
JAVA反射机制、注解、动态加载类、动态调用对象方法
【IT十八掌徐培成】Java基础第25天-03.递归获取所有超类-内省获取所有属性-getter-setter.zip
NULL 博文链接:https://tianyami.iteye.com/blog/1576854
java反射全解(反射原理+反射API详解+反射与数组+反射与泛型+反射源码与性能开销+反射优缺点+反射与内省)
内省机制
ClassMate是一个零依赖Java库,用于准确地内省类型信息,包括可靠地解析类(“类型”)和成员(字段,方法和构造函数)的泛型类型声明。 该项目已获得许可。 状态 支持 社区支持 社区通过邮件列表支持Classmate: ...
Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to...
小议布赖恩·温的公众理解科学理论研究内省模型.doc
JavaBean及内省,介绍了JavaBean的概念、属性、使用、执行原理以及对内省的介绍
java有着一个非常突出的动态相关机制:...这种“看透class”的能力(the ability of the program to examine itself)被称为introspection(内省、内观、反省)。Reflection和introspection是常被并提的两个术语。
摘要 Reflection 是Java被视为动态(或准动态)语言的一个关键性质。这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其...Introspection(内省、内观) Reflection(反射)