/
ConstraintAnnotationVisitor.java
222 lines (197 loc) · 6.62 KB
/
ConstraintAnnotationVisitor.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
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.internal;
import java.util.List;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic.Kind;
import org.hibernate.validator.ap.internal.checks.ConstraintCheckFactory;
import org.hibernate.validator.ap.internal.checks.ConstraintCheckIssue;
import org.hibernate.validator.ap.internal.checks.ConstraintChecks;
import org.hibernate.validator.ap.internal.util.AnnotationApiHelper;
import org.hibernate.validator.ap.internal.util.Configuration;
import org.hibernate.validator.ap.internal.util.ConstraintHelper;
import org.hibernate.validator.ap.internal.util.MessagerAdapter;
/**
* An {@link javax.lang.model.element.ElementVisitor} that visits annotated elements (type declarations,
* methods and fields) and applies different {@link org.hibernate.validator.ap.internal.checks.ConstraintCheck}s to them.
* Each {@link ConstraintCheckIssue} occurred will be reported using the
* {@link javax.annotation.processing.Messager} API.
*
* @author Gunnar Morling
*/
public final class ConstraintAnnotationVisitor extends AbstractElementVisitor<Void, List<AnnotationMirror>> {
private final ConstraintCheckFactory constraintCheckFactory;
public ConstraintAnnotationVisitor(
ProcessingEnvironment processingEnvironment, MessagerAdapter messager, Configuration configuration) {
super( messager, configuration );
AnnotationApiHelper annotationApiHelper = new AnnotationApiHelper(
processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils()
);
ConstraintHelper constraintHelper = new ConstraintHelper(
processingEnvironment.getTypeUtils(), annotationApiHelper
);
constraintCheckFactory = new ConstraintCheckFactory(
processingEnvironment.getTypeUtils(),
processingEnvironment.getElementUtils(),
constraintHelper,
annotationApiHelper,
configuration.methodConstraintsSupported()
);
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* method. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotations may only be given at non-static, JavaBeans getter
* methods which's return type is supported by the constraints.</li>
* <li>
* The {@code @Valid} annotation may only be given at non-static,
* non-primitive JavaBeans getter methods.</li>
* </ul>
*/
@Override
public Void visitExecutableAsMethod(
ExecutableElement method,
List<AnnotationMirror> mirrors) {
checkConstraints( method, mirrors );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* field. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotations may only be given at non-static fields which's
* type is supported by the constraints.</li>
* <li>
* The {@code @Valid} annotation may only be given at non-static,
* non-primitive fields.</li>
* </ul>
*/
@Override
public Void visitVariableAsField(VariableElement annotatedField, List<AnnotationMirror> mirrors) {
checkConstraints( annotatedField, mirrors );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* method parameter. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotation parameter values are meaningful and valid.
* </li>
* </ul>
*/
@Override
public Void visitVariableAsParameter(VariableElement annotatedField, List<AnnotationMirror> mirrors) {
checkConstraints( annotatedField, mirrors );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* annotation type declaration. The following checks are performed:
* </p>
* <ul>
* <li>
* The only annotation types allowed to be annotated with other constraint
* annotations are composed constraint annotation type declarations.</li>
* </ul>
*/
@Override
public Void visitTypeAsAnnotationType(
TypeElement annotationType,
List<AnnotationMirror> mirrors) {
checkConstraints( annotationType, mirrors );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* class type declaration. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotations may at types supported by the constraints.</li>
* <li>
* </ul>
*/
@Override
public Void visitTypeAsClass(TypeElement e, List<AnnotationMirror> p) {
checkConstraints( e, p );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* enum type declaration. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotations may at types supported by the constraints.</li>
* <li>
* </ul>
*/
@Override
public Void visitTypeAsEnum(TypeElement e, List<AnnotationMirror> p) {
checkConstraints( e, p );
return null;
}
/**
* <p>
* Checks whether the given annotations are correctly specified at the given
* interface type declaration. The following checks are performed:
* </p>
* <ul>
* <li>
* Constraint annotations may at types supported by the constraints.</li>
* <li>
* </ul>
*/
@Override
public Void visitTypeAsInterface(TypeElement e, List<AnnotationMirror> p) {
checkConstraints( e, p );
return null;
}
/**
* Retrieves the checks required for the given element and annotations,
* executes them and reports all occurred errors.
*
* @param annotatedElement The element to check.
* @param mirrors The annotations to check.
*/
private void checkConstraints(Element annotatedElement, List<AnnotationMirror> mirrors) {
for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
try {
ConstraintChecks constraintChecks = constraintCheckFactory.getConstraintChecks(
annotatedElement, oneAnnotationMirror
);
reportIssues( constraintChecks.execute( annotatedElement, oneAnnotationMirror ) );
}
//HV-293: if single constraints can't be properly checked, report this and
//proceed with next constraints
catch (Exception e) {
if ( verbose ) {
messager.getDelegate()
.printMessage( Kind.NOTE, e.getMessage() != null ? e.getMessage() : e.toString(), annotatedElement, oneAnnotationMirror );
}
}
}
}
}