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

6 java serialization sonarqube

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

我有以下共享接口

public interface MPUpdate {

    void apply( SoapService svc, byte[] jerseyClientResp ) throws MPException ;
}
Run Code Online (Sandbox Code Playgroud)

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

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 );      
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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
    }

}
Run Code Online (Sandbox Code Playgroud)

最后这是它的使用方式:

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

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

非常感谢

jta*_*orn 5

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