SonarQube
Version 7.0 (build 36138)
Java code sample:
ConfigurationConstant toGlobalConfigConstant(Enum key) {
try {
return ConfigurationConstant.valueOf(key.name());
} catch (Exception ex) {
String keyName = key != null ? key.name() : "null";
// ^ Change this condition so that it does not always evaluate to "true"
logger.warn("toGlobalConfigConstant(" + keyName + ") - Invalid constant name: ", ex);
return null;
}
}
If the key parameter is null, a NPE is thrown and it will still be null inside the catch block. We need to protect the access to key. I’ve checked it with a unit test.