1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.javadoc;
21
22 import java.util.Arrays;
23 import java.util.BitSet;
24 import java.util.List;
25
26 import com.puppycrawl.tools.checkstyle.PropertyType;
27 import com.puppycrawl.tools.checkstyle.StatelessCheck;
28 import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
29 import com.puppycrawl.tools.checkstyle.api.DetailAST;
30 import com.puppycrawl.tools.checkstyle.api.DetailNode;
31 import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
32 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33 import com.puppycrawl.tools.checkstyle.internal.annotation.PreserveOrder;
34 import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
35 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @StatelessCheck
51 public class AtclauseOrderCheck extends AbstractJavadocCheck {
52
53
54
55
56
57 public static final String MSG_KEY = "at.clause.order";
58
59
60
61
62 private static final String[] DEFAULT_ORDER = {
63 "@author", "@version",
64 "@param", "@return",
65 "@throws", "@exception",
66 "@see", "@since",
67 "@serial", "@serialField",
68 "@serialData", "@deprecated",
69 };
70
71
72
73
74 @XdocsPropertyType(PropertyType.TOKEN_ARRAY)
75 private BitSet target = TokenUtil.asBitSet(
76 TokenTypes.CLASS_DEF,
77 TokenTypes.INTERFACE_DEF,
78 TokenTypes.ENUM_DEF,
79 TokenTypes.METHOD_DEF,
80 TokenTypes.CTOR_DEF,
81 TokenTypes.VARIABLE_DEF,
82 TokenTypes.RECORD_DEF,
83 TokenTypes.COMPACT_CTOR_DEF
84 );
85
86
87
88
89
90
91
92
93 @PreserveOrder
94 private List<String> tagOrder = Arrays.asList(DEFAULT_ORDER);
95
96
97
98
99
100
101
102 public void setTarget(String... targets) {
103 target = TokenUtil.asBitSet(targets);
104 }
105
106
107
108
109
110
111
112 public void setTagOrder(String... orders) {
113 tagOrder = Arrays.asList(orders);
114 }
115
116 @Override
117 public int[] getDefaultJavadocTokens() {
118 return new int[] {
119 JavadocCommentsTokenTypes.JAVADOC_CONTENT,
120 };
121 }
122
123 @Override
124 public int[] getRequiredJavadocTokens() {
125 return getAcceptableJavadocTokens();
126 }
127
128 @Override
129 public void visitJavadocToken(DetailNode ast) {
130 final int parentType = getParentType(getBlockCommentAst());
131
132 if (target.get(parentType)) {
133 checkOrderInTagSection(ast);
134 }
135 }
136
137
138
139
140
141
142 private void checkOrderInTagSection(DetailNode javadoc) {
143 int maxIndexOfPreviousTag = 0;
144 DetailNode node = javadoc.getFirstChild();
145
146 while (node != null) {
147 if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_BLOCK_TAG) {
148 final String tagText = JavadocUtil.getTagName(node);
149 final int indexOfCurrentTag = tagOrder.indexOf("@" + tagText);
150
151 if (indexOfCurrentTag != -1) {
152 if (indexOfCurrentTag < maxIndexOfPreviousTag) {
153 log(node.getLineNumber(), MSG_KEY, tagOrder.toString());
154 }
155 else {
156 maxIndexOfPreviousTag = indexOfCurrentTag;
157 }
158 }
159 }
160 node = node.getNextSibling();
161 }
162 }
163
164
165
166
167
168
169
170 private static int getParentType(DetailAST commentBlock) {
171 final DetailAST parentNode = commentBlock.getParent();
172 int result = parentNode.getType();
173 if (result == TokenTypes.TYPE || result == TokenTypes.MODIFIERS) {
174 result = parentNode.getParent().getType();
175 }
176 else if (parentNode.getParent() != null
177 && parentNode.getParent().getType() == TokenTypes.MODIFIERS) {
178 result = parentNode.getParent().getParent().getType();
179 }
180 return result;
181 }
182
183 }