Java sonarQube - 使字段瞬态或可序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36017994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:22:40  来源:igfitidea点击:

sonarQube - make field transient or serializable

javaserializationsonarqube

提问by

I'm trying to solve the following violation reported by sonarQube plugin for Jenkins: "make 'update' transient or serializable.". Gravity: critical, tag: serialization.

我正在尝试解决 Jenkins 的 sonarQube 插件报告的以下违规问题:“使‘更新’瞬态或可序列化。”。重力:关键,标签:序列化。

I have the following shared interface

我有以下共享接口

public interface MPUpdate {

    void apply( SoapService svc, byte[] jerseyClientResp ) throws MPException ;
}

The following enum is the entry point for the application logic

以下枚举是应用程序逻辑的入口点

public enum DomainResource implements MPUpdate {

    PROGRAMMES( new ProgrammeUpdate() ),
    PRODUCTIONS( new ProductionUpdate() );
    // more enums

    private DomainResource( MPUpdate update ) {
        this.update = update;
    }

    private final MPUpdate update; // Sonar: make "update" transient or serializable, priority: critical, tag: serialization

    @Override
    public void apply( SoapService svc, byte[] jerseyClientResp ) throws MPException {
        update.apply( svc, jerseyClientResp );      
    }
}

One of the unit of logic initialized through the enum

通过枚举初始化的逻辑单元之一

public class ProgrammeUpdate implements MPUpdate {

    private final ResponseConverter<ProgrammeDto> responseConverter = new ResponseConverter<>( ProgrammeDto.class );

    @Override
    public void apply( SoapService svc, byte[] jerseyClientResp ) throws MPException {

        // APPLICATION LOGIC
    }

}

And finally this is how it's used:

最后这是它的使用方式:

...
String levelFromUrl = getLevel(); // eg. "programmes"
MPUpdate resource;
resource = DomainResource.valueOf( levelFromUrl.toUpperCase() ); 
...
resource.apply( soapService, jerseyClientOutcome );
...

Any help? Does the use of enum improve performance for logging?

有什么帮助吗?使用 enum 是否可以提高日志记录的性能?

Many thanks

非常感谢

采纳答案by jtahlborn

You don't need it to be serializable. You should mark it as transient. Enums are serialized using the simple name string, so additional fields are irrelevant. just mark the field as transient to make sonar happy (although the tool itself should really be able to identify this situation).

你不需要它是可序列化的。您应该将其标记为瞬态。枚举使用简单的名称字符串进行序列化,因此其他字段是无关紧要的。只需将该字段标记为瞬态以使声纳满意(尽管工具本身应该确实能够识别这种情况)。