|
29 | 29 | import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS; |
30 | 30 |
|
31 | 31 | import java.lang.annotation.Annotation; |
32 | | -import java.util.ArrayList; |
33 | | -import java.util.Arrays; |
34 | | -import java.util.Collection; |
35 | | -import java.util.List; |
36 | | -import java.util.Set; |
37 | | -import java.util.TreeSet; |
| 32 | +import java.util.*; |
38 | 33 |
|
39 | 34 | import javax.lang.model.element.AnnotationMirror; |
40 | 35 | import javax.lang.model.element.Element; |
|
51 | 46 | import javax.lang.model.util.ElementFilter; |
52 | 47 | import javax.lang.model.util.Elements; |
53 | 48 |
|
54 | | -import org.androidannotations.annotations.EActivity; |
55 | | -import org.androidannotations.annotations.EBean; |
56 | | -import org.androidannotations.annotations.EFragment; |
57 | | -import org.androidannotations.annotations.EIntentService; |
58 | | -import org.androidannotations.annotations.ServiceAction; |
59 | | -import org.androidannotations.annotations.Trace; |
60 | | -import org.androidannotations.annotations.ViewById; |
| 49 | +import org.androidannotations.annotations.*; |
61 | 50 | import org.androidannotations.annotations.rest.Delete; |
62 | 51 | import org.androidannotations.annotations.rest.Get; |
63 | 52 | import org.androidannotations.annotations.rest.Head; |
@@ -100,6 +89,11 @@ public class ValidatorHelper { |
100 | 89 |
|
101 | 90 | private static final Collection<Integer> VALID_LOG_LEVELS = Arrays.asList(LOG_VERBOSE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR); |
102 | 91 |
|
| 92 | + private static final List<Receiver.RegisterAt> VALID_ACTIVITY_REGISTER_AT = Arrays.asList(Receiver.RegisterAt.OnCreateOnDestroy, Receiver.RegisterAt.OnResumeOnPause, Receiver.RegisterAt.OnStartOnStop); |
| 93 | + private static final List<Receiver.RegisterAt> VALID_SERVICE_REGISTER_AT = Arrays.asList(Receiver.RegisterAt.OnCreateOnDestroy); |
| 94 | + private static final List<Receiver.RegisterAt> VALID_FRAGMENT_REGISTER_AT = Arrays.asList(Receiver.RegisterAt.OnCreateOnDestroy, Receiver.RegisterAt.OnResumeOnPause, Receiver.RegisterAt.OnStartOnStop, Receiver.RegisterAt.OnAttachOnDetach); |
| 95 | + |
| 96 | + |
103 | 97 | protected final TargetAnnotationHelper annotationHelper; |
104 | 98 |
|
105 | 99 | public final ValidatorParameterHelper param; |
@@ -196,6 +190,13 @@ public void enclosingElementHasEActivityOrEFragment(Element element, AnnotationE |
196 | 190 | hasOneOfClassAnnotations(element, enclosingElement, validatedElements, validAnnotations, valid); |
197 | 191 | } |
198 | 192 |
|
| 193 | + public void enclosingElementHasEActivityOrEFragmentOrEService(Element element, AnnotationElements validatedElements, IsValid valid) { |
| 194 | + Element enclosingElement = element.getEnclosingElement(); |
| 195 | + @SuppressWarnings("unchecked") |
| 196 | + List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EFragment.class, EService.class); |
| 197 | + hasOneOfClassAnnotations(element, enclosingElement, validatedElements, validAnnotations, valid); |
| 198 | + } |
| 199 | + |
199 | 200 | public void enclosingElementHasEFragment(Element element, AnnotationElements validatedElements, IsValid valid) { |
200 | 201 | Element enclosingElement = element.getEnclosingElement(); |
201 | 202 | hasClassAnnotation(element, enclosingElement, validatedElements, EFragment.class, valid); |
@@ -599,16 +600,21 @@ public void extendsOneOfTypes(Element element, List<String> typeQualifiedNames, |
599 | 600 | } |
600 | 601 |
|
601 | 602 | public void extendsType(Element element, String typeQualifiedName, IsValid valid) { |
| 603 | + if (!extendsType(element, typeQualifiedName)) { |
| 604 | + valid.invalidate(); |
| 605 | + annotationHelper.printAnnotationError(element, "%s can only be used on an element that extends " + typeQualifiedName); |
| 606 | + } |
| 607 | + } |
| 608 | + |
| 609 | + private boolean extendsType(Element element, String typeQualifiedName) { |
602 | 610 | TypeMirror elementType = element.asType(); |
603 | 611 |
|
604 | 612 | TypeElement typeElement = annotationHelper.typeElementFromQualifiedName(typeQualifiedName); |
605 | 613 | if (typeElement != null) { |
606 | 614 | TypeMirror expectedType = typeElement.asType(); |
607 | | - if (!annotationHelper.isSubtype(elementType, expectedType)) { |
608 | | - valid.invalidate(); |
609 | | - annotationHelper.printAnnotationError(element, "%s can only be used on an element that extends " + typeQualifiedName); |
610 | | - } |
| 615 | + return annotationHelper.isSubtype(elementType, expectedType); |
611 | 616 | } |
| 617 | + return false; |
612 | 618 | } |
613 | 619 |
|
614 | 620 | public void allowedType(Element element, IsValid valid, TypeMirror fieldTypeMirror, List<String> allowedTypes) { |
@@ -1325,11 +1331,42 @@ public void hasNotMultipleAnnotatedMethodWithSameName(Element element, IsValid v |
1325 | 1331 | String enclosedElementName = enclosedElement.getSimpleName().toString(); |
1326 | 1332 | if (actionNames.contains(enclosedElementName)) { |
1327 | 1333 | valid.invalidate(); |
1328 | | - annotationHelper.printError(enclosedElement, "The " + TargetAnnotationHelper.annotationName(ServiceAction.class) + " annotated method must have unique name even if the signature is not the same"); |
| 1334 | + annotationHelper.printError(enclosedElement, "The " + TargetAnnotationHelper.annotationName(annotation) + " annotated method must have unique name even if the signature is not the same"); |
1329 | 1335 | } else { |
1330 | 1336 | actionNames.add(enclosedElementName); |
1331 | 1337 | } |
1332 | 1338 | } |
1333 | 1339 | } |
1334 | 1340 |
|
| 1341 | + public void hasRightRegisterAtValueDependingOnEnclosingElement(Element element, IsValid valid) { |
| 1342 | + Element enclosingElement = element.getEnclosingElement(); |
| 1343 | + Receiver.RegisterAt registerAt = element.getAnnotation(Receiver.class).registerAt(); |
| 1344 | + |
| 1345 | + Map<String, List<Receiver.RegisterAt>> validRegisterAts = new HashMap<String, List<Receiver.RegisterAt>>(); |
| 1346 | + validRegisterAts.put(CanonicalNameConstants.ACTIVITY, VALID_ACTIVITY_REGISTER_AT); |
| 1347 | + validRegisterAts.put(CanonicalNameConstants.SERVICE, VALID_SERVICE_REGISTER_AT); |
| 1348 | + validRegisterAts.put(CanonicalNameConstants.FRAGMENT, VALID_FRAGMENT_REGISTER_AT); |
| 1349 | + |
| 1350 | + for (Map.Entry<String, List<Receiver.RegisterAt>> validRegisterAt : validRegisterAts.entrySet()) { |
| 1351 | + String enclosingType = validRegisterAt.getKey(); |
| 1352 | + Collection<Receiver.RegisterAt> validRegisterAtValues = validRegisterAt.getValue(); |
| 1353 | + if(extendsType(enclosingElement, enclosingType) && !validRegisterAtValues.contains(registerAt)) { |
| 1354 | + valid.invalidate(); |
| 1355 | + annotationHelper.printAnnotationError(element, "The parameter registerAt of @Receiver in "+enclosingType+" can only be one of the following values : "+validRegisterAtValues); |
| 1356 | + } |
| 1357 | + } |
| 1358 | + } |
| 1359 | + |
| 1360 | + public void hasSupportV4JarIfLocal(Element element, IsValid valid) { |
| 1361 | + boolean local = element.getAnnotation(Receiver.class).local(); |
| 1362 | + if (local) { |
| 1363 | + |
| 1364 | + Elements elementUtils = annotationHelper.getElementUtils(); |
| 1365 | + if (elementUtils.getTypeElement(CanonicalNameConstants.LOCAL_BROADCAST_MANAGER) == null) { |
| 1366 | + valid.invalidate(); |
| 1367 | + annotationHelper.printAnnotationError(element, "To use the LocalBroadcastManager, you MUST include the android-support-v4 jar"); |
| 1368 | + } |
| 1369 | + } |
| 1370 | + } |
| 1371 | + |
1335 | 1372 | } |
0 commit comments