protected <T> String getCountField(Class<T> clazz){
String out = "o";
try {
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
for(PropertyDescriptor propertydesc : 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;
}

解决方案 »

  1.   

    楼主能不能把代码贴得规范一点?csdn中又不是没有专门用于贴代码的功能的.
      

  2.   


    protected <T> String getCountField(Class <T> clazz){ 
    String out = "o"; 
    try { 
    PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); 
    for(PropertyDescriptor propertydesc : 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; 
    }
      

  3.   

    把import也贴出来。否则不清楚你引用的哪个类。
    简单来说,这个逻辑不难。
    for(PropertyDescriptor propertydesc : propertyDescriptors)
    是一个遍历ARRAY的循环。
     (!ps[1].getName().equals("class")? ps[1].getName(): ps[0].getName())
    是一个三元表达式。
      

  4.   


    package cn.itcast.service.base;import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.io.Serializable;
    import java.lang.reflect.Method;
    import java.util.LinkedHashMap;import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;import cn.itcast.bean.QueryResult;
    import cn.itcast.utils.GenericsUtils;
    protected static <E> String getCountField(Class<E> clazz){
    String out = "o";
    try {
    PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
    for(PropertyDescriptor propertydesc : 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;
    }