<Java reflection> object.getClass().getDeclaredFields() 为空
<Java reflection> object.getClass().getDeclaredFields() is empty
我有一个设置,我希望有一个方法通过传入报告名称并在运行时创建对象来处理不同的报告模板(每个模板都有 less/more 字段)。然后它将检查每个字段是否存在,如果存在则设置值。然后对象将被序列化为 JSON for return.
我有一个测试设置如下。问题是我无法获得创建对象的字段列表。 object.getClass().getDeclaredFields() 总是给出一个空数组。
想看看您是否能找出任何错误,或者是否有更聪明的方法。
主要逻辑:
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
public class Test {
public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
Class<?> cls = Class.forName("CustomerReservationReportBasic");
CustomerReservationReport customerReservationReport = (CustomerReservationReport) cls.getDeclaredConstructor()
.newInstance();
System.out.println(hasField(customerReservationReport, "name"));
}
public static boolean hasField(Object object, String fieldName) {
return Arrays.stream(object.getClass().getDeclaredFields()).anyMatch(f -> f.getName().equals(fieldName));
}
}
型号:
客户预订报告
这是父级 class,所有基本报告字段都在这里
import java.math.BigDecimal;
import lombok.Data;
@Data
public abstract class CustomerReservationReport implements Comparable<CustomerReservationReport> {
private String name;
private int num_of_visit;
private BigDecimal total_spend;
@Override
public int compareTo(CustomerReservationReport customerReservationReport) {
return this.getName().compareTo(customerReservationReport.getName());
}
}
CustomerReservationReportBasic
这将是各种报告中的一种。
public class CustomerReservationReportBasic extends CustomerReservationReport {
public CustomerReservationReportBasic() {
super();
}
}
来自 Class::getDeclaredFields()
的 Javadoc
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
您还需要获取对象超类的字段。
我有一个设置,我希望有一个方法通过传入报告名称并在运行时创建对象来处理不同的报告模板(每个模板都有 less/more 字段)。然后它将检查每个字段是否存在,如果存在则设置值。然后对象将被序列化为 JSON for return.
我有一个测试设置如下。问题是我无法获得创建对象的字段列表。 object.getClass().getDeclaredFields() 总是给出一个空数组。
想看看您是否能找出任何错误,或者是否有更聪明的方法。
主要逻辑:
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
public class Test {
public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
Class<?> cls = Class.forName("CustomerReservationReportBasic");
CustomerReservationReport customerReservationReport = (CustomerReservationReport) cls.getDeclaredConstructor()
.newInstance();
System.out.println(hasField(customerReservationReport, "name"));
}
public static boolean hasField(Object object, String fieldName) {
return Arrays.stream(object.getClass().getDeclaredFields()).anyMatch(f -> f.getName().equals(fieldName));
}
}
型号:
客户预订报告
这是父级 class,所有基本报告字段都在这里
import java.math.BigDecimal;
import lombok.Data;
@Data
public abstract class CustomerReservationReport implements Comparable<CustomerReservationReport> {
private String name;
private int num_of_visit;
private BigDecimal total_spend;
@Override
public int compareTo(CustomerReservationReport customerReservationReport) {
return this.getName().compareTo(customerReservationReport.getName());
}
}
CustomerReservationReportBasic
这将是各种报告中的一种。
public class CustomerReservationReportBasic extends CustomerReservationReport {
public CustomerReservationReportBasic() {
super();
}
}
来自 Class::getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
您还需要获取对象超类的字段。