]> source.dussan.org Git - sonarqube.git/commitdiff
Fix violations
authorDavid Gageot <david@gageot.net>
Thu, 5 Jul 2012 21:35:22 +0000 (23:35 +0200)
committerDavid Gageot <david@gageot.net>
Fri, 6 Jul 2012 09:06:39 +0000 (11:06 +0200)
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java

index 783bffb6ad95bbcd70e24f44fc28c7efa5332dcd..807f3e175049c3573e61aa44c3fda75a94c5b3f7 100644 (file)
@@ -299,22 +299,28 @@ public class DefaultIndex extends SonarIndex {
     if (resource == null) {
       throw new IllegalArgumentException("A resource must be set on the ViolationQuery in order to search for violations.");
     }
+
     Bucket bucket = buckets.get(resource);
     if (bucket == null) {
       return Collections.emptyList();
     }
+
     List<Violation> filteredViolations = Lists.newArrayList();
     ViolationQuery.SwitchMode mode = violationQuery.getSwitchMode();
     for (Violation violation : bucket.getViolations()) {
-      if (mode == ViolationQuery.SwitchMode.BOTH ||
-          (mode == ViolationQuery.SwitchMode.OFF && violation.isSwitchedOff()) ||
-          (mode == ViolationQuery.SwitchMode.ON && !violation.isSwitchedOff())) {
+      if (isFiltered(violation, mode)) {
         filteredViolations.add(violation);
       }
     }
     return filteredViolations;
   }
 
+  private static boolean isFiltered(Violation violation, ViolationQuery.SwitchMode mode) {
+    return (mode == ViolationQuery.SwitchMode.BOTH
+      || (mode == ViolationQuery.SwitchMode.OFF && violation.isSwitchedOff())
+      || (mode == ViolationQuery.SwitchMode.ON && !violation.isSwitchedOff()));
+  }
+
   @Override
   public void addViolation(Violation violation, boolean force) {
     Resource resource = violation.getResource();
@@ -330,32 +336,31 @@ public class DefaultIndex extends SonarIndex {
     }
 
     Bucket bucket = checkIndexed(resource);
-    if (bucket != null && !bucket.isExcluded()) {
-      boolean isIgnored = !force && violationFilters != null && violationFilters.isIgnored(violation);
-      if (!isIgnored) {
-
-        // TODO this code is not the responsibility of this index. It should be moved somewhere else.
-
-        if (violation.isManual()) {
-          doAddViolation(violation, bucket);
-        } else {
-          ActiveRule activeRule = profile.getActiveRule(violation.getRule());
-          if (activeRule == null) {
-            if (currentProject.getReuseExistingRulesConfig()) {
-              violation.setSeverity(violation.getRule().getSeverity());
-              doAddViolation(violation, bucket);
-
-            } else {
-              LoggerFactory.getLogger(getClass()).debug("Rule is not activated, ignoring violation {}", violation);
-            }
-
-          } else {
-            violation.setSeverity(activeRule.getSeverity());
-            doAddViolation(violation, bucket);
-          }
-        }
+    if (bucket == null || bucket.isExcluded()) {
+      return;
+    }
+
+    boolean isIgnored = !force && violationFilters != null && violationFilters.isIgnored(violation);
+    if (!isIgnored) {
+      addViolation(violation, bucket);
+    }
+  }
+
+  private void addViolation(Violation violation, Bucket bucket) {
+    // TODO this code is not the responsibility of this index. It should be moved somewhere else.
+    if (!violation.isManual()) {
+      ActiveRule activeRule = profile.getActiveRule(violation.getRule());
+      if (activeRule != null) {
+        violation.setSeverity(activeRule.getSeverity());
+      } else if (currentProject.getReuseExistingRulesConfig()) {
+        violation.setSeverity(violation.getRule().getSeverity());
+      } else {
+        LoggerFactory.getLogger(getClass()).debug("Rule is not activated, ignoring violation {}", violation);
+        return;
       }
     }
+
+    doAddViolation(violation, bucket);
   }
 
   private void doAddViolation(Violation violation, Bucket bucket) {