问题
I have the following piece of code in my program and I am running SonarQube 5 for code quality check on it after integrating it with Maven. I am facing this error
Make this "public static processStatus" field final.
Make this "public static processStatusId" field final
But I don't want to make this as final. Is there any other solution?
public abstract class ProcessStatusListPO_ {
private ProcessStatusListPO_() {
}
public static volatile SingularAttribute<ProcessStatusListPO, String> processStatus ;
public static volatile SingularAttribute<ProcessStatusListPO, Long> processStatusId ;
}
回答1:
Sonar is complaining that you shouldn't use mutable state, but you don't want to follow that advice. Your options thus are: ignore the warning or follow the advice even if you don't want to. Following the advice doesn't mean slapping 'final' in there (as it doesn't make sense in this context), but redesigning your code to be better.
You can ignore using suppresswarnings already explained. If you want to follow the advice, you need to redesign your piece of code in a different way.
We don't know your code beyond what you've posted, so we cannot say how exactly you should redesign it. However, doing that is recommended. As to why, you can read up on it here: Why is Global State so Evil?
回答2:
if you assume that variable unique in all context make that
public static final volatile SingularAttribute processStatus ;
- That way you need instance of the var.
- Or put annotations
@SuppressWarnings(value = { "CWE-580" })
- that way i'm use.
- if anybody has other solutions more elegant please send us.
来源:https://stackoverflow.com/questions/40044282/sonar-asking-to-make-this-field-final