SonarQube Reports Major Bug when checking null for variables whose value comes from any method that is annotated with org.springframework.lang.Nullable
- versions : SonarQube-v7.2,
error observed
private static final String CACHE_JAXB_CTX = "jaxbContextCache";
@Autowired
private CacheManager cacheMgr;
@PostConstruct
public void init() {
try {
this.cacheJaxb = null;
this.cacheJaxb = this.cacheMgr.getCache(CACHE_JAXB_CTX);
if ( this.cacheJaxb == null) {
throw new RuntimeException("jaxb cache not initialized, please check your configuration file");
}
} catch (RuntimeException e) {
logger.error("error on postConstruct", e);
throw e;
}
}
When the above code is analyzed by SonarQube, a major bug is reported in the if() block where the object is checked for nullability.
Whereas the org.springframework.cache.CacheManager.getCache(String name) clearly states that the value returned can be null.
/**
* Return the cache associated with the given name.
* @param name the cache identifier (must not be {@code null})
* @return the associated cache, or {@code null} if none found
*/
@Nullable
Cache getCache(String name);