25,980
社区成员
发帖
与我相关
我的任务
分享
protected <T> String getCountField(Class<T> clazz) {
String out = "o";
try {
//取得bean(clazz)类的信息,通过getPropertyDescriptors方法获取属性描述数组对象.
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
//对bean(clazz)类的属性列表进行循环.从数组中取去一个个的属性对象进行操作——获取相应的读写方法,
// 然后调用相应的读写方法。这些是通过反射机制做到的.
for (PropertyDescriptor propertydesc : propertyDescriptors) {
//取得bean(clazz)属性的propertydesc这个方法的属性值.返回getXXX()方法.
// 另外还有:getWriteMethod()方法,他是取得setXXX()方法的.他们是一对的.
Method method = propertydesc.getReadMethod();
//属性,方法是否为空的判断.
if (method != null && method.isAnnotationPresent(EmbeddedId.class)) {
//Introspector的作用是抽取JavaBean中的属性与方法、事件等
PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors();
//输入EmbeddedId.class的get方法名
out = "o." + propertydesc.getName() + "." + (!ps[1].getName().equals("class") ? ps[1].getName() : ps[0].getName());
break;
}
}
} catch (Exception e) {
//异常抛出.
e.printStackTrace();
}
return out;
}
protected<T> String getCountField(Class<T> clazz)
{
String out = "o";
try
{
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
foreach (PropertyDescriptor propertydesc in propertyDescriptors)
{
Method method = propertydesc.getReadMethod();
if (method != null && method.isAnnotationPresent(EmbeddedId.class))
{
PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors();
out = "o." + propertydesc.getName() + "." + (!ps[1].getName().equals("class") ? ps[1].getName() : ps[0].getName());
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}