*/
package org.sonar.java.ast.check;
-import org.sonar.check.BelongsToProfile;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-
+import com.google.common.collect.Sets;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.TextBlock;
+import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import org.apache.commons.lang.StringUtils;
+import org.sonar.check.BelongsToProfile;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.java.ast.visitor.AstUtils;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.recognizer.CodeRecognizer;
-import com.google.common.collect.Sets;
-import com.puppycrawl.tools.checkstyle.api.DetailAST;
-import com.puppycrawl.tools.checkstyle.api.TextBlock;
-import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
/**
* @since 2.13
}
for (List<TextBlock> listOfComments : getFileContents().getCComments().values()) {
// This list contains not only comments in C style, but also documentation comments and JSNI comments
- comments.addAll(listOfComments);
+ for (TextBlock comment : listOfComments) {
+ if (!isHeader(comment)) {
+ comments.addAll(listOfComments);
+ }
+ }
}
}
+ /**
+ * We assume that comment on a first line - is a header with license.
+ * However possible to imagine corner case: file may contain commented-out code starting from first line.
+ * But we assume that probability of this is really low.
+ */
+ private static boolean isHeader(TextBlock comment) {
+ return comment.getStartLineNo() == 1;
+ }
+
/**
* Removes documentation comments and JSNI comments from candidates for commented-out code in order to prevent false-positives.
*/
*/
package org.sonar.java.ast.check;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.java.CheckMessages;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.measures.Metric;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
public class CommentedOutCodeLineCheckTest {
private Squid squid;
SourceFile sourceFile = (SourceFile) squid.search("CommentedCode.java");
CheckMessages checkMessages = new CheckMessages(sourceFile);
- checkMessages.assertNext().atLine(26).withMessage("This block of commented-out lines of code should be removed.");
- checkMessages.assertNext().atLine(27);
- checkMessages.assertNext().atLine(28);
-
- checkMessages.assertNext().atLine(32);
+ checkMessages.assertNext().atLine(32).withMessage("This block of commented-out lines of code should be removed.");
+ checkMessages.assertNext().atLine(33);
+ checkMessages.assertNext().atLine(34);
checkMessages.assertNext().atLine(38);
- checkMessages.assertNext().atLine(39);
- checkMessages.assertNext().atLine(40);
checkMessages.assertNext().atLine(44);
+ checkMessages.assertNext().atLine(45);
+ checkMessages.assertNext().atLine(46);
+
+ checkMessages.assertNext().atLine(50);
- checkMessages.assertNext().atLine(60);
+ checkMessages.assertNext().atLine(66);
- checkMessages.assertNext().atLine(69);
+ checkMessages.assertNext().atLine(75);
checkMessages.assertNoMore();
- assertThat(sourceFile.getInt(Metric.COMMENT_LINES), is(40)); // TODO but in fact 46, so without fake-JSNI
- assertThat(sourceFile.getInt(Metric.COMMENT_BLANK_LINES), is(16));
+ assertThat(sourceFile.getInt(Metric.COMMENT_LINES), is(44)); // TODO but in fact 50, so without fake-JSNI
+ assertThat(sourceFile.getInt(Metric.COMMENT_BLANK_LINES), is(17));
}
}