简体   繁体   English

如何获取嵌入式类的所有字段?

[英]How do I get all fields of embedded classes?

I have an instance of class Account (let's call it entity):我有一个 Account 类的实例(我们称之为实体):

private String accountType;
private List<String> attributes = new ArrayList<>();
private Date createdDate;
private ContactInfo contactInfo;
private AccountStatus accountStatus;

As you can see there are classes "ContactInfo" and "AccountStatus" inside.如您所见,里面有“ContactInfo”和“AccountStatus”类。 How can I get all fields in Account class and all it's used classes providing that I have the entity?如果我拥有实体,如何获取 Account 类中的所有字段及其所有使用的类?

This is what I write now, which only return all the fields in given entity.这就是我现在写的,它只返回给定实体中的所有字段。

private static <T> List<Field> getFields(T entity) {
  List<Field> fields = new ArrayList<>();
  Class clazz = entity.getClass();
  PropertyDescriptor[] propertyDescriptors;
  try {
    propertyDescriptors = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
  } catch (IntrospectionException e) {
    // do something
  }
  for (PropertyDescriptor pd : propertyDescriptors) {
    Field field = null;
    Class klass = clazz;
    while (klass != null && field == null) {
      try {
        field = klass.getDeclaredField(pd.getName());
      } catch (NoSuchFieldException e) {
        klass = klass.getSuperclass();
      }
    }
    fields.add(field);
  }
  return fields;
}

Thanks to @Andreas new I have it solved:感谢@Andreas new 我已经解决了:

private static <T extends BaseEntity> List<Field> getFields(T entity) {
  List<Field> fields = new ArrayList<>();
  Class clazz = entity.getClass();
  PropertyDescriptor[] propertyDescriptors;
  try {
    propertyDescriptors = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
  } catch (IntrospectionException e) {
    return fields;
  }
  for (PropertyDescriptor pd : propertyDescriptors) {
    List<Field> subFields = getSettableFields(pd.getPropertyType());
    if (subFields.isEmpty()) {
      try {
        fields.add(clazz.getDeclaredField(pd.getName()));
      } catch (NoSuchFieldException e) {
        return fields;
      }
    } else {
      fields.addAll(subFields);
    }
  }
  return fields;
}
private static List<Field> getFields(Class<?> clazz) {
  List<Field> fields = new ArrayList<>();
  PropertyDescriptor[] propertyDescriptors;
  try {
    propertyDescriptors = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
  } catch (IntrospectionException e) {
    return fields;
  }
  for (PropertyDescriptor pd : propertyDescriptors) {
    try {
      fields.add(clazz.getDeclaredField(pd.getName()));
    } catch (NoSuchFieldException e) {
      return fields;
    }
  }
  return fields;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让Maven Surefire运行所有类和测试? - How do I get Maven Surefire to run all classes and tests? 如何获取存储在字段中的对象,然后获取其类,然后获取该类的字段,等等? (递归获取类的字段) - How do I get the object stored in a field, to then get its class, then get the fields of that class etc? (Recursively obtain fields of classes) 如何以优雅的方式初始化具有大量字段的类? - How do I initialize classes with lots of fields in an elegant way? 如何获取eclipse中Java项目中使用的所有类和方法的列表? - How do I get the list of all the classes and methods used in a Java project in eclipse? 我如何在jdk9中获得sun.font的所有类 - How do i get all the classes of sun.font in jdk9 如何获得所有由Cayenne管理的实体类的列表? - How do I get a list of all Cayenne-managed entity classes? 如何配置连接到几个都变得更具体的几个类的Java方法? - How do I configure this Java method connected to several classes that all get more specific? JMeter 因“检索所有嵌入式资源”而变慢 - 如何扩展? - JMeter slow with "Retrieve all embedded resources" - how do I scale? 如何配置嵌入式jetty服务器以记录所有请求? - How do I configure embedded jetty server to log all requests? 如何拥有一个复合 id,其中字段在多个嵌入式类中可用 - How to have a composite id where fields are available in multiple embedded classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM