1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package com.puppycrawl.tools.checkstyle.checks.annotation; |
20 | |
|
21 | |
import java.util.regex.Matcher; |
22 | |
import java.util.regex.Pattern; |
23 | |
|
24 | |
import com.puppycrawl.tools.checkstyle.api.AnnotationUtility; |
25 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
26 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
27 | |
import com.puppycrawl.tools.checkstyle.api.JavadocTagInfo; |
28 | |
import com.puppycrawl.tools.checkstyle.api.TextBlock; |
29 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
30 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 4 | public final class MissingDeprecatedCheck extends Check |
75 | |
{ |
76 | |
|
77 | |
private static final String DEPRECATED = "Deprecated"; |
78 | |
|
79 | |
|
80 | |
private static final String FQ_DEPRECATED = "java.lang." + DEPRECATED; |
81 | |
|
82 | |
|
83 | 1 | private static final Pattern MATCH_DEPRECATED = |
84 | |
Utils.createPattern("@(deprecated)\\s+\\S"); |
85 | |
|
86 | |
|
87 | 1 | private static final Pattern MATCH_DEPRECATED_MULTILINE_START = |
88 | |
Utils.createPattern("@(deprecated)\\s*$"); |
89 | |
|
90 | |
|
91 | 1 | private static final Pattern MATCH_DEPRECATED_MULTILINE_CONT = |
92 | |
Utils.createPattern("(\\*/|@|[^\\s\\*])"); |
93 | |
|
94 | |
|
95 | |
private static final String END_JAVADOC = "*/"; |
96 | |
|
97 | |
private static final String NEXT_TAG = "@"; |
98 | |
|
99 | |
|
100 | |
@Override |
101 | |
public int[] getDefaultTokens() |
102 | |
{ |
103 | 4 | return this.getAcceptableTokens(); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
@Override |
108 | |
public int[] getAcceptableTokens() |
109 | |
{ |
110 | 4 | return new int[] { |
111 | |
TokenTypes.INTERFACE_DEF, |
112 | |
TokenTypes.CLASS_DEF, |
113 | |
TokenTypes.ANNOTATION_DEF, |
114 | |
TokenTypes.ENUM_DEF, |
115 | |
TokenTypes.METHOD_DEF, |
116 | |
TokenTypes.CTOR_DEF, |
117 | |
TokenTypes.VARIABLE_DEF, |
118 | |
TokenTypes.ENUM_CONSTANT_DEF, |
119 | |
TokenTypes.ANNOTATION_FIELD_DEF, |
120 | |
}; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
@Override |
125 | |
public void visitToken(final DetailAST aAST) |
126 | |
{ |
127 | 41 | final TextBlock javadoc = |
128 | |
this.getFileContents().getJavadocBefore(aAST.getLineNo()); |
129 | |
|
130 | 41 | final boolean containsAnnotation = |
131 | |
AnnotationUtility.containsAnnotation(aAST, DEPRECATED) |
132 | |
|| AnnotationUtility.containsAnnotation(aAST, FQ_DEPRECATED); |
133 | |
|
134 | 41 | final boolean containsJavadocTag = this.containsJavadocTag(javadoc); |
135 | |
|
136 | 41 | if (containsAnnotation ^ containsJavadocTag) { |
137 | 20 | this.log(aAST.getLineNo(), "annotation.missing.deprecated"); |
138 | |
} |
139 | 41 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
private boolean containsJavadocTag(final TextBlock aJavadoc) |
148 | |
{ |
149 | 41 | if (aJavadoc == null) { |
150 | 7 | return false; |
151 | |
} |
152 | |
|
153 | 34 | final String[] lines = aJavadoc.getText(); |
154 | |
|
155 | 34 | boolean found = false; |
156 | |
|
157 | 34 | int currentLine = aJavadoc.getStartLineNo() - 1; |
158 | |
|
159 | 145 | for (int i = 0; i < lines.length; i++) { |
160 | 111 | currentLine++; |
161 | 111 | final String line = lines[i]; |
162 | |
|
163 | 111 | final Matcher javadocNoargMatcher = |
164 | |
MissingDeprecatedCheck.MATCH_DEPRECATED.matcher(line); |
165 | 111 | final Matcher noargMultilineStart = |
166 | |
MissingDeprecatedCheck. |
167 | |
MATCH_DEPRECATED_MULTILINE_START.matcher(line); |
168 | |
|
169 | 111 | if (javadocNoargMatcher.find()) { |
170 | 24 | if (found) { |
171 | 3 | this.log(currentLine, "javadoc.duplicateTag", |
172 | |
JavadocTagInfo.DEPRECATED.getText()); |
173 | |
} |
174 | 24 | found = true; |
175 | |
} |
176 | 87 | else if (noargMultilineStart.find()) { |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | 9 | for (int remIndex = i + 1; |
183 | 27 | remIndex < lines.length; remIndex++) |
184 | |
{ |
185 | 9 | final Matcher multilineCont = |
186 | |
MissingDeprecatedCheck.MATCH_DEPRECATED_MULTILINE_CONT |
187 | |
.matcher(lines[remIndex]); |
188 | |
|
189 | 9 | if (multilineCont.find()) { |
190 | 9 | remIndex = lines.length; |
191 | 9 | final String lFin = multilineCont.group(1); |
192 | 9 | if (!lFin.equals(MissingDeprecatedCheck.NEXT_TAG) |
193 | |
&& !lFin.equals(MissingDeprecatedCheck.END_JAVADOC)) |
194 | |
{ |
195 | 3 | if (found) { |
196 | 0 | this.log(currentLine, "javadoc.duplicateTag", |
197 | |
JavadocTagInfo.DEPRECATED.getText()); |
198 | |
} |
199 | 3 | found = true; |
200 | |
} |
201 | |
else { |
202 | 6 | this.log(currentLine, "javadoc.missing"); |
203 | 6 | if (found) { |
204 | 2 | this.log(currentLine, "javadoc.duplicateTag", |
205 | |
JavadocTagInfo.DEPRECATED.getText()); |
206 | |
} |
207 | 6 | found = true; |
208 | |
} |
209 | |
} |
210 | |
} |
211 | |
} |
212 | |
} |
213 | 34 | return found; |
214 | |
} |
215 | |
} |