| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2017 the original author or authors. | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | ||
| 20 | package com.puppycrawl.tools.checkstyle.checks; | |
| 21 | ||
| 22 | import java.util.HashSet; | |
| 23 | import java.util.List; | |
| 24 | import java.util.Map; | |
| 25 | import java.util.Set; | |
| 26 | import java.util.regex.Pattern; | |
| 27 | ||
| 28 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
| 29 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 30 | import com.puppycrawl.tools.checkstyle.api.TextBlock; | |
| 31 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
| 32 | ||
| 33 | /** | |
| 34 | * <p> | |
| 35 | * The check to ensure that comments are the only thing on a line. | |
| 36 | * For the case of // comments that means that the only thing that should | |
| 37 | * precede it is whitespace. | |
| 38 | * It doesn't check comments if they do not end line, i.e. it accept | |
| 39 | * the following: | |
| 40 | * {@code Thread.sleep( 10 <some comment here> );} | |
| 41 | * Format property is intended to deal with the "} // while" example. | |
| 42 | * </p> | |
| 43 | * | |
| 44 | * <p>Rationale: Steve McConnell in "Code Complete" suggests that endline | |
| 45 | * comments are a bad practice. An end line comment would | |
| 46 | * be one that is on the same line as actual code. For example: | |
| 47 | * <pre> | |
| 48 | * a = b + c; // Some insightful comment | |
| 49 | * d = e / f; // Another comment for this line | |
| 50 | * </pre> | |
| 51 | * Quoting "Code Complete" for the justification: | |
| 52 | * <ul> | |
| 53 | * <li> | |
| 54 | * "The comments have to be aligned so that they do not | |
| 55 | * interfere with the visual structure of the code. If you don't | |
| 56 | * align them neatly, they'll make your listing look like it's been | |
| 57 | * through a washing machine." | |
| 58 | * </li> | |
| 59 | * <li> | |
| 60 | * "Endline comments tend to be hard to format...It takes time | |
| 61 | * to align them. Such time is not spent learning more about | |
| 62 | * the code; it's dedicated solely to the tedious task of | |
| 63 | * pressing the spacebar or tab key." | |
| 64 | * </li> | |
| 65 | * <li> | |
| 66 | * "Endline comments are also hard to maintain. If the code on | |
| 67 | * any line containing an endline comment grows, it bumps the | |
| 68 | * comment farther out, and all the other endline comments will | |
| 69 | * have to bumped out to match. Styles that are hard to | |
| 70 | * maintain aren't maintained...." | |
| 71 | * </li> | |
| 72 | * <li> | |
| 73 | * "Endline comments also tend to be cryptic. The right side of | |
| 74 | * the line doesn't offer much room and the desire to keep the | |
| 75 | * comment on one line means the comment must be short. | |
| 76 | * Work then goes into making the line as short as possible | |
| 77 | * instead of as clear as possible. The comment usually ends | |
| 78 | * up as cryptic as possible...." | |
| 79 | * </li> | |
| 80 | * <li> | |
| 81 | * "A systemic problem with endline comments is that it's hard | |
| 82 | * to write a meaningful comment for one line of code. Most | |
| 83 | * endline comments just repeat the line of code, which hurts | |
| 84 | * more than it helps." | |
| 85 | * </li> | |
| 86 | * </ul> | |
| 87 | * His comments on being hard to maintain when the size of | |
| 88 | * the line changes are even more important in the age of | |
| 89 | * automated refactorings. | |
| 90 | * | |
| 91 | * <p>To configure the check so it enforces only comment on a line: | |
| 92 | * <pre> | |
| 93 | * <module name="TrailingComment"> | |
| 94 | * <property name="format" value="^\\s*$"/> | |
| 95 | * </module> | |
| 96 | * </pre> | |
| 97 | * | |
| 98 | * @author o_sukhodolsky | |
| 99 | */ | |
| 100 | public class TrailingCommentCheck extends AbstractCheck { | |
| 101 | ||
| 102 | /** | |
| 103 | * A key is pointing to the warning message text in "messages.properties" | |
| 104 | * file. | |
| 105 | */ | |
| 106 | public static final String MSG_KEY = "trailing.comments"; | |
| 107 | ||
| 108 | /** Pattern for legal trailing comment. */ | |
| 109 | private Pattern legalComment; | |
| 110 | ||
| 111 | /** The regexp to match against. */ | |
| 112 | private Pattern format = Pattern.compile("^[\\s});]*$"); | |
| 113 | ||
| 114 | /** | |
| 115 | * Sets patter for legal trailing comments. | |
| 116 | * @param legalComment pattern to set. | |
| 117 | */ | |
| 118 | public void setLegalComment(final Pattern legalComment) { | |
| 119 | this.legalComment = legalComment; | |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Set the format for the specified regular expression. | |
| 124 | * @param pattern a pattern | |
| 125 | */ | |
| 126 | public final void setFormat(Pattern pattern) { | |
| 127 | format = pattern; | |
| 128 | } | |
| 129 | ||
| 130 | @Override | |
| 131 | public int[] getDefaultTokens() { | |
| 132 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 133 | } | |
| 134 | ||
| 135 | @Override | |
| 136 | public int[] getAcceptableTokens() { | |
| 137 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 138 | } | |
| 139 | ||
| 140 | @Override | |
| 141 | public int[] getRequiredTokens() { | |
| 142 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
| 143 | } | |
| 144 | ||
| 145 | @Override | |
| 146 | public void visitToken(DetailAST ast) { | |
| 147 | throw new IllegalStateException("visitToken() shouldn't be called."); | |
| 148 | } | |
| 149 | ||
| 150 | @Override | |
| 151 | public void beginTree(DetailAST rootAST) { | |
| 152 | final Map<Integer, TextBlock> cppComments = getFileContents() | |
| 153 | .getSingleLineComments(); | |
| 154 | final Map<Integer, List<TextBlock>> cComments = getFileContents() | |
| 155 | .getBlockComments(); | |
| 156 | final Set<Integer> lines = new HashSet<>(); | |
| 157 | lines.addAll(cppComments.keySet()); | |
| 158 | lines.addAll(cComments.keySet()); | |
| 159 | ||
| 160 |
1
1. beginTree : negated conditional → KILLED |
for (Integer lineNo : lines) { |
| 161 |
1
1. beginTree : Replaced integer subtraction with addition → KILLED |
final String line = getLines()[lineNo - 1]; |
| 162 | final String lineBefore; | |
| 163 | final TextBlock comment; | |
| 164 |
1
1. beginTree : negated conditional → KILLED |
if (cppComments.containsKey(lineNo)) { |
| 165 | comment = cppComments.get(lineNo); | |
| 166 | lineBefore = line.substring(0, comment.getStartColNo()); | |
| 167 | } | |
| 168 | else { | |
| 169 | final List<TextBlock> commentList = cComments.get(lineNo); | |
| 170 |
1
1. beginTree : Replaced integer subtraction with addition → KILLED |
comment = commentList.get(commentList.size() - 1); |
| 171 | lineBefore = line.substring(0, comment.getStartColNo()); | |
| 172 | ||
| 173 | // do not check comment which doesn't end line | |
| 174 |
1
1. beginTree : negated conditional → KILLED |
if (comment.getText().length == 1 |
| 175 |
1
1. beginTree : negated conditional → KILLED |
&& !CommonUtils.isBlank(line |
| 176 |
1
1. beginTree : Replaced integer addition with subtraction → KILLED |
.substring(comment.getEndColNo() + 1))) { |
| 177 | continue; | |
| 178 | } | |
| 179 | } | |
| 180 |
1
1. beginTree : negated conditional → KILLED |
if (!format.matcher(lineBefore).find() |
| 181 |
1
1. beginTree : negated conditional → KILLED |
&& !isLegalComment(comment)) { |
| 182 |
1
1. beginTree : removed call to com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck::log → KILLED |
log(lineNo, MSG_KEY); |
| 183 | } | |
| 184 | } | |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Checks if given comment is legal (single-line and matches to the | |
| 189 | * pattern). | |
| 190 | * @param comment comment to check. | |
| 191 | * @return true if the comment if legal. | |
| 192 | */ | |
| 193 | private boolean isLegalComment(final TextBlock comment) { | |
| 194 | final boolean legal; | |
| 195 | ||
| 196 | // multi-line comment can not be legal | |
| 197 |
2
1. isLegalComment : negated conditional → KILLED 2. isLegalComment : negated conditional → KILLED |
if (legalComment == null || comment.getStartLineNo() != comment.getEndLineNo()) { |
| 198 | legal = false; | |
| 199 | } | |
| 200 | else { | |
| 201 | String commentText = comment.getText()[0]; | |
| 202 | // remove chars which start comment | |
| 203 | commentText = commentText.substring(2); | |
| 204 | // if this is a C-style comment we need to remove its end | |
| 205 |
1
1. isLegalComment : negated conditional → KILLED |
if (commentText.endsWith("*/")) { |
| 206 |
1
1. isLegalComment : Replaced integer subtraction with addition → KILLED |
commentText = commentText.substring(0, commentText.length() - 2); |
| 207 | } | |
| 208 | commentText = commentText.trim(); | |
| 209 | legal = legalComment.matcher(commentText).find(); | |
| 210 | } | |
| 211 |
1
1. isLegalComment : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return legal; |
| 212 | } | |
| 213 | } | |
Mutations | ||
| 132 |
1.1 |
|
| 137 |
1.1 |
|
| 142 |
1.1 |
|
| 160 |
1.1 |
|
| 161 |
1.1 |
|
| 164 |
1.1 |
|
| 170 |
1.1 |
|
| 174 |
1.1 |
|
| 175 |
1.1 |
|
| 176 |
1.1 |
|
| 180 |
1.1 |
|
| 181 |
1.1 |
|
| 182 |
1.1 |
|
| 197 |
1.1 2.2 |
|
| 205 |
1.1 |
|
| 206 |
1.1 |
|
| 211 |
1.1 |