Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar: Instance methods should not write to "static" fields

I am getting this prompt from Sonar: Instance methods should not write to "static" fields

I'm not quite sure what I need to change to fix this issue.

Does "SemaMonitorProxy.applicationContext" have to equal a static method?

public class SemaMonitorProxy implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public void registerFailedLoginAttempt(HttpServletRequest request, HttpServletResponse response) {
        final SemaMonitor semaMonitor = applicationContext.getBean(SemaMonitor.class);
        semaMonitor.registerFailedLoginAttempt(request, response);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SemaMonitorProxy.applicationContext = applicationContext;
    }
}
like image 909
Kevin M Avatar asked Nov 26 '25 17:11

Kevin M


1 Answers

In fact this method:

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SemaMonitorProxy.applicationContext = applicationContext;
}

is an instance method writing to a static field:

private static ApplicationContext applicationContext

You cannot make the above method static. So the only solution would be to remove the static keyword from the applicationContext declaration.

private ApplicationContext applicationContext
like image 71
gil.fernandes Avatar answered Nov 28 '25 05:11

gil.fernandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!