|
4 | 4 | import java.util.List; |
5 | 5 | import java.util.Map; |
6 | 6 | import java.util.Map.Entry; |
| 7 | +import java.util.stream.Collectors; |
7 | 8 | import java.util.Set; |
8 | 9 |
|
9 | 10 | import org.antlr.sql.sca.nodes.IParsedNode; |
|
13 | 14 | import org.sonar.plugins.sql.models.rules.RuleResultType; |
14 | 15 |
|
15 | 16 | public class ViolationsAnalyzer { |
16 | | - private static final Logger LOGGER = Loggers.get(ViolationsAnalyzer.class); |
| 17 | + private static final Logger LOGGER = Loggers.get(ViolationsAnalyzer.class); |
17 | 18 |
|
18 | | - public static class FoundViolation { |
19 | | - public final Set<IParsedNode> violatingNodes = new HashSet<IParsedNode>(); |
20 | | - public boolean failuresFound = false; |
21 | | - } |
| 19 | + public static class FoundViolation { |
| 20 | + public final Set<IParsedNode> violatingNodes = new HashSet<IParsedNode>(); |
| 21 | + public boolean failuresFound = false; |
| 22 | + } |
22 | 23 |
|
23 | | - public FoundViolation isMatch(Map<RuleImplementation, List<IParsedNode>> items) { |
| 24 | + public FoundViolation isMatch(Map<RuleImplementation, List<IParsedNode>> items) { |
24 | 25 |
|
25 | | - boolean skipSatisfied = false; |
26 | | - boolean failuresFound = false; |
27 | | - FoundViolation violation = new FoundViolation(); |
28 | | - for (Entry<RuleImplementation, List<IParsedNode>> item : items.entrySet()) { |
| 26 | + boolean skipSatisfied = false; |
| 27 | + boolean failuresFound = false; |
| 28 | + FoundViolation violation = new FoundViolation(); |
| 29 | + for (Entry<RuleImplementation, List<IParsedNode>> item : items.entrySet()) { |
29 | 30 |
|
30 | | - RuleImplementation impl = item.getKey(); |
31 | | - List<IParsedNode> values = item.getValue(); |
32 | | - if (LOGGER.isDebugEnabled()) { |
33 | | - LOGGER.debug(() -> "Checking rules: " + impl.getRuleViolationMessage() + " against nodes: " + values |
34 | | - + " with result type: " + impl.getRuleResultType()); |
35 | | - } |
36 | | - if (RuleResultType.DEFAULT == impl.getRuleResultType()) { |
37 | | - continue; |
38 | | - } |
| 31 | + RuleImplementation impl = item.getKey(); |
| 32 | + List<IParsedNode> values = item.getValue(); |
| 33 | + if (LOGGER.isDebugEnabled()) { |
| 34 | + LOGGER.debug(() -> "Checking rules: " + impl.getRuleViolationMessage() + " against nodes: " |
| 35 | + + values.stream().map(x -> x.getText() + "@" + x.getClassName()+" on "+x.getLine()).collect(Collectors.toList()) |
| 36 | + + " with result type: " + impl.getRuleResultType()); |
| 37 | + } |
| 38 | + if (RuleResultType.DEFAULT == impl.getRuleResultType()) { |
| 39 | + continue; |
| 40 | + } |
39 | 41 |
|
40 | | - if (RuleResultType.SKIP_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
41 | | - skipSatisfied = true; |
42 | | - } |
43 | | - if (RuleResultType.SKIP_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
44 | | - skipSatisfied = true; |
45 | | - } |
46 | | - if (RuleResultType.FAIL_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
47 | | - failuresFound = true; |
48 | | - violation.violatingNodes.addAll(values); |
| 42 | + if (RuleResultType.SKIP_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
| 43 | + skipSatisfied = true; |
| 44 | + } |
| 45 | + if (RuleResultType.SKIP_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
| 46 | + skipSatisfied = true; |
| 47 | + } |
| 48 | + if (RuleResultType.FAIL_IF_FOUND == impl.getRuleResultType() && !values.isEmpty()) { |
| 49 | + failuresFound = true; |
| 50 | + violation.violatingNodes.addAll(values); |
49 | 51 |
|
50 | | - } |
| 52 | + } |
51 | 53 |
|
52 | | - if (RuleResultType.FAIL_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
53 | | - failuresFound = true; |
54 | | - violation.violatingNodes.addAll(values); |
55 | | - } |
| 54 | + if (RuleResultType.FAIL_IF_NOT_FOUND == impl.getRuleResultType() && values.isEmpty()) { |
| 55 | + failuresFound = true; |
| 56 | + violation.violatingNodes.addAll(values); |
| 57 | + } |
56 | 58 |
|
57 | | - if (RuleResultType.FAIL_IF_MORE_FOUND == impl.getRuleResultType() && values.size() > impl.getTimes()) { |
58 | | - failuresFound = true; |
59 | | - violation.violatingNodes.addAll(values); |
60 | | - } |
| 59 | + if (RuleResultType.FAIL_IF_MORE_FOUND == impl.getRuleResultType() && values.size() > impl.getTimes()) { |
| 60 | + failuresFound = true; |
| 61 | + violation.violatingNodes.addAll(values); |
| 62 | + } |
61 | 63 |
|
62 | | - if (RuleResultType.FAIL_IF_LESS_FOUND == impl.getRuleResultType() && values.size() < impl.getTimes()) { |
63 | | - failuresFound = true; |
64 | | - violation.violatingNodes.addAll(values); |
65 | | - } |
| 64 | + if (RuleResultType.FAIL_IF_LESS_FOUND == impl.getRuleResultType() && values.size() < impl.getTimes()) { |
| 65 | + failuresFound = true; |
| 66 | + violation.violatingNodes.addAll(values); |
| 67 | + } |
66 | 68 |
|
67 | | - } |
68 | | - if (skipSatisfied) { |
69 | | - violation.failuresFound = false; |
70 | | - return violation; |
71 | | - } |
72 | | - violation.failuresFound = failuresFound; |
73 | | - return violation; |
74 | | - } |
| 69 | + } |
| 70 | + if (skipSatisfied) { |
| 71 | + violation.failuresFound = false; |
| 72 | + return violation; |
| 73 | + } |
| 74 | + violation.failuresFound = failuresFound; |
| 75 | + return violation; |
| 76 | + } |
75 | 77 | } |
0 commit comments