SonarLint V3:“Serializable"中的字段;对于 List 接口,类应该是瞬态的或可序列化

2024-05-09Java开发问题
136

本文介绍了SonarLint V3:“Serializable"中的字段;对于 List 接口,类应该是瞬态的或可序列化的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的问题与 this 非常相似 除了我在 SonarLint V3 (squid:S1948) 中遇到的这个问题.

My question is very similar to this except that this issue I have encountered in SonarLint V3 (squid:S1948).

我的代码是:

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

}

摘要对象实现可序列化.

The Summary Object implements 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;

    }

}

现在,如果我将 List 替换为 ArrayList ,那么 SonarLint 中会出现另一个警告,即我们应该使用接口而不是实现类.

Now, If I replace List by ArrayList , then another warning in SonarLint arises that we should be using interface instead of implementation classes.

我认为这可能会在 SonarQube 中解决,但对于 SonarLint 我不知道.这是一个错误还是我做错了什么?

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

推荐答案

SonarLint 是对的.问题是不能保证 elements 字段是可序列化的.您需要像这样在 T 类型上添加类型绑定

SonarLint is right. The problem is that there is no guarantee that elements field is serializable. You need to add type bound on T type like this

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

这样,如果为列表选择的实现是可序列化的(Java 中的标准集合类型也是如此),则列表将是可序列化的.

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

这篇关于SonarLint V3:“Serializable"中的字段;对于 List 接口,类应该是瞬态的或可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13