1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2018 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.coding; | |
21 | ||
22 | import java.util.regex.Pattern; | |
23 | ||
24 | import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
25 | import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
26 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
27 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
28 | import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | |
29 | ||
30 | /** | |
31 | * <p> | |
32 | * Checks for illegal token text. | |
33 | * </p> | |
34 | * <p> An example of how to configure the check to forbid String literals | |
35 | * containing {@code "a href"} is: | |
36 | * </p> | |
37 | * <pre> | |
38 | * <module name="IllegalTokenText"> | |
39 | * <property name="tokens" value="STRING_LITERAL"/> | |
40 | * <property name="format" value="a href"/> | |
41 | * </module> | |
42 | * </pre> | |
43 | * <p> An example of how to configure the check to forbid leading zeros in an | |
44 | * integer literal, other than zero and a hex literal is: | |
45 | * </p> | |
46 | * <pre> | |
47 | * <module name="IllegalTokenText"> | |
48 | * <property name="tokens" value="NUM_INT,NUM_LONG"/> | |
49 | * <property name="format" value="^0[^lx]"/> | |
50 | * <property name="ignoreCase" value="true"/> | |
51 | * </module> | |
52 | * </pre> | |
53 | * @author Rick Giles | |
54 | */ | |
55 | @StatelessCheck | |
56 | public class IllegalTokenTextCheck | |
57 | extends AbstractCheck { | |
58 | ||
59 | /** | |
60 | * A key is pointing to the warning message text in "messages.properties" | |
61 | * file. | |
62 | */ | |
63 | public static final String MSG_KEY = "illegal.token.text"; | |
64 | ||
65 | /** | |
66 | * Custom message for report if illegal regexp found | |
67 | * ignored if empty. | |
68 | */ | |
69 | private String message = ""; | |
70 | ||
71 | /** The format string of the regexp. */ | |
72 | private String formatString = "$^"; | |
73 | ||
74 | /** The regexp to match against. */ | |
75 | private Pattern format = Pattern.compile(formatString); | |
76 | ||
77 | /** {@code true} if the casing should be ignored. */ | |
78 | private boolean ignoreCase; | |
79 | ||
80 | @Override | |
81 | public int[] getDefaultTokens() { | |
82 |
1
1. getDefaultTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::getDefaultTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
83 | } | |
84 | ||
85 | @Override | |
86 | public int[] getAcceptableTokens() { | |
87 |
1
1. getAcceptableTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::getAcceptableTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new int[] { |
88 | TokenTypes.NUM_DOUBLE, | |
89 | TokenTypes.NUM_FLOAT, | |
90 | TokenTypes.NUM_INT, | |
91 | TokenTypes.NUM_LONG, | |
92 | TokenTypes.IDENT, | |
93 | TokenTypes.COMMENT_CONTENT, | |
94 | TokenTypes.STRING_LITERAL, | |
95 | TokenTypes.CHAR_LITERAL, | |
96 | }; | |
97 | } | |
98 | ||
99 | @Override | |
100 | public int[] getRequiredTokens() { | |
101 |
1
1. getRequiredTokens : mutated return of Object value for com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::getRequiredTokens to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CommonUtils.EMPTY_INT_ARRAY; |
102 | } | |
103 | ||
104 | @Override | |
105 | public boolean isCommentNodesRequired() { | |
106 |
1
1. isCommentNodesRequired : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return true; |
107 | } | |
108 | ||
109 | @Override | |
110 | public void visitToken(DetailAST ast) { | |
111 | final String text = ast.getText(); | |
112 |
1
1. visitToken : negated conditional → KILLED |
if (format.matcher(text).find()) { |
113 | String customMessage = message; | |
114 |
1
1. visitToken : negated conditional → KILLED |
if (customMessage.isEmpty()) { |
115 | customMessage = MSG_KEY; | |
116 | } | |
117 |
1
1. visitToken : removed call to com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::log → KILLED |
log( |
118 | ast.getLineNo(), | |
119 | ast.getColumnNo(), | |
120 | customMessage, | |
121 | formatString); | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
126 | * Setter for message property. | |
127 | * @param message custom message which should be used | |
128 | * to report about violations. | |
129 | */ | |
130 | public void setMessage(String message) { | |
131 |
1
1. setMessage : negated conditional → KILLED |
if (message == null) { |
132 | this.message = ""; | |
133 | } | |
134 | else { | |
135 | this.message = message; | |
136 | } | |
137 | } | |
138 | ||
139 | /** | |
140 | * Set the format to the specified regular expression. | |
141 | * @param format a {@code String} value | |
142 | * @throws org.apache.commons.beanutils.ConversionException unable to parse format | |
143 | */ | |
144 | public void setFormat(String format) { | |
145 | formatString = format; | |
146 |
1
1. setFormat : removed call to com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::updateRegexp → KILLED |
updateRegexp(); |
147 | } | |
148 | ||
149 | /** | |
150 | * Set whether or not the match is case sensitive. | |
151 | * @param caseInsensitive true if the match is case insensitive. | |
152 | * @noinspection BooleanParameter | |
153 | */ | |
154 | public void setIgnoreCase(boolean caseInsensitive) { | |
155 | ignoreCase = caseInsensitive; | |
156 |
1
1. setIgnoreCase : removed call to com/puppycrawl/tools/checkstyle/checks/coding/IllegalTokenTextCheck::updateRegexp → KILLED |
updateRegexp(); |
157 | } | |
158 | ||
159 | /** | |
160 | * Updates the {@link #format} based on the values from {@link #formatString} and | |
161 | * {@link #ignoreCase}. | |
162 | */ | |
163 | private void updateRegexp() { | |
164 | final int compileFlags; | |
165 |
1
1. updateRegexp : negated conditional → KILLED |
if (ignoreCase) { |
166 | compileFlags = Pattern.CASE_INSENSITIVE; | |
167 | } | |
168 | else { | |
169 | compileFlags = 0; | |
170 | } | |
171 | format = CommonUtils.createPattern(formatString, compileFlags); | |
172 | } | |
173 | ||
174 | } | |
Mutations | ||
82 |
1.1 |
|
87 |
1.1 |
|
101 |
1.1 |
|
106 |
1.1 |
|
112 |
1.1 |
|
114 |
1.1 |
|
117 |
1.1 |
|
131 |
1.1 |
|
146 |
1.1 |
|
156 |
1.1 |
|
165 |
1.1 |