jdk6.0从入门到精通-----chapter16反射机制---javabean

本文介绍了一个简单的JavaBean实例,包括其属性及对应的getter和setter方法。此外,还提供了一个工具类用于获取属性类型和方法返回类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

javabean就是简单的java类

package Beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

public class TestBean
{
public int id;
public String name;
public boolean joined;
public Date birthday;

public int getId()
{
return id;
}

public void setId(int id)
{
this.id=id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name=name;
}

public boolean isJoined()//也可以是getJoined()
{
return joined;
}

public void setJoined(boolean joined)
{
this.joined=joined;
}

public Date getBirthday()
{
return birthday;
}

public void setBirthday(Date birthday)
{
this.birthday=birthday;
}

public String getGreeting()
{
return "Hello "+this.name;
}

public List<TestBean> getTestBeans()
{
List<TestBean> testBeanList=new ArrayList<TestBean>();
return testBeanList;
}
}


package Beans;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TestBeansUtil {
public static Class findPropertyType(String propertyName, Class clazz)
throws IntrospectionException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz);

System.out.println(" getter=" + pd.getReadMethod());
System.out.println(" setter=" + pd.getWriteMethod());

if (pd != null)
return pd.getPropertyType();
else
return Object.class;
}

public static Class getMethodType(Method method) {
Class clazz = method.getReturnType();
return clazz;
}

public static Class getMethodGenericType(Method method) {

Type type = method.getGenericReturnType();

if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
return (Class) ptype.getActualTypeArguments()[0];
}

return Object.class;
}

public static void main(String[] args) throws IntrospectionException,
ClassNotFoundException, IllegalAccessException,
NoSuchMethodException {
// 获取id属性的类型
System.out.println(TestBeansUtil.findPropertyType("id", TestBean.class)
.getName());
// 获取name属性的类型
System.out.println(TestBeansUtil.findPropertyType("name",
TestBean.class).getName());
// 获取joined属性的类型
System.out.println(TestBeansUtil.findPropertyType("joined",
TestBean.class).getName());
// 获取birthday属性的类型
System.out.println(TestBeansUtil.findPropertyType("birthday",
TestBean.class).getName());
// 获取getGreeting()方法的返回值类型
Method method1 = Class.forName("TestBean").getMethod("getGreeting",
new Class[] {});
System.out.println(TestBeansUtil.getMethodType(method1).getName());
// 获取getTestBeans()方法的返回值类型和泛型
Method method2 = Class.forName("TestBean").getMethod("getTestBeans",
new Class[] {});
System.out.println(TestBeansUtil.getMethodType(method2).getName());
System.out.println(TestBeansUtil.getMethodGenericType(method2)
.getName());
}
}


执行结果
		getter=public int Beans.TestBean.getId()
setter=public void Beans.TestBean.setId(int)
int
getter=public java.lang.String Beans.TestBean.getName()
setter=public void Beans.TestBean.setName(java.lang.String)
java.lang.String
getter=public boolean Beans.TestBean.isJoined()
setter=public void Beans.TestBean.setJoined(boolean)
boolean
getter=public java.util.Date Beans.TestBean.getBirthday()
setter=public void Beans.TestBean.setBirthday(java.util.Date)
java.util.Date
java.lang.String
java.util.List
Beans.TestBean
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值