forked from sevntu-checkstyle/sevntu.checkstyle
/
RequiredParameterForAnnotationCheck.java
209 lines (189 loc) · 6.74 KB
/
RequiredParameterForAnnotationCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2011 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.github.sevntu.checkstyle.checks.annotation;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import java.util.Set;
import java.util.TreeSet;
/**
* <p>
* Marks a given parameter as required for a given annotation. This only certify
* the minimal parameters that need to be used on the annotation.
* </p>
* <p>
* Parameters:<br>
* <b>annotationName</b> - The name of the target annotation where we are enforcing the
* parameter.<br>
* <b>requiredParameters</b> - Set of parameter names that are required on the target
* annotation. Names can be specified on any order in target annotation.
* </p>
* <p>
* <b>Example 1.</b><br>
* Configuration:
* </p>
* <pre>
* <module name="RequiredParameterForAnnotation">
* <property name="annotationName" value="TheAnnotation"/>
* <property name="requiredParameters" value="ThePropertyName1"/>
* </module>
* </pre>
* <p>
* Result:
* </p>
* <pre>
* <code>
* {@literal @}TheAnnotation() //Violation. ThePropertyName1 missing.
* someMethod() {}
*
* {@literal @}TheAnnotation(ThePropertyName2=2) //Violation. ThePropertyName1 missing.
* class SomeClass {}
*
* {@literal @}TheAnnotation(ThePropertyName1=1) //Correct.
* class SomeClass {}
*
* {@literal @}TheAnnotation(ThePropertyName2=2, ThePropertyName3=3, ThePropertyName1=1) //Correct.
* class SomeClass {}
* </code>
* </pre>
<p>
* <b>Example 2.</b><br>
* Configuration:
* </p>
* <pre>
* <module name="RequiredParameterForAnnotation">
* <property name="annotationName" value="TheAnnotation"/>
* <property name="requiredParameters" value="ThePropertyName1,ThePropertyName2,ThePropertyName3"/>
* </module>
* </pre>
* <p>
* Result:
* </p>
* <pre>
* <code>
* {@literal @}TheAnnotation() //Violation. ThePropertyName1, ThePropertyName2, ThePropertyName3 missing.
* someMethod() {}
*
* {@literal @}TheAnnotation(ThePropertyName2=2) //Violation. ThePropertyName1, ThePropertyName3 missing.
* class SomeClass {}
*
* {@literal @}TheAnnotation(ThePropertyName3=3, ThePropertyName2=2, ThePropertyName1=1) //Correct.
* class SomeClass {}
* </code>
* </pre>
*
* @author <a href="mailto:andrew.uljanenko@gmail.com">Andrew Uljanenko</a>
*/
public class RequiredParameterForAnnotationCheck extends Check
{
/**
* Key for error message.
*/
public static final String MSG_KEY = "annotation.missing.required.parameter";
/**
* The annotation name we are interested in.
*/
private String annotationName;
/**
* Parameters that should be in annotation.
*/
private Set<String> requiredParameters = new TreeSet<String>();
/**
* The annotation name we are interested in.
*/
public void setAnnotationName(String annotationName)
{
this.annotationName = annotationName;
}
/**
* The required list of parameters we have to use.
*/
public void setRequiredParameters(String[] requiredPropertiesParameter)
{
for (String item : requiredPropertiesParameter) {
this.requiredParameters.add(item);
}
}
@Override
public int[] getDefaultTokens()
{
return new int[] { TokenTypes.ANNOTATION };
}
@Override
public int[] getRequiredTokens()
{
return new int[] { TokenTypes.ANNOTATION };
};
@Override
public void visitToken(DetailAST annotationNode)
{
String annotationName = getAnnotationName(annotationNode);
if (annotationName.equals(this.annotationName)) {
Set<String> missingParameters =
Sets.difference(requiredParameters, getAnnotationParameters(annotationNode));
if (!missingParameters.isEmpty()) {
String missingParametersAsString = Joiner.on(", ").join(missingParameters);
log(annotationNode, MSG_KEY, this.annotationName, missingParametersAsString);
}
}
}
/**
* Returns full name of an annotation.
* @param aAnnotation
* @return name of an annotation.
*/
private static String getAnnotationName(DetailAST annotationNode)
{
DetailAST identNode = annotationNode.findFirstToken(TokenTypes.IDENT);
String result;
if (identNode != null) {
result = identNode.getText();
}
else {
StringBuilder builder = new StringBuilder();
DetailAST separationDotNode = annotationNode.findFirstToken(TokenTypes.DOT);
while (separationDotNode.getType() == TokenTypes.DOT) {
builder.insert(0, '.').insert(1, separationDotNode.getLastChild().getText());
separationDotNode = separationDotNode.getFirstChild();
}
builder.insert(0, separationDotNode.getText());
result = builder.toString();
}
return result;
}
/**
* Returns the name of annotations properties.
* @param aAnnotation
* @return name of annotation properties.
*/
private static Set<String> getAnnotationParameters(DetailAST annotationNode)
{
Set<String> parametersList = new TreeSet<String>();
DetailAST annotationChildNode = annotationNode.getFirstChild();
while (annotationChildNode != null) {
if (annotationChildNode.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) {
parametersList.add(annotationChildNode.getFirstChild().getText());
}
annotationChildNode = annotationChildNode.getNextSibling();
}
return parametersList;
}
}