Skip to content

Commit 345f325

Browse files
committed
Refactor ValidatorHelper.hasSomeAnnotation methods
1 parent 4dc1f18 commit 345f325

3 files changed

Lines changed: 29 additions & 35 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
import java.lang.annotation.Annotation;
3131
import java.util.ArrayList;
32-
import java.util.Arrays;
3332
import java.util.Collection;
33+
import java.util.Collections;
3434
import java.util.List;
3535
import java.util.Set;
3636
import 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,74 +149,72 @@ 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) {
220218
boolean foundAnnotation = false;
221219
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
222220
if (element.getAnnotation(validAnnotation) != null) {
@@ -225,7 +223,8 @@ public void hasOneOfClassAnnotations(Element reportElement, Element element, Lis
225223
}
226224
}
227225
if (!foundAnnotation) {
228-
valid.addError(reportElement, "%s can only be used in a class annotated with " + getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
226+
validation.addError(reportElement,
227+
"%s can only be used in a " + element.getKind().toString().toLowerCase() + " annotated with " + getFormattedValidEnhancedBeanAnnotationTypes(validAnnotations) + ".");
229228
}
230229
}
231230

@@ -248,11 +247,6 @@ public void hasViewByIdAnnotation(Element element, ElementValidation valid) {
248247
elementHasAnnotation(ViewById.class, element, valid, error);
249248
}
250249

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-
256250
public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid, String error) {
257251
Element enclosingElement = element.getEnclosingElement();
258252
elementHasAnnotation(annotation, enclosingElement, valid, error);

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/ExtraParameterHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ExtraParameterHandler(Class<? extends Annotation> targetClass, Class<? ex
5151

5252
@Override
5353
protected void validate(Element element, ElementValidation valid) {
54-
validatorHelper.enclosingMethodHasAnnotation(methodAnnotationClass, element, valid);
54+
validatorHelper.enclosingElementHasAnnotation(methodAnnotationClass, element, valid);
5555

5656
validatorHelper.canBePutInABundle(element, valid);
5757
}

‎AndroidAnnotations/androidannotations-rest-spring/rest-spring/src/main/java/org/androidannotations/rest/spring/handler/PostHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public FieldHandler(AndroidAnnotationsEnvironment environment) {
6767

6868
@Override
6969
protected void validate(Element element, ElementValidation validation) {
70-
validatorHelper.enclosingMethodHasAnnotation(Post.class, element, validation);
70+
validatorHelper.enclosingElementHasAnnotation(Post.class, element, validation);
7171
}
7272

7373
@Override

0 commit comments

Comments
 (0)