From: Stephane Gamard Date: Tue, 1 Jul 2014 14:06:22 +0000 (+0200) Subject: fix quality flaw (rule.getLanguage() can be null) X-Git-Tag: 4.4-RC1~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f79488e2b6476d304c362c7e5b1155e5d76f35f;p=sonarqube.git fix quality flaw (rule.getLanguage() can be null) --- diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java index fd3909bcc58..5b065f8d699 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java @@ -30,8 +30,11 @@ import org.sonar.core.persistence.Dto; import javax.annotation.CheckForNull; import javax.annotation.Nullable; - -import java.util.*; +import java.util.Arrays; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import java.util.TreeSet; public final class RuleDto extends Dto { @@ -165,6 +168,7 @@ public final class RuleDto extends Dto { return this; } + @CheckForNull public String getLanguage() { return language; } diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index f42a71c7e4d..4c72cc20ea6 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -455,7 +455,7 @@ 127.0.0.1 9200 true - FULL + BASIC /dev diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java index 32c4eab70a4..beaad6a900c 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java @@ -56,14 +56,14 @@ public class RuleNormalizer extends BaseNormalizer { public static final IndexField DEFAULT_VALUE = add(IndexField.Type.STRING, "defaultValue"); public static final Set ALL_FIELDS = getAllFields(); - private static Set getAllFields() { + private static final Set getAllFields() { Set fields = new HashSet(); for (Field classField : RuleParamField.class.getDeclaredFields()) { if (classField.getType().isAssignableFrom(IndexField.class)) { try { fields.add(IndexField.class.cast(classField.get(null))); } catch (IllegalAccessException e) { - throw new IllegalStateException("Can not introspect rule index fields", e); + throw new IllegalStateException("Could not access Field '" + classField.getName() + "'"); } } } @@ -116,29 +116,29 @@ public class RuleNormalizer extends BaseNormalizer { public static final IndexField PARAMS = addEmbedded("params", RuleParamField.ALL_FIELDS); - public static Set ALL_FIELDS = getAllFields(); + public static final Set ALL_FIELDS = getAllFields(); - private static Set getAllFields() { + private static final Set getAllFields() { Set fields = new HashSet(); for (Field classField : RuleField.class.getDeclaredFields()) { if (classField.getType().isAssignableFrom(IndexField.class)) { try { fields.add(IndexField.class.cast(classField.get(null))); } catch (IllegalAccessException e) { - e.printStackTrace(); + throw new IllegalStateException("Could not access Field '" + classField.getName() + "'"); } } } return fields; } - public static IndexField of(String fieldName) { + public static final IndexField of(String fieldName) { for (IndexField field : ALL_FIELDS) { if (field.field().equals(fieldName)) { return field; } } - return null; + throw new IllegalStateException("Could not find an IndexField for '" + fieldName + "'"); } } @@ -151,16 +151,17 @@ public class RuleNormalizer extends BaseNormalizer { @Override public List normalize(RuleKey key) { DbSession dbSession = db.openSession(false); + List requests = new ArrayList(); try { - List requests = new ArrayList(); - requests.addAll(normalize(db.ruleDao().getNullableByKey(dbSession, key))); + RuleDto rule = db.ruleDao().getByKey(dbSession, key); + requests.addAll(normalize(rule)); for (RuleParamDto param : db.ruleDao().findRuleParamsByRuleKey(dbSession, key)) { requests.addAll(normalizeNested(param, key)); } - return requests; } finally { dbSession.close(); } + return requests; } @Override @@ -213,8 +214,7 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), null); update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), null); if (rule.getDefaultSubCharacteristicId() != null) { - CharacteristicDto characteristic = null; - CharacteristicDto subCharacteristic = null; + CharacteristicDto characteristic, subCharacteristic = null; subCharacteristic = db.debtCharacteristicDao().selectById(rule.getDefaultSubCharacteristicId(), session); if (subCharacteristic != null) { characteristic = db.debtCharacteristicDao().selectById(subCharacteristic.getParentId()); @@ -228,8 +228,7 @@ public class RuleNormalizer extends BaseNormalizer { update.put(RuleField.CHARACTERISTIC.field(), DebtCharacteristic.NONE); update.put(RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE); } else { - CharacteristicDto characteristic = null; - CharacteristicDto subCharacteristic = null; + CharacteristicDto characteristic, subCharacteristic = null; subCharacteristic = db.debtCharacteristicDao().selectById(rule.getSubCharacteristicId(), session); characteristic = db.debtCharacteristicDao().selectById(subCharacteristic.getParentId()); update.put(RuleField.CHARACTERISTIC.field(), characteristic.getKey());