3636import java .util .ArrayList ;
3737import java .util .Arrays ;
3838import java .util .Collection ;
39- import java .util .HashMap ;
4039import java .util .List ;
41- import java .util .Map ;
4240import java .util .regex .Pattern ;
4341
4442/**
@@ -69,7 +67,7 @@ public final class JavadocTagsCheck extends AbstractCheck {
6967 /**
7068 * Map of tag and its pattern.
7169 */
72- private final Map < String , Pattern > tags = new HashMap <>();
70+ private final List < RequiredJavaDocTag > required = new ArrayList <>(1 );
7371
7472 /**
7573 * List of prohibited javadoc tags.
@@ -97,10 +95,13 @@ public int[] getRequiredTokens() {
9795
9896 @ Override
9997 public void init () {
100- this .tags .put (
101- "since" ,
102- Pattern .compile (
98+ this .required .add (
99+ new RequiredJavaDocTag (
100+ "since" ,
101+ Pattern .compile (
103102 "^\\ d+(\\ .\\ d+){1,2}(\\ .[0-9A-Za-z-]+(\\ .[0-9A-Za-z-]+)*)?$"
103+ ),
104+ this ::log
104105 )
105106 );
106107 }
@@ -115,34 +116,26 @@ public void visitToken(final DetailAST ast) {
115116 for (final String tag : this .prohibited ) {
116117 this .findProhibited (lines , start , cstart , cend , tag );
117118 }
118- for (final String tag : this .tags . keySet () ) {
119- this .matchTagFormat (lines , cstart , cend , tag );
119+ for (final RequiredJavaDocTag tag : this .required ) {
120+ tag .matchTagFormat (lines , cstart , cend );
120121 }
121122 } else {
122123 this .log (0 , "Problem finding class/interface comment" );
123124 }
124125 }
125126
126- /**
127- * Get the text of the given tag.
128- * @param line Line with the tag.
129- * @return The text of the tag.
130- */
131- private static String getTagText (final String line ) {
132- return line .substring (
133- line .indexOf (' ' , line .indexOf ('@' )) + 1
134- );
135- }
136-
137127 /**
138128 * Find a text in lines, by going up.
139129 * @param lines List of lines to check.
140130 * @param start Start searching from this line number.
141131 * @param text Text to find.
142132 * @return Line number with found text, or -1 if it wasn't found.
143133 */
144- private static int findTrimmedTextUp (final String [] lines ,
145- final int start , final String text ) {
134+ private static int findTrimmedTextUp (
135+ final String [] lines ,
136+ final int start ,
137+ final String text
138+ ) {
146139 int found = -1 ;
147140 for (int pos = start - 1 ; pos >= 0 ; pos -= 1 ) {
148141 if (lines [pos ].trim ().equals (text )) {
@@ -182,8 +175,13 @@ private static int findCommentEnd(final String[] lines, final int start) {
182175 * @param tag Name of the tag.
183176 * @checkstyle ParameterNumber (3 lines)
184177 */
185- private void findProhibited (final String [] lines , final int start ,
186- final int cstart , final int cend , final String tag ) {
178+ private void findProhibited (
179+ final String [] lines ,
180+ final int start ,
181+ final int cstart ,
182+ final int cend ,
183+ final String tag
184+ ) {
187185 final List <Integer > found =
188186 this .findTagLineNum (lines , cstart , cend , tag );
189187 if (!found .isEmpty ()) {
@@ -195,38 +193,6 @@ private void findProhibited(final String[] lines, final int start,
195193 }
196194 }
197195
198- /**
199- * Check if the tag text matches the format from pattern.
200- * @param lines List of all lines.
201- * @param start Line number where comment starts.
202- * @param end Line number where comment ends.
203- * @param tag Name of the tag.
204- * @checkstyle ParameterNumber (3 lines)
205- */
206- private void matchTagFormat (final String [] lines , final int start ,
207- final int end , final String tag ) {
208- final List <Integer > found = this .findTagLineNum (lines , start , end , tag );
209- if (found .isEmpty ()) {
210- this .log (
211- start + 1 ,
212- "Missing ''@{0}'' tag in class/interface comment" ,
213- tag
214- );
215- return ;
216- }
217- for (final Integer item : found ) {
218- final String text = JavadocTagsCheck .getTagText (lines [item ]);
219- if (!this .tags .get (tag ).matcher (text ).matches ()) {
220- this .log (
221- item + 1 ,
222- "Tag text ''{0}'' does not match the pattern ''{1}''" ,
223- text ,
224- this .tags .get (tag ).toString ()
225- );
226- }
227- }
228- }
229-
230196 /**
231197 * Find given tag in comment lines.
232198 * @param lines Lines to search for the tag.
@@ -236,8 +202,12 @@ private void matchTagFormat(final String[] lines, final int start,
236202 * @return Line number with found tag or -1 otherwise.
237203 * @checkstyle ParameterNumber (3 lines)
238204 */
239- private List <Integer > findTagLineNum (final String [] lines , final int start ,
240- final int end , final String tag ) {
205+ private List <Integer > findTagLineNum (
206+ final String [] lines ,
207+ final int start ,
208+ final int end ,
209+ final String tag
210+ ) {
241211 final String prefix = String .format (" * @%s " , tag );
242212 final List <Integer > found = new ArrayList <>(1 );
243213 for (int pos = start ; pos <= end ; pos += 1 ) {
0 commit comments