0

I have a series of Data Transport Objects, DTO. They have a few elements that are common to each different DTO. One element is a collection of the DTO, because we can have a list, or series of these DTO's to process at a time. I've used generics so Spring can handle the different DTOs and pass up to the UI. Here are some excerpts from my GenericListDTO() class.

 @XmlElement
    @NotNull
    private Collection<DTO> dataList;
    @XmlElement
    private int totObjs;

My DTO builder is defined like this:

public static <DTO> GenericListDTOBuilder<DTO> builder() { 
   return new GenericListDTOBuilder<>(); 
}

Here is the builder class defined inside my GenericList Class.

public static class GenericListDTOBuilder<DTO> {
        private Collection<DTO> dataList;

        public GenericListDTOBuilder<DTO> data(@NotNull Collection<DTO> data) {
            this.dataList = dataList;
            return this;
        }

        private int total;
        
        public GenericListDTOBuilder<DTO> total(int total) {
            this.total = total;
            return this;
        }

        public GenericListDTO<DTO> build() {
            return new GenericListDTO<>(this.data, this.total, this.validUntil);
        }

        
        }

    }

Later in the class I have this constructor.

@NotNull
    public static <DTO> GenericListDTO<DTO> just(@NotNull Collection<DTO> dataList) {
        
        return  builder().dataList(dataList).total(dataList.size()).build();
    }

Intellij at the .dataList(dataList) portion gives this error, "Required type: Collection Provided:Collection.

I don't understand why it's giving that error when dataList has been defined as private Collection dataList; and I'm passing in a Collection object.

To get this to not have an error I have to double cast it.

return (GenericListDTA) builder().dataList((Collection) dataList).total(dataList.size()).build();

Why do wont it recognize dataList as a Collection of DTO? Why is it defaulting to Collection

  • Can you provide a reproducible example. Your code is missing the GenericListDTOBuilder.dataList method for example. – jon hanson Apr 14 '23 at 22:05

2 Answers2

0

Issue seems to be on the GenericListDTOBuilder.data() method. You are assigning parameter value instead of data.

public GenericListDTOBuilder<DTO> data(@NotNull Collection<DTO> data) {
   this.dataList = data;
   return this;
}
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • I'm not convinced this is the problem. The questioner is asking about a compiler error, whereas what you have pointed out would be a runtime problem. I suspect however that this is an artefact of code that hasn't been anonymised correctly: the code is inconsistent in its use of `data` and `dataList`, and `validUntil` is used but never defined in the class `GenericListDTOBuilder`. – Luke Woodward Apr 14 '23 at 21:44
0

This is probably a problem with inferring the return type of builder() in your just() method.

There are two ways I can see to fix this. Firstly, extract the return value of builder() to a local variable, and then call methods on that:

    GenericListDTOBuilder<DTO> builder = builder();
    return builder.dataList(dataList).total(dataList.size()).build();

Secondly, provide the type parameter when calling the builder() method. To get the type parameter in, you also need to qualify the static method with the class name:

    return GenericListDTOBuilder.<DTO>builder().dataList(dataList).total(dataList.size()).build();

Note that the type variable DTO in your static just method is not the same as the type variable DTO in the class. You would be best off renaming this type variable to avoid any confusion.

See also this answer.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104