001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.measures;
021
022 import org.sonar.api.utils.SonarException;
023
024 import java.lang.reflect.Field;
025 import java.util.*;
026
027 /**
028 * @since 1.10
029 */
030 public final class CoreMetrics {
031
032 private CoreMetrics() {
033 }
034
035 public static final String DOMAIN_SIZE = "Size";
036 public static final String DOMAIN_TESTS = "Tests";
037 public static final String DOMAIN_COMPLEXITY = "Complexity";
038 public static final String DOMAIN_DOCUMENTATION = "Documentation";
039 public static final String DOMAIN_RULES = "Rules";
040 public static final String DOMAIN_RULE_CATEGORIES = "Rule categories";
041 public static final String DOMAIN_GENERAL = "General";
042
043 /* core statistics */
044 public static final Metric LINES = new Metric("lines", "Lines", "Lines", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
045 public static final Metric NCLOC = new Metric("ncloc", "Lines of code", "Non Commenting Lines of Code", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
046 public static final Metric CLASSES = new Metric("classes", "Classes", "Classes", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
047 public static final Metric FILES = new Metric("files", "Files", "Number of files", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE);
048 public static final Metric DIRECTORIES = new Metric("directories", "Directories", "Directories", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE);
049 public static final Metric PACKAGES = new Metric("packages", "Packages", "Packages", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
050 public static final Metric FUNCTIONS = new Metric("functions", "Methods", "Methods", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
051 public static final Metric ACCESSORS = new Metric("accessors", "Accessors", "Accessors", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
052 public static final Metric STATEMENTS = new Metric("statements", "Statements", "Number of statements", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
053 public static final Metric BRANCHES = new Metric("branches", "Branches", "Branches", Metric.ValueType.INT, Metric.DIRECTION_BETTER, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
054 public static final Metric PUBLIC_API = new Metric("public_api", "Public API", "Public API", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE).setFormula(new SumChildValuesFormula(false));
055
056 /* complexity */
057 public static final Metric COMPLEXITY = new Metric("complexity", "Complexity", "Cyclomatic complexity", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_COMPLEXITY).setFormula(new SumChildValuesFormula(false));
058 public static final Metric CLASS_COMPLEXITY = new Metric("class_complexity", "Complexity /class", "Complexity average by class", Metric.ValueType.FLOAT, Metric.DIRECTION_WORST, true, DOMAIN_COMPLEXITY);
059 public static final Metric FUNCTION_COMPLEXITY = new Metric("function_complexity", "Complexity /method", "Complexity average by method", Metric.ValueType.FLOAT, Metric.DIRECTION_WORST, true, DOMAIN_COMPLEXITY);
060 public static final Metric FILE_COMPLEXITY = new Metric("file_complexity", "Complexity /file", "Complexity average by file", Metric.ValueType.FLOAT, Metric.DIRECTION_WORST, true, DOMAIN_COMPLEXITY);
061 public static final Metric CLASS_COMPLEXITY_DISTRIBUTION = new Metric("class_complexity_distribution", "Classes distribution /complexity", "Classes distribution /complexity", Metric.ValueType.DISTRIB, Metric.DIRECTION_NONE, true, DOMAIN_COMPLEXITY);
062 public static final Metric FUNCTION_COMPLEXITY_DISTRIBUTION = new Metric("function_complexity_distribution", "Functions distribution /complexity", "Functions distribution /complexity", Metric.ValueType.DISTRIB, Metric.DIRECTION_NONE, true, DOMAIN_COMPLEXITY);
063 public static final Metric UNCOVERED_COMPLEXITY_BY_TESTS = new Metric("uncovered_complexity_by_tests", "Uncovered complexity", "Uncovered complexity", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_COMPLEXITY);
064
065 /* comments */
066 public static final Metric COMMENT_LINES = new Metric("comment_lines", "Comment lines", "Number of comment lines", Metric.ValueType.INT, Metric.DIRECTION_BETTER, false, DOMAIN_DOCUMENTATION).setFormula(new SumChildValuesFormula(false));
067 public static final Metric COMMENT_LINES_DENSITY = new Metric("comment_lines_density", "Comments (%)", "Comments balanced by ncloc + comment lines", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_DOCUMENTATION);
068 public static final Metric PUBLIC_DOCUMENTED_API_DENSITY = new Metric("public_documented_api_density", "Public documented API (%)", "Public documented classes and methods balanced by ncloc", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_DOCUMENTATION);
069 public static final Metric PUBLIC_UNDOCUMENTED_API = new Metric("public_undocumented_api", "Public undocumented API", "Public undocumented classes, methods and variables", Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_DOCUMENTATION).setFormula(new SumChildValuesFormula(false));
070 public static final Metric COMMENTED_OUT_CODE_LINES = new Metric("commented_out_code_lines", "Commented out lines of code", "Commented out lines of code", Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_DOCUMENTATION).setFormula(new SumChildValuesFormula(false));
071
072 /* unit tests */
073 public static final Metric TESTS = new Metric("tests", "Unit tests", "Number of unit tests", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
074 public static final Metric TEST_EXECUTION_TIME = new Metric("test_execution_time", "Unit tests duration", "Execution duration of unit tests ", Metric.ValueType.MILLISEC, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
075 public static final Metric TEST_ERRORS = new Metric("test_errors", "Unit test errors", "Number of unit test errors", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
076 public static final Metric SKIPPED_TESTS = new Metric("skipped_tests", "Skipped unit tests", "Number of skipped unit tests", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
077 public static final Metric TEST_FAILURES = new Metric("test_failures", "Unit test failures", "Number of unit test failures", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
078 public static final Metric TEST_SUCCESS_DENSITY = new Metric("test_success_density", "Unit test success (%)", "Ratio of successful unit tests", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_TESTS);
079 public static final Metric TEST_DATA = new Metric("test_data", "Unit tests details", "Unit tests details", Metric.ValueType.DATA, Metric.DIRECTION_WORST, false, DOMAIN_TESTS);
080
081 public static final Metric COVERAGE = new Metric("coverage", "Coverage", "Coverage by unit tests", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_TESTS);
082
083 public static final Metric COVERED_LINES = new Metric("covered_lines", "Covered lines", "Covered lines", Metric.ValueType.INT, Metric.DIRECTION_BETTER, false, DOMAIN_TESTS).setFormula(new SumChildValuesFormula(false));
084 public static final Metric LINE_COVERAGE = new Metric("line_coverage", "Line coverage", "Line coverage", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_TESTS);
085 public static final Metric COVERAGE_LINE_HITS_DATA = new Metric("coverage_line_hits_data", "Coverage hits data", "Code coverage line hits data", Metric.ValueType.DATA, Metric.DIRECTION_NONE, false, DOMAIN_TESTS);
086
087 public static final Metric COVERED_BRANCHES = new Metric("covered_branches", "Covered branches", "Covered branches", Metric.ValueType.INT, Metric.DIRECTION_BETTER, false, DOMAIN_TESTS).setFormula(new SumChildValuesFormula(false));
088 public static final Metric BRANCH_COVERAGE = new Metric("branch_coverage", "Branch coverage", "Branch coverage", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_TESTS);
089 public static final Metric BRANCH_COVERAGE_HITS_DATA = new Metric("branch_coverage_hits_data", "Branch coverage hits", "Branch coverage hits", Metric.ValueType.DATA, Metric.DIRECTION_NONE, false, DOMAIN_TESTS);
090
091 /* duplicated lines */
092 public static final Metric DUPLICATED_LINES = new Metric("duplicated_lines", "Duplicated lines", "Duplicated lines", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE);
093 public static final Metric DUPLICATED_BLOCKS = new Metric("duplicated_blocks", "Duplicated blocks", "Duplicated blocks", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_SIZE);
094 public static final Metric DUPLICATED_FILES = new Metric("duplicated_files", "Duplicated files", "Duplicated files", Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_SIZE);
095 public static final Metric DUPLICATED_LINES_DENSITY = new Metric("duplicated_lines_density", "Duplicated lines (%)", "Duplicated lines balanced by statements", Metric.ValueType.PERCENT, Metric.DIRECTION_WORST, true, DOMAIN_SIZE);
096 public static final Metric DUPLICATIONS_DATA = new Metric("duplications_data", "Duplications details", "Duplications details", Metric.ValueType.DATA, Metric.DIRECTION_NONE, false, DOMAIN_SIZE);
097
098 /* coding rules */
099 public static final Metric USABILITY = new Metric("usability", "Usability", "Usability", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULE_CATEGORIES);
100 public static final Metric RELIABILITY = new Metric("reliability", "Reliability", "Reliability", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULE_CATEGORIES);
101 public static final Metric EFFICIENCY = new Metric("efficiency", "Efficiency", "Efficiency", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULE_CATEGORIES);
102 public static final Metric PORTABILITY = new Metric("portability", "Portability", "Portability", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULE_CATEGORIES);
103 public static final Metric MAINTAINABILITY = new Metric("maintainability", "Maintainability", "Maintainability", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULE_CATEGORIES);
104
105 public static final Metric WEIGHTED_VIOLATIONS = new Metric("weighted_violations", "Weighted violations", "Weighted Violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
106 public static final Metric VIOLATIONS_DENSITY = new Metric("violations_density", "Rules compliance", "Rules compliance", Metric.ValueType.PERCENT, Metric.DIRECTION_BETTER, true, DOMAIN_RULES);
107 public static final Metric VIOLATIONS = new Metric("violations", "Violations", "Violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
108 public static final Metric BLOCKER_VIOLATIONS = new Metric("blocker_violations", "Blocker violations", "Blocker violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
109 public static final Metric CRITICAL_VIOLATIONS = new Metric("critical_violations", "Critical violations", "Critical violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
110 public static final Metric MAJOR_VIOLATIONS = new Metric("major_violations", "Major violations", "Major violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
111 public static final Metric MINOR_VIOLATIONS = new Metric("minor_violations", "Minor violations", "Minor violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
112 public static final Metric INFO_VIOLATIONS = new Metric("info_violations", "Info violations", "Info violations", Metric.ValueType.INT, Metric.DIRECTION_WORST, false, DOMAIN_RULES);
113
114 /* alerts */
115 public static final Metric ALERT_STATUS = new Metric("alert_status", "Alert", "Alert", Metric.ValueType.LEVEL, Metric.DIRECTION_BETTER, true, DOMAIN_GENERAL);
116
117 /* quality profile */
118 public static final Metric PROFILE = new Metric("profile", "Profile", "Selected quality profile", Metric.ValueType.DATA, Metric.DIRECTION_NONE, false, DOMAIN_GENERAL);
119
120 private static List<Metric> metrics = new ArrayList<Metric>();
121
122 private static Set<String> metricKeys = new HashSet<String>();
123
124 public static final Map<String, String> DEPRECATED_KEY = new HashMap<String, String>();
125
126 static {
127 DEPRECATED_KEY.put("ncss", NCLOC.getKey());
128 DEPRECATED_KEY.put("lines", LINES.getKey());
129 DEPRECATED_KEY.put("classes_count", CLASSES.getKey());
130 DEPRECATED_KEY.put("packages_count", PACKAGES.getKey());
131 DEPRECATED_KEY.put("functions_count", FUNCTIONS.getKey());
132 DEPRECATED_KEY.put("files_count", FILES.getKey());
133 DEPRECATED_KEY.put("directories_count", DIRECTORIES.getKey());
134 DEPRECATED_KEY.put("ccn", COMPLEXITY.getKey());
135 DEPRECATED_KEY.put("ccn_class", CLASS_COMPLEXITY.getKey());
136 DEPRECATED_KEY.put("ccn_file", FILE_COMPLEXITY.getKey());
137 DEPRECATED_KEY.put("ccn_function", FUNCTION_COMPLEXITY.getKey());
138 DEPRECATED_KEY.put("ccn_classes_count_distribution", CLASS_COMPLEXITY_DISTRIBUTION.getKey());
139 DEPRECATED_KEY.put("ccn_vs_cc", UNCOVERED_COMPLEXITY_BY_TESTS.getKey());
140 DEPRECATED_KEY.put("comment_ratio", COMMENT_LINES_DENSITY.getKey());
141 DEPRECATED_KEY.put("test_count", TESTS.getKey());
142 DEPRECATED_KEY.put("test_errors_count", TEST_ERRORS.getKey());
143 DEPRECATED_KEY.put("test_skipped_count", SKIPPED_TESTS.getKey());
144 DEPRECATED_KEY.put("test_failures_count", TEST_FAILURES.getKey());
145 DEPRECATED_KEY.put("test_success_percentage", TEST_SUCCESS_DENSITY.getKey());
146 DEPRECATED_KEY.put("test_details", TEST_DATA.getKey());
147 DEPRECATED_KEY.put("code_coverage", COVERAGE.getKey());
148 DEPRECATED_KEY.put("code_coverage_line_hits_data", COVERAGE_LINE_HITS_DATA.getKey());
149 DEPRECATED_KEY.put("duplicated_lines", DUPLICATED_LINES.getKey());
150 DEPRECATED_KEY.put("duplicated_blocks", DUPLICATED_BLOCKS.getKey());
151 DEPRECATED_KEY.put("duplicated_files", DUPLICATED_FILES.getKey());
152 DEPRECATED_KEY.put("duplicated_lines_ratio", DUPLICATED_LINES_DENSITY.getKey());
153 DEPRECATED_KEY.put("duplications_data", DUPLICATIONS_DATA.getKey());
154 DEPRECATED_KEY.put("rules_compliance", VIOLATIONS_DENSITY.getKey());
155 DEPRECATED_KEY.put("rules_violations", VIOLATIONS.getKey());
156 DEPRECATED_KEY.put("rules_index", VIOLATIONS_DENSITY.getKey());
157 DEPRECATED_KEY.put("rules_violations_count", VIOLATIONS.getKey());
158 DEPRECATED_KEY.put("alert_status", ALERT_STATUS.getKey());
159
160 }
161
162
163 public static List<Metric> getMetrics() {
164 if (metrics.isEmpty()) {
165 for (Field field : CoreMetrics.class.getFields()) {
166 if (Metric.class.isAssignableFrom(field.getType())) {
167 try {
168 metrics.add((Metric) field.get(null));
169 } catch (IllegalAccessException e) {
170 throw new SonarException("can not load metrics from " + CoreMetrics.class.getSimpleName(), e);
171 }
172 }
173 }
174 }
175 return metrics;
176 }
177
178 public static Set<String> getKeys() {
179 if (metricKeys.isEmpty()) {
180 for (Metric metric : getMetrics()) {
181 metricKeys.add(metric.getKey());
182 }
183 }
184 return metricKeys;
185 }
186
187 public static String toValidKey(String key) {
188 String value = DEPRECATED_KEY.get(key);
189 if (value == null) {
190 value = key;
191 }
192 return value;
193 }
194 }