6

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

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?

Many thanks

  • Read this: http://stackoverflow.com/questions/5177013/how-does-marking-a-field-as-transient-make-it-possible-to-serialise-an-object – mascoj Mar 15 '16 at 17:24
  • Possible duplicate of [Does enum's field have to be Serializable?](http://stackoverflow.com/questions/31432528/does-enums-field-have-to-be-serializable) – fps Mar 15 '16 at 18:41

1 Answers1

5

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).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • 1
    Thanks. I've decided to simply do as you suggested. In alternative I also had to add "extends Serializable" to the interface and add a field "serialVersionUID" on every class that implemented that interface. –  Mar 15 '16 at 18:07
  • 1
    I have implemented serializable interface in the class, even then I am getting this error. – Nirmal Mangal Jul 25 '17 at 17:42