From 956068aea4fec3b9fcfed8fc8f34cc581dfd95f7 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 5 Jul 2012 23:35:22 +0200 Subject: [PATCH] Fix violations --- .../org/sonar/batch/index/DefaultIndex.java | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 783bffb6ad9..807f3e17504 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -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 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) { -- 2.39.5