Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将对象的字段和值复制到地图_Java - Fatal编程技术网

Java 将对象的字段和值复制到地图

Java 将对象的字段和值复制到地图,java,Java,我想从一个复杂的对象复制字段,这个对象包含其他对象。 现在它复制包装器类没有问题,但是如何复制子类的字段和值呢 代码 最后,我的问题是如何改进方法getValueMapFromInsuranceVehicle()以复制更多字段? 基本上如何使下面的代码工作 public Map<String, Object> getValueMapFromInsuranceVehicle(Long insuranceId) throws InvocationTargetException, Ill

我想从一个复杂的对象复制字段,这个对象包含其他对象。 现在它复制包装器类没有问题,但是如何复制子类的字段和值呢

代码

最后,我的问题是如何改进方法getValueMapFromInsuranceVehicle()以复制更多字段? 基本上如何使下面的代码工作

 public Map<String, Object> getValueMapFromInsuranceVehicle(Long insuranceId) throws InvocationTargetException, IllegalAccessException {
        InsurancePolicy insurance = repository.findById(insuranceId).get();

        Method[] methods = insurance.getClass().getMethods();  // insurance instead of vehicle
        Map<String, Object> map = new HashMap<String, Object>();
        for (Method m : methods) {
            if (m.getName().startsWith("get")) {
                Object value =  m.invoke(insurance); // insurance instead of insurancevehicle
                map.put(m.getName().substring(3), value);
            }
        }
        // add other fields specific to our needs like currentYear
        return map;
    }
public Map getValueMapFromInsuranceVehicle(Long insuranceId)抛出InvocationTargetException、IllegaAccessException{
InsurancePolicy insurance=repository.findById(insuranceId.get();
方法[]方法=insurance.getClass().getMethods();//保险代替车辆
Map Map=newhashmap();
用于(方法m:方法){
if(m.getName().startsWith(“get”)){
对象值=m.invoke(保险);//保险而不是保险车辆
map.put(m.getName().substring(3),value);
}
}
//添加其他特定于我们需求的字段,如currentYear
返回图;
}

要获取保险单中其他对象的方法,您可以使用相同的代码,但使用instanceOf添加一些检查,以确定该对象是InsuranceCustomer、InsuranceVehicle还是InsuranceCalculation,以及是否与方法[]methods=insurance.getClass().getMethods()使用相同的代码;已更改为对象类。我建议您将代码分成更多的方法,这样您就可以使用递归。

这是一种非常肮脏的使用getter的方法。但也许你已经知道这一点,并且有你的理由。我想指出一些危险的地方
m.getName().substring(3)
很容易,您可以使用两个getter使其名称的前3个字母相同。在这种情况下,您将只调用其中一个getter。为什么需要映射。这是原因吗?为什么不克隆InsuranceVehicle对象或您想要的任何其他对象?
class InsurancePolicy {
    @OneToOne
    private Person person;
    @OneToOne
    private Vehicle vehicle;
    @OneToOne
    private InsurancePolicyStatus status;

    private LocalDate policyStart = LocalDate.now().plusDays(1);

    private LocalDate policyEnd = policyStart.plusYears(1).minusDays(1);

    private boolean policy_AC = true;

    private boolean policy_OC = true;

    private boolean policy_ASS;

    private boolean policy_NNW;

    private String vehicleUsageType;
     InsuranceCalculation calculation

    @Embedded
    private InsuranceVehicle insuranceVehicle;

    @Embedded
    private InsuranceCustomer customer;

    private String coownerHowMany;

    private String abroad;

}
 public Map<String, Object> getValueMapFromInsuranceVehicle(Long insuranceId) throws InvocationTargetException, IllegalAccessException {
        InsurancePolicy insurance = repository.findById(insuranceId).get();

        Method[] methods = insurance.getClass().getMethods();  // insurance instead of vehicle
        Map<String, Object> map = new HashMap<String, Object>();
        for (Method m : methods) {
            if (m.getName().startsWith("get")) {
                Object value =  m.invoke(insurance); // insurance instead of insurancevehicle
                map.put(m.getName().substring(3), value);
            }
        }
        // add other fields specific to our needs like currentYear
        return map;
    }