Warning message: Methods returns should not be invariant (java:S3516)
The warning is attached to method preHandle()
OS: Windows 10 Pro; Version 21H2; OS build 19044.1348; Windows Feature Experience Pack 120.2212.3920.0
Java: Java version “17” 2021-09-14 LTS; Java™ SE Runtime Environment (build 17+35-LTS-2724)
Project Java compliance: Java 11
IDE: Spring Tool Suite 4; Version: 4.12.1.RELEASE; Build Id: 202110260750
SonarLint: SonarLint (for Eclipse) v7.1.0.39158
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
public static final String CUSTOM_FORCED_LOCALE = "CUSTOM_FORCED_LOCALE";
public class CustomLocaleChangeInterceptor extends LocaleChangeInterceptor {
@Override
protected Locale parseLocaleValue(final String localeValue) {
return super.parseLocaleValue(localeValue);
}
/**
* I get warning message here on method name
*/
@Override
public boolean preHandle(
final HttpServletRequest request,
final HttpServletResponse response,
final Object handler
) throws ServletException {
final String newLocale = request.getParameter(getParamName());
if (newLocale == null) {
// proceed
return true;
}
if (!customCheckHttpMethod(request.getMethod())) {
// proceed
return true;
}
final LocaleResolver localeResolver =
RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException(
"No Resolver found: not in a DispatcherServlet request?"
);
}
try {
final Locale parsedLocale = parseLocaleValue(newLocale);
request.setAttribute(CUSTOM_FORCED_LOCALE, newLocale);
localeResolver.setLocale(request, response, parsedLocale);
}
catch (final IllegalArgumentException e) {
if (!isIgnoreInvalidLocale()) {
throw e;
}
}
// proceed
return true;
}
private boolean customCheckHttpMethod(final String method) {
final String[] configuredMethods = getHttpMethods();
if (ObjectUtils.isEmpty(configuredMethods)) {
return true;
}
for (final String configuredMethod : configuredMethods) {
if (configuredMethod.equalsIgnoreCase(method)) {
return true;
}
}
return false;
}
}
}