From f02ad88827e09165aa7433b2951085db4503055a Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 9 Feb 2012 19:37:22 +0400 Subject: [PATCH] SONAR-3210 Compute new measure for Java files - comments in file --- .../java/ast/visitor/FileLinesVisitor.java | 7 ++++-- .../ast/visitor/FileLinesVisitorTest.java | 4 ++++ .../org/sonar/api/measures/CoreMetrics.java | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java index 3c1788cba46..596df2cc277 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java @@ -61,8 +61,11 @@ public class FileLinesVisitor extends JavaAstVisitor { Source source = getSource(); for (int line = 1; line <= source.getNumberOfLines(); line++) { - int linesOfCode = source.getMeasure(Metric.LINES_OF_CODE, line, line); - measures.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode); + int lineOfCode = source.getMeasure(Metric.LINES_OF_CODE, line, line); + measures.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, lineOfCode); + + int comment = source.getMeasure(Metric.COMMENT_LINES, line, line); + measures.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, comment); } measures.save(); } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java index eb2d0808932..a4be9d8ff2d 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java @@ -62,6 +62,10 @@ public class FileLinesVisitorTest { verify(measures).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 1, 1); verify(measures).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 2, 0); verify(measures).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 3, 1); + + verify(measures).setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, 1, 0); + verify(measures).setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, 18, 1); + verify(measures).save(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java index 45d1bd15619..a8f657d0373 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java @@ -1651,6 +1651,10 @@ public final class CoreMetrics { public static final String NCLOC_DATA_KEY = "ncloc_data"; /** + * Information about lines of code in file. + * Key-value pairs, where key - is a number of line, and value - is an indicator of whether line contains code (1) or not (0). + * + * @see org.sonar.api.measures.FileLinesContext * @since 2.14 */ @Beta @@ -1659,6 +1663,25 @@ public final class CoreMetrics { .setDomain(DOMAIN_SIZE) .create(); + /** + * @since 2.14 + */ + @Beta + public static final String COMMENT_LINES_DATA_KEY = "comment_lines_data"; + + /** + * Information about comments in file. + * Key-value pairs, where key - is a number of line, and value - is an indicator of whether line contains comment (1) or not (0). + * + * @see org.sonar.api.measures.FileLinesContext + * @since 2.14 + */ + @Beta + public static final Metric COMMENT_LINES_DATA = new Metric.Builder(COMMENT_LINES_DATA_KEY, "comment_lines_data", Metric.ValueType.DATA) + .setHidden(true) + .setDomain(DOMAIN_DOCUMENTATION) + .create(); + //-------------------------------------------------------------------------------------------------------------------- // // OTHERS -- 2.39.5