团队引入了Sonar Qube做代码质量管理,反馈了一个问题:Make the enclosing method "static" or remove this set.
发生问题的地方在:

查了一下,是rule S2696

意思是:从非静态方法正确更新静态字段很难做到正确,如果有多个类实例和/或多个线程在运行,很容易导致错误。 理想情况下,静态字段仅从同步静态方法中更新。
解决方法:
1.adding @SuppressWarnings("squid:S2696") to the top of the offending method causes Sonar to ignore that warning completely.
2.
@Setter
private static volatile ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
setContext(ac);
}
未经验证~先记录一下
团队使用SonarQube进行代码质量管理时发现了一个S2696警告,该警告提示从非静态方法更新静态字段可能导致错误。问题涉及一个非静态方法设置静态ApplicationContext。解决方案包括添加注解忽略警告或调整字段为静态和同步。博客记录了这个问题及其潜在的解决策略。
2786

被折叠的 条评论
为什么被折叠?



