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