Apache Commons->HashCodeBuilder.reflectionHashCode如何排除子类的部分字段

问题描述 投票:0回答:0

我试图在哈希码生成期间为子类排除一些字段,似乎我可以选择完全排除它或不排除,但即使在排除列表中指定它之后也不能选择特定字段

例如如果我有一个

    Class Car {
        @HashCodeExclude
        int id

        int modelYear

        String color

        TyreClass tyre;
    }

    Class TyreClass {
   
       @HashCodeExclude
       int id;

       @HashCodeExclude
       int version;

       float radius

       String colour

       double width
    }

现在,当我尝试调用 HashCodeBuilder.reflectionHashCode 方法时,例如:

    TyreClass tyreObject = new TyreClass();
             type object.setId(1);

    Car carObject_1 = new Car();
          carObject_1.setId(1);
          carObject_1.setTyre(tyreObject);

    Car carObject_2 = new Car();
          carObject_2.setVersion(2);   /// Changing this value which is to be excluded for hashcode generation
         carObject_2.setTyre(tyreObject);

     List<String> excludedFields = new ArrayList<>();
           excludedFields.add("id");
           excludedFields.add("version");
     int hashCode_1 = HashCodeBuilder.reflectionHashCode(carObject_1, excludedFields)
     int hashCode_2 = HashCodeBuilder.reflectionHashCode(carObject_2, excludedFields)

I was hoping, since I have excluded the field "version" by adding it to the exclusion list, I was hoping that it would be excluded but it's not.
Can somebody guide me on what I can do to exclude the "version" field of the Tyre object?
hashcode apache-commons exclusion-constraint
© www.soinside.com 2019 - 2024. All rights reserved.