Search in sources :

Example 1 with IdValidatorHelper

use of org.androidannotations.helper.IdValidatorHelper in project androidannotations by androidannotations.

the class SharedPrefHandler method validate.

@Override
public void validate(Element element, ElementValidation validation) {
    super.validate(element, validation);
    TypeElement typeElement = (TypeElement) element;
    validatorHelper.isInterface(typeElement, validation);
    List<? extends Element> inheritedMembers = getProcessingEnvironment().getElementUtils().getAllMembers(typeElement);
    for (Element memberElement : inheritedMembers) {
        if (!memberElement.getEnclosingElement().asType().toString().equals("java.lang.Object")) {
            coreValidatorHelper.isPrefMethod(memberElement, validation);
            DefaultPrefInfo<?> info;
            IdValidatorHelper defaultAnnotationValidatorHelper = null;
            if (validation.isValid()) {
                info = DEFAULT_PREF_INFOS.get(((ExecutableElement) memberElement).getReturnType().toString());
                coreValidatorHelper.hasCorrectDefaultAnnotation((ExecutableElement) memberElement, validation);
                if (validation.isValid() && memberElement.getAnnotation(DefaultRes.class) != null) {
                    defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), DefaultRes.class.getName()));
                    defaultAnnotationValidatorHelper.resIdsExist(memberElement, info.resType, FallbackStrategy.USE_ELEMENT_NAME, validation);
                } else if (validation.isValid() && memberElement.getAnnotation(info.annotationClass) != null) {
                    defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), info.annotationClass.getName()));
                }
                if (validation.isValid() && defaultAnnotationValidatorHelper != null) {
                    defaultAnnotationValidatorHelper.annotationParameterIsOptionalValidResId(memberElement, IRClass.Res.STRING, "keyRes", validation);
                }
            }
        }
    }
    SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
    SharedPref.Scope scope = sharedPrefAnnotation.value();
    String name = sharedPrefAnnotation.name();
    boolean hasCustomName = !name.trim().isEmpty();
    EnumSet<SharedPref.Scope> allowedScopes = EnumSet.of(SharedPref.Scope.ACTIVITY, SharedPref.Scope.UNIQUE);
    if (hasCustomName && !allowedScopes.contains(scope)) {
        validation.addError("SharedPref#name() is only supported for Scope.ACTIVITY and Scope.UNIQUE.");
    }
}
Also used : IdValidatorHelper(org.androidannotations.helper.IdValidatorHelper) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) IdAnnotationHelper(org.androidannotations.helper.IdAnnotationHelper) DefaultRes(org.androidannotations.annotations.sharedpreferences.DefaultRes) SharedPref(org.androidannotations.annotations.sharedpreferences.SharedPref)

Aggregations

Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 DefaultRes (org.androidannotations.annotations.sharedpreferences.DefaultRes)1 DefaultString (org.androidannotations.annotations.sharedpreferences.DefaultString)1 SharedPref (org.androidannotations.annotations.sharedpreferences.SharedPref)1 IdAnnotationHelper (org.androidannotations.helper.IdAnnotationHelper)1 IdValidatorHelper (org.androidannotations.helper.IdValidatorHelper)1