Make SerialVersionUID Static Final

Properties

Property Value
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 5 min
Links S2057

Description

Adds the modifiers static and final to SerialVersionUid long variables when they are absent.

Benefits

For serializable classes the SerialVersionUid should be written explicitly. If the UID changes, that can lead to errors (for example, an object not being able to be deserialized). To avoid those errors, apply this rule.

Requirement & Tags

Requirements

None

Code Changes

Pre

import java.io.Serializable;

public class TestSerialVersionUidRule {
    private class Test01 implements Serializable {
        /**
         *
         */
        private long serialVersionUID = 1L;
        private long otherField;
        private Object objectField;
    }
    private class Test02 implements Serializable {
        /**
         *
         */
        private long testLong, serialVersionUID = 1L;
    }
}

Post

import java.io.Serializable;

public class TestSerialVersionUidRule {
    private class Test01 implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private long otherField;
        private Object objectField;
    }
    private class Test02 implements Serializable {
        private static final long serialVersionUID = 1L;
        /**
         *
         */
        private long testLong;
    }
}

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow version: