SonarLint V3: fields in "Serializable" class must be either temporary or serializable for List interface

My question is very similar to this except that SonarLint V3 (squid: S1948) had this problem.

My code:

public class Page<T> implements Serializable {

    Summary summary;
    List<T> elements;

    public Page() {
        summary = new Summary();
    }

    public List<T> getItemsReceived() {
        return elements;
    }

    public void setItemsReceived(List<T> list) {
        this.elements = list;
    }

    public Summary getSummary() {
        return summary;
    }

    public void setSummary(Summary summary) {
        this.summary = summary;
    }

}

      

A summary object implements a serializable.

public class Summary implements Serializable {

    int offset;
    int limit;
    long totalElements;

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public long getTotalNumberOfElements() {
        return totalElements;
    }

    public void setTotalNumberOfElements(long totalNumberOfElements) {
        this.totalElements = totalNumberOfElements;

    }

}

      

Now if I replace List by ArrayList then another warning comes up in SonarLint that we should use interface instead of implementation classes.

I think it might be resolved in SonarQube, but for SonarLint I don't know. Is this a bug or am I doing something wrong?

+3


source to share


1 answer


SonarLint is right. The problem is that there is no guarantee that the field is elements

serializable. You need to add the binding type to the type T

like this

public class Page<T extends Serializable> implements Serializable {}

      



Thus, the list will be serialized if the implementation chosen for it is serializable (which is true for standard collection types in Java).

+2


source







All Articles