2929
3030import java .lang .annotation .Annotation ;
3131import java .util .ArrayList ;
32- import java .util .Arrays ;
3332import java .util .Collection ;
33+ import java .util .Collections ;
3434import java .util .List ;
3535import java .util .Set ;
3636import java .util .TreeSet ;
@@ -66,7 +66,7 @@ public class ValidatorHelper {
6666
6767 private static final List <String > ANDROID_FRAGMENT_QUALIFIED_NAMES = asList (CanonicalNameConstants .FRAGMENT , CanonicalNameConstants .SUPPORT_V4_FRAGMENT );
6868
69- private static final Collection <Integer > VALID_LOG_LEVELS = Arrays . asList (LOG_VERBOSE , LOG_DEBUG , LOG_INFO , LOG_WARN , LOG_ERROR );
69+ private static final Collection <Integer > VALID_LOG_LEVELS = asList (LOG_VERBOSE , LOG_DEBUG , LOG_INFO , LOG_WARN , LOG_ERROR );
7070
7171 private static final List <String > VALID_PREFERENCE_CLASSES = asList (CanonicalNameConstants .PREFERENCE_ACTIVITY , CanonicalNameConstants .PREFERENCE_FRAGMENT ,
7272 CanonicalNameConstants .SUPPORT_V4_PREFERENCE_FRAGMENT , CanonicalNameConstants .MACHINARIUS_V4_PREFERENCE_FRAGMENT , CanonicalNameConstants .SUPPORT_V7_PREFERENCE_FRAGMENTCOMPAT ,
@@ -149,83 +149,98 @@ public void enclosingElementIsNotAbstractIfNotAbstract(Element element, ElementV
149149 }
150150 }
151151
152+ public void enclosingElementHasAnnotation (Class <? extends Annotation > annotation , Element element , ElementValidation validation ) {
153+ enclosingElementHasOneOfAnnotations (element , Collections .<Class <? extends Annotation >> singletonList (annotation ), validation );
154+ }
155+
152156 public void enclosingElementHasEBeanAnnotation (Element element , ElementValidation valid ) {
153- Element enclosingElement = element .getEnclosingElement ();
154- hasClassAnnotation (element , enclosingElement , EBean .class , valid );
157+ enclosingElementHasAnnotation (EBean .class , element , valid );
155158 }
156159
157160 public void enclosingElementHasEActivity (Element element , ElementValidation valid ) {
158- Element enclosingElement = element .getEnclosingElement ();
159- hasClassAnnotation (element , enclosingElement , EActivity .class , valid );
161+ enclosingElementHasAnnotation (EActivity .class , element , valid );
160162 }
161163
162164 public void enclosingElementHasEActivityOrEFragment (Element element , ElementValidation valid ) {
163- Element enclosingElement = element .getEnclosingElement ();
164165 List <Class <? extends Annotation >> validAnnotations = asList (EActivity .class , EFragment .class );
165- hasOneOfClassAnnotations (element , enclosingElement , validAnnotations , valid );
166+ enclosingElementHasOneOfAnnotations (element , validAnnotations , valid );
166167 }
167168
168169 public void enclosingElementHasEActivityOrEFragmentOrEServiceOrEIntentService (Element element , ElementValidation valid ) {
169- Element enclosingElement = element .getEnclosingElement ();
170170 List <Class <? extends Annotation >> validAnnotations = asList (EActivity .class , EFragment .class , EService .class , EIntentService .class );
171- hasOneOfClassAnnotations (element , enclosingElement , validAnnotations , valid );
171+ enclosingElementHasOneOfAnnotations (element , validAnnotations , valid );
172172 }
173173
174174 public void enclosingElementHasEFragment (Element element , ElementValidation valid ) {
175- Element enclosingElement = element .getEnclosingElement ();
176- hasClassAnnotation (element , enclosingElement , EFragment .class , valid );
175+ enclosingElementHasAnnotation (EFragment .class , element , valid );
177176 }
178177
179178 public void enclosingElementHasEIntentService (Element element , ElementValidation valid ) {
180- Element enclosingElement = element .getEnclosingElement ();
181- hasClassAnnotation (element , enclosingElement , EIntentService .class , valid );
179+ enclosingElementHasAnnotation (EIntentService .class , element , valid );
182180 }
183181
184182 public void enclosingElementHasEReceiver (Element element , ElementValidation valid ) {
185- Element enclosingElement = element .getEnclosingElement ();
186- hasClassAnnotation (element , enclosingElement , EReceiver .class , valid );
183+ enclosingElementHasAnnotation (EReceiver .class , element , valid );
187184 }
188185
189186 public void hasEActivity (Element element , ElementValidation valid ) {
190- hasClassAnnotation (element , element , EActivity .class , valid );
187+ hasAnnotation (element , element , EActivity .class , valid );
191188 }
192189
193190 public void hasEActivityOrEFragment (Element element , ElementValidation valid ) {
194191 List <Class <? extends Annotation >> validAnnotations = asList (EActivity .class , EFragment .class );
195- hasOneOfClassAnnotations (element , element , validAnnotations , valid );
192+ hasOneOfAnnotations (element , element , validAnnotations , valid );
196193 }
197194
198195 public void enclosingElementHasEnhancedViewSupportAnnotation (Element element , ElementValidation valid ) {
199- Element enclosingElement = element .getEnclosingElement ();
200- hasOneOfClassAnnotations (element , enclosingElement , VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS , valid );
196+ enclosingElementHasOneOfAnnotations (element , VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS , valid );
201197 }
202198
203199 public void enclosingElementHasEnhancedComponentAnnotation (Element element , ElementValidation valid ) {
204- Element enclosingElement = element .getEnclosingElement ();
205- hasOneOfClassAnnotations (element , enclosingElement , VALID_ENHANCED_COMPONENT_ANNOTATIONS , valid );
200+ enclosingElementHasOneOfAnnotations (element , VALID_ENHANCED_COMPONENT_ANNOTATIONS , valid );
206201 }
207202
208203 public void enclosingElementHasAndroidAnnotation (Element element , ElementValidation valid ) {
209- Element enclosingElement = element .getEnclosingElement ();
210- hasOneOfClassAnnotations (element , enclosingElement , environment ().getGeneratingAnnotations (), valid );
204+ enclosingElementHasOneOfAnnotations (element , environment ().getGeneratingAnnotations (), valid );
211205 }
212206
213- private void hasClassAnnotation (Element reportElement , Element element , Class <? extends Annotation > validAnnotation , ElementValidation valid ) {
207+ private void hasAnnotation (Element element , Element reportElement , Class <? extends Annotation > validAnnotation , ElementValidation valid ) {
214208 ArrayList <Class <? extends Annotation >> validAnnotations = new ArrayList <>();
215209 validAnnotations .add (validAnnotation );
216- hasOneOfClassAnnotations ( reportElement , element , validAnnotations , valid );
210+ hasOneOfAnnotations ( element , reportElement , validAnnotations , valid );
217211 }
218212
219- public void hasOneOfClassAnnotations (Element reportElement , Element element , List <Class <? extends Annotation >> validAnnotations , ElementValidation valid ) {
213+ public void enclosingElementHasOneOfAnnotations (Element element , List <Class <? extends Annotation >> validAnnotations , ElementValidation validation ) {
214+ hasOneOfAnnotations (element , element .getEnclosingElement (), validAnnotations , validation );
215+ }
216+
217+ private void hasOneOfAnnotations (Element reportElement , Element element , List <Class <? extends Annotation >> validAnnotations , ElementValidation validation ) {
218+ checkAnnotations (reportElement , element , validAnnotations , true , validation );
219+ }
220+
221+ public void doesNotHaveOneOfAnnotations (Element element , List <Class <? extends Annotation >> validAnnotations , ElementValidation validation ) {
222+ checkAnnotations (element , element , validAnnotations , false , validation );
223+ }
224+
225+ public void doesNotHaveAnnotation (Element element , Class <? extends Annotation > annotation , ElementValidation validation ) {
226+ doesNotHaveOneOfAnnotations (element , Collections .<Class <? extends Annotation >> singletonList (annotation ), validation );
227+ }
228+
229+ private void checkAnnotations (Element reportElement , Element element , List <Class <? extends Annotation >> validAnnotations , boolean shouldFind , ElementValidation validation ) {
230+
220231 boolean foundAnnotation = false ;
221232 for (Class <? extends Annotation > validAnnotation : validAnnotations ) {
222233 if (element .getAnnotation (validAnnotation ) != null ) {
223234 foundAnnotation = true ;
224235 break ;
225236 }
226237 }
227- if (!foundAnnotation ) {
228- valid .addError (reportElement , "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes (validAnnotations ) + "." );
238+
239+ if (shouldFind != foundAnnotation ) {
240+ String not = shouldFind ? "" : " not" ;
241+
242+ validation .addError (reportElement , "%s can only be used in a " + element .getKind ().toString ().toLowerCase () + not + " annotated with "
243+ + getFormattedValidEnhancedBeanAnnotationTypes (validAnnotations ) + "." );
229244 }
230245 }
231246
@@ -248,11 +263,6 @@ public void hasViewByIdAnnotation(Element element, ElementValidation valid) {
248263 elementHasAnnotation (ViewById .class , element , valid , error );
249264 }
250265
251- public void enclosingMethodHasAnnotation (Class <? extends Annotation > annotation , Element element , ElementValidation valid ) {
252- String error = "can only be used with a method annotated with" ;
253- enclosingElementHasAnnotation (annotation , element , valid , error );
254- }
255-
256266 public void enclosingElementHasAnnotation (Class <? extends Annotation > annotation , Element element , ElementValidation valid , String error ) {
257267 Element enclosingElement = element .getEnclosingElement ();
258268 elementHasAnnotation (annotation , enclosingElement , valid , error );
0 commit comments