]> source.dussan.org Git - sonarqube.git/blob
f710ad9105167f9df44196099078ae25beaca477
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
22 import com.google.common.collect.ImmutableList;
23 import org.sonar.server.computation.task.projectanalysis.component.PathAwareCrawler;
24 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
25 import org.sonar.server.computation.task.projectanalysis.formula.AverageFormula;
26 import org.sonar.server.computation.task.projectanalysis.formula.DistributionFormula;
27 import org.sonar.server.computation.task.projectanalysis.formula.Formula;
28 import org.sonar.server.computation.task.projectanalysis.formula.FormulaExecutorComponentVisitor;
29 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
30 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
31 import org.sonar.server.computation.task.step.ComputationStep;
32
33 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
34 import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION_KEY;
35 import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY;
36 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY;
37 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
38 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
39 import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
40 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
41 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_KEY;
42 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
43 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
44 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
45 import static org.sonar.server.computation.task.projectanalysis.formula.SumFormula.createIntSumFormula;
46
47 /**
48  * Computes complexity measures on files and then aggregates them on higher components.
49  */
50 public class ComplexityMeasuresStep implements ComputationStep {
51
52   private static final ImmutableList<Formula> FORMULAS = ImmutableList.<Formula>of(
53     createIntSumFormula(COMPLEXITY_KEY),
54     createIntSumFormula(COMPLEXITY_IN_CLASSES_KEY),
55     createIntSumFormula(COMPLEXITY_IN_FUNCTIONS_KEY),
56     new DistributionFormula(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY),
57     new DistributionFormula(FILE_COMPLEXITY_DISTRIBUTION_KEY),
58     new DistributionFormula(CLASS_COMPLEXITY_DISTRIBUTION_KEY),
59     AverageFormula.Builder.newBuilder().setOutputMetricKey(FILE_COMPLEXITY_KEY)
60       .setMainMetricKey(COMPLEXITY_KEY)
61       .setByMetricKey(FILES_KEY)
62       .build(),
63     AverageFormula.Builder.newBuilder().setOutputMetricKey(CLASS_COMPLEXITY_KEY)
64       .setMainMetricKey(COMPLEXITY_IN_CLASSES_KEY)
65       .setByMetricKey(CLASSES_KEY)
66       .build(),
67     AverageFormula.Builder.newBuilder().setOutputMetricKey(FUNCTION_COMPLEXITY_KEY)
68       .setMainMetricKey(COMPLEXITY_IN_FUNCTIONS_KEY)
69       .setByMetricKey(FUNCTIONS_KEY)
70       .build());
71
72   private final TreeRootHolder treeRootHolder;
73   private final MetricRepository metricRepository;
74   private final MeasureRepository measureRepository;
75
76   public ComplexityMeasuresStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
77     this.treeRootHolder = treeRootHolder;
78     this.metricRepository = metricRepository;
79     this.measureRepository = measureRepository;
80   }
81
82   @Override
83   public void execute() {
84     new PathAwareCrawler<>(
85       FormulaExecutorComponentVisitor.newBuilder(metricRepository, measureRepository).buildFor(FORMULAS))
86       .visit(treeRootHolder.getRoot());
87   }
88
89   @Override
90   public String getDescription() {
91     return "Compute complexity measures";
92   }
93 }