简体   繁体   English

<java reflection> object.getClass().getDeclaredFields() 为空</java>

[英]<Java reflection> object.getClass().getDeclaredFields() is empty

I have a setup where I want to have a method to handle different report templates (each template have less/more fields than the others) by passing in the report name and create the object at runtime.我有一个设置,我希望通过传入报告名称并在运行时创建 object 来处理不同的报告模板(每个模板的字段比其他模板少/多)。 It shall then check if each field exists, and set the value if it does.然后它将检查每个字段是否存在,如果存在则设置值。 The object will then be serialized to JSON for return.然后 object 将被序列化为 JSON 用于返回。

I have a test setup as below.我有一个如下的测试设置。 The problem is that I cannot get a list of fields of the created object.问题是我无法获得创建的 object 的字段列表。 The object.getClass().getDeclaredFields() always give an empty array. object.getClass().getDeclaredFields() 总是给出一个空数组。

Would like to see if you can spot out any mistakes or if there is smarter way of doing this.想看看您是否可以发现任何错误,或者是否有更聪明的方法来做到这一点。

Main logic:主要逻辑:

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));
    }
}

Model: Model:

CustomerReservationReport客户预订报告

This is the parent class and all the fundamental report fields are here这是父 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客户预订报告基本

This would be one of the various kinds of reports.这将是各种报告中的一种。

public class CustomerReservationReportBasic extends CustomerReservationReport {
    public CustomerReservationReportBasic() {
        super();
    }
}

From the Javadoc for Class::getDeclaredFields()来自Class::getDeclaredFields()的 Javadoc

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.返回一个 Field 对象数组,反映由 class 或由此 Class object 表示的接口声明的所有字段。 This includes public, protected, default (package) access, and private fields, but excludes inherited fields .这包括公共、受保护、默认(包)访问和私有字段,但不包括继承字段

You will need to also get the fields of the object's superclass(es).您还需要获取对象超类的字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM