Reputation: 31
When I used SonarQube to examine my Java code, some warning popped up: Fields in a “Serializable” class should either be transient or serializable squid : S1948.
My code:
public class ServiceResult implements Serializable {
public static final Integer INTERNAL_ERROR = 500;
public static final Integer SERVICE_EXCEPTION = 501;
public static final Integer PARAM_ERROR = 999;
public static final int AUTHORITY_ERROR = 401;
public static final Integer SUCCESS = 200;
public static final Integer INPUT_ERROR = 402;
private Integer code = SUCCESS;
private Object msg;
public ServiceResult() {
}
public ServiceResult(Integer code, Object msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getMsg() {
return msg;
}
public void setMsg(Object msg) {
this.msg = msg;
}
public static ServiceResult fail(String msg) {
ServiceResult serviceResult = new ServiceResult();
serviceResult.setCode(INTERNAL_ERROR);
serviceResult.setMsg(msg);
return serviceResult;
}
public static ServiceResult success(Object msg) {
ServiceResult serviceResult = new ServiceResult();
serviceResult.setCode(SUCCESS);
serviceResult.setMsg(msg);
return serviceResult;
}
}
I am just not sure what else I can do here, Class is serializable and do have all member fields which are serializable as well.
Please do let me know if this has been tackled before.
Upvotes: 1
Views: 7351
Reputation: 8624
This SonarQube rule requires that all fields of a serializable class must also be serializable (or marked as transient
, so that they are ignored during a serialization).
The Object
class does not implement Serializable
, so the following line violates that rule:
private Object msg;
To resolve this issue, either exclude msg
from serialization by making it transient
or change the type from Object
to String
or any other class that implements Serializable
.
Upvotes: 1