2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar 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.
11 * Sonar 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.java.ast.check;
22 import java.util.Arrays;
23 import java.util.List;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.check.Priority;
28 import org.sonar.check.Rule;
29 import org.sonar.java.ast.visitor.AstUtils;
30 import org.sonar.java.ast.visitor.JavaAstVisitor;
31 import org.sonar.java.recognizer.JavaFootprint;
32 import org.sonar.squid.api.CheckMessage;
33 import org.sonar.squid.api.SourceFile;
34 import org.sonar.squid.recognizer.CodeRecognizer;
36 import com.google.common.collect.Sets;
37 import com.puppycrawl.tools.checkstyle.api.DetailAST;
38 import com.puppycrawl.tools.checkstyle.api.TextBlock;
39 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
44 @Rule(key = "CommentedOutCodeLine", priority = Priority.MAJOR)
45 public class CommentedOutCodeLineCheck extends JavaAstVisitor {
47 private static final double THRESHOLD = 0.9;
50 * This list was taken from com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck
52 private static final List<Integer> WANTED_TOKENS = Arrays.asList(
53 TokenTypes.INTERFACE_DEF,
55 TokenTypes.ANNOTATION_DEF,
57 TokenTypes.METHOD_DEF,
59 TokenTypes.VARIABLE_DEF,
60 TokenTypes.ENUM_CONSTANT_DEF,
61 TokenTypes.ANNOTATION_FIELD_DEF,
62 TokenTypes.PACKAGE_DEF);
64 private final CodeRecognizer codeRecognizer;
65 private Set<TextBlock> comments;
67 public CommentedOutCodeLineCheck() {
68 codeRecognizer = new CodeRecognizer(THRESHOLD, new JavaFootprint());
72 public List<Integer> getWantedTokens() {
77 * Creates candidates for commented-out code - all comment blocks.
80 public void visitFile(DetailAST ast) {
81 comments = Sets.newHashSet();
82 for (TextBlock comment : getFileContents().getCppComments().values()) {
83 comments.add(comment);
85 for (List<TextBlock> listOfComments : getFileContents().getCComments().values()) {
86 // This list contains not only comments in C style, but also documentation comments and JSNI comments
87 comments.addAll(listOfComments);
92 * Removes documentation comments and JSNI comments from candidates for commented-out code in order to prevent false-positives.
95 public void visitToken(DetailAST ast) {
96 if (canBeDocumented(ast)) {
97 TextBlock javadoc = getFileContents().getJavadocBefore(ast.getLineNo());
98 if (javadoc != null) {
99 comments.remove(javadoc);
102 removeJSNIComments(ast);
106 * From documentation for Javadoc-tool:
107 * Documentation comments should be recognized only when placed
108 * immediately before class, interface, constructor, method, or field declarations.
110 private static boolean canBeDocumented(DetailAST ast) {
111 if (AstUtils.isType(ast, TokenTypes.VARIABLE_DEF)) {
112 return AstUtils.isClassVariable(ast);
118 * Detects commented-out code in remaining candidates.
121 public void leaveFile(DetailAST ast) {
122 SourceFile sourceFile = (SourceFile) peekSourceCode();
123 for (TextBlock comment : comments) {
124 String[] lines = comment.getText();
125 for (int i = 0; i < lines.length; i++) {
126 if (codeRecognizer.isLineOfCode(lines[i])) {
127 CheckMessage message = new CheckMessage(this, "This block of commented-out lines of code should be removed.");
128 message.setLine(comment.getStartLineNo() + i);
129 sourceFile.log(message);
138 * From GWT documentation:
139 * JSNI methods are declared native and contain JavaScript code in a specially formatted comment block
140 * between the end of the parameter list and the trailing semicolon.
141 * A JSNI comment block begins with the exact token {@link #START_JSNI} and ends with the exact token {@link #END_JSNI}.
143 private void removeJSNIComments(DetailAST ast) {
144 if (AstUtils.isType(ast, TokenTypes.METHOD_DEF) && AstUtils.isModifier(ast, TokenTypes.LITERAL_NATIVE)) {
145 DetailAST endOfParameterList = ast.findFirstToken(TokenTypes.PARAMETERS).getNextSibling();
146 DetailAST trailingSemicolon = ast.getLastChild();
148 for (int lineNumber = endOfParameterList.getLineNo(); lineNumber <= trailingSemicolon.getLineNo(); lineNumber++) {
149 List<TextBlock> listOfComments = getFileContents().getCComments().get(lineNumber);
150 if (listOfComments != null) {
151 for (TextBlock comment : listOfComments) {
152 if (isJSNI(comment) && isCommentBetween(comment, endOfParameterList, trailingSemicolon)) {
153 comments.remove(comment);
161 private static boolean isCommentBetween(TextBlock comment, DetailAST start, DetailAST end) {
162 return comment.intersects(start.getLineNo(), start.getColumnNo(), end.getLineNo(), end.getColumnNo());
165 private static final String START_JSNI = "/*-{";
166 private static final String END_JSNI = "}-*/";
168 private boolean isJSNI(TextBlock comment) {
169 String[] lines = comment.getText();
170 String firstLine = lines[0];
171 String lastLine = lines[lines.length - 1];
172 return StringUtils.startsWith(firstLine, START_JSNI) && StringUtils.endsWith(lastLine, END_JSNI);