public void validateInterceptors(Element element, ElementValidation valid) { TypeMirror clientHttpRequestInterceptorType = annotationHelper.typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_INTERCEPTOR).asType(); TypeMirror clientHttpRequestInterceptorTypeErased = annotationHelper.getTypeUtils().erasure(clientHttpRequestInterceptorType); List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element, annotationHelper.getTarget(), "interceptors"); if (interceptors == null) { return; TypeMirror erasedInterceptorType = annotationHelper.getTypeUtils().erasure(interceptorType); if (annotationHelper.isSubtype(erasedInterceptorType, clientHttpRequestInterceptorTypeErased)) { Element interceptorElement = interceptorType.asElement(); if (interceptorElement.getKind().isClass()) { if (!annotationHelper.isAbstract(interceptorElement)) { if (interceptorElement.getAnnotation(EBean.class) == null) { List<ExecutableElement> constructors = ElementFilter.constructorsIn(interceptorElement.getEnclosedElements()); boolean hasPublicWithNoArgumentConstructor = false; for (ExecutableElement constructor : constructors) { if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) { hasPublicWithNoArgumentConstructor = true;
public String annotationName() { return annotationName(annotationName); } }
public void validateRestSimpleParameter(Element element, String requiredClass, String parameterName, ElementValidation validation) { TypeMirror requiredType = annotationHelper.typeElementFromQualifiedName(requiredClass).asType(); DeclaredType paramterType = annotationHelper.extractAnnotationClassParameter(element, annotationHelper.getTarget(), parameterName); if (paramterType != null) { if (annotationHelper.isSubtype(paramterType, requiredType)) { Element parameterElement = paramterType.asElement(); if (parameterElement.getKind().isClass()) { if (!annotationHelper.isAbstract(parameterElement)) { if (parameterElement.getAnnotation(EBean.class) != null) { typeIsValid(EBean.class, paramterType, validation); return; } List<ExecutableElement> constructors = ElementFilter.constructorsIn(parameterElement.getEnclosedElements()); for (ExecutableElement constructor : constructors) { if (annotationHelper.isPublic(constructor) && constructor.getParameters().isEmpty()) { return; } } validation.addError(element, "The " + parameterName + " class must have a public no argument constructor or must be annotated with @EBean"); } else { validation.addError(element, "The " + parameterName + " class must not be abstract"); } } else { validation.addError(element, "The " + parameterName + " class must be a class"); } } else { validation.addError(element, "The " + parameterName + " class must be a subtype of " + requiredClass); } } }
@Override public boolean isSatisfied(VariableElement param) { TypeMirror elementType = param.asType(); TypeElement typeElement = annotationHelper.typeElementFromQualifiedName(typeName); if (typeElement != null) { TypeMirror expectedType = typeElement.asType(); return annotationHelper.isSubtype(elementType, expectedType); } return false; }
private void createDaoParametrizedTypes() { daoTypeElement = helper.typeElementFromQualifiedName(OrmLiteClasses.DAO); runtimeExceptionDaoTypeElement = helper.typeElementFromQualifiedName(OrmLiteClasses.RUNTIME_EXCEPTION_DAO); Types typeUtils = helper.getTypeUtils(); TypeMirror wildcardType = typeUtils.getWildcardType(null, null); daoParametrizedType = helper.getTypeUtils().getDeclaredType(daoTypeElement, wildcardType, wildcardType); runtimeExceptionDaoParametrizedType = helper.getTypeUtils().getDeclaredType(runtimeExceptionDaoTypeElement, wildcardType, wildcardType); } }
public void childFragmentUsedOnlyIfEnclosingClassIsFragment(Element element, ElementValidation validation) { boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment"); if (childFragment) { TypeElement fragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.FRAGMENT); TypeElement supportFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.SUPPORT_V4_FRAGMENT); TypeElement androidxFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.ANDROIDX_FRAGMENT); boolean enclosingElementIsFragment = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); if (fragment != null && annotationHelper.isSubtype(enclosingElement, fragment)) { enclosingElementIsFragment = true; } else if (supportFragment != null && annotationHelper.isSubtype(enclosingElement, supportFragment)) { enclosingElementIsFragment = true; } else if (androidxFragment != null && annotationHelper.isSubtype(enclosingElement, androidxFragment)) { enclosingElementIsFragment = true; } if (!enclosingElementIsFragment) { validation.addError(element, "The 'childFragmentManager' parameter only can be used if the class containing the annotated field is either subclass of " + CanonicalNameConstants.FRAGMENT + ", " + CanonicalNameConstants.SUPPORT_V4_FRAGMENT + " or " + CanonicalNameConstants.ANDROIDX_FRAGMENT); } } }
public void restInterfaceHasFormConverter(Element element, ElementValidation validation) { Element restInterface = element.getEnclosingElement().getEnclosingElement(); if (restInterface.getAnnotation(Rest.class) == null) { return; } List<DeclaredType> converters = annotationHelper.extractAnnotationClassArrayParameter(restInterface, Rest.class.getCanonicalName(), "converters"); boolean formConverterFound = false; TypeMirror formConverter = annotationHelper.getElementUtils().getTypeElement(FORM_HTTP_MESSAGE_CONVERTER).asType(); for (DeclaredType converter : converters) { if (formConverter != null && annotationHelper.isSubtype(converter, formConverter)) { formConverterFound = true; break; } } if (!formConverterFound) { validation.addError(element, "%s annotated method parameter must be in a @Rest annotated interface which uses at least one " // + FORM_HTTP_MESSAGE_CONVERTER + " (or subtype)"); } }
public void typeHasAnnotation(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation valid) { Element typeElement = annotationHelper.getTypeUtils().asElement(elementType); if (!elementHasAnnotationSafe(annotation, typeElement)) { valid.addError("%s can only be used on an element annotated with @" + annotation.getName()); } }
public void typeOrTargetValueHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) { Element targetElement = findTargetElement(element, valid); if (targetElement == null) { return; } DeclaredType targetAnnotationClassValue = annotationHelper.extractAnnotationClassParameter(element); if (targetAnnotationClassValue != null) { targetElement = targetAnnotationClassValue.asElement(); if (!annotationHelper.getTypeUtils().isAssignable(targetAnnotationClassValue, targetElement.asType())) { valid.addError("The value of %s must be assignable into the annotated field"); } } typeHasValidAnnotation(annotation, targetElement, valid); }
public void extendsMenuItem(Element element, ElementValidation valid) { Element enclosingElement = element.getEnclosingElement(); String enclosingQualifiedName = enclosingElement.asType().toString(); TypeElement enclosingTypeElement = annotationHelper.typeElementFromQualifiedName(enclosingQualifiedName); if (enclosingTypeElement != null) { extendsType(element, CanonicalNameConstants.MENU_ITEM, valid); } }
public void isAbstractOrHasEmptyConstructor(Element element, ElementValidation valid) { List<ExecutableElement> constructors = ElementFilter.constructorsIn(element.getEnclosedElements()); if (!annotationHelper.isAbstract(element)) { if (constructors.size() == 1) { ExecutableElement constructor = constructors.get(0); if (!annotationHelper.isPrivate(constructor)) { if (constructor.getParameters().size() != 0) { valid.addError("%s annotated element should have an empty constructor"); } } else { valid.addError("%s annotated element should not have a private constructor"); } } else { valid.addError("%s annotated element should have only one constructor"); } } }
public void uniqueResourceId(Element element, Res resourceType, ElementValidation valid) { if (valid.isValid()) { List<String> annotationQualifiedIds = idAnnotationHelper.extractAnnotationResources(element, resourceType, true); Element elementEnclosingElement = element.getEnclosingElement(); Set<? extends Element> annotatedElements = validatedModel().getRootAnnotatedElements(annotationHelper.getTarget()); for (Element uniqueCheckElement : annotatedElements) { Element uniqueCheckEnclosingElement = uniqueCheckElement.getEnclosingElement(); if (elementEnclosingElement.equals(uniqueCheckEnclosingElement)) { List<String> checkQualifiedIds = idAnnotationHelper.extractAnnotationResources(uniqueCheckElement, resourceType, true); for (String checkQualifiedId : checkQualifiedIds) { for (String annotationQualifiedId : annotationQualifiedIds) { if (annotationQualifiedId.equals(checkQualifiedId)) { String annotationSimpleId = annotationQualifiedId.substring(annotationQualifiedId.lastIndexOf('.') + 1); valid.addError("The resource id " + annotationSimpleId + " is already used on the following " + annotationHelper.annotationName() + " method: " + uniqueCheckElement); return; } } } } } } }
public boolean isClassPresent(String className) { return annotationHelper.getElementUtils().getTypeElement(className) != null; }
public void hasNotMultipleAnnotatedMethodWithSameName(Element element, ElementValidation valid, Class<? extends Annotation> annotation) { Set<String> actionNames = new TreeSet<>(); List<? extends Element> enclosedElements = element.getEnclosedElements(); for (Element enclosedElement : enclosedElements) { if (enclosedElement.getKind() != ElementKind.METHOD || !annotationHelper.hasOneOfClassAnnotations(enclosedElement, annotation)) { continue; } String enclosedElementName = enclosedElement.getSimpleName().toString(); if (actionNames.contains(enclosedElementName)) { valid.addError(enclosedElement, "The " + TargetAnnotationHelper.annotationName(annotation) + " annotated method must have unique name even if the signature is not the same"); } else { actionNames.add(enclosedElementName); } } }
public void enclosingElementIsNotAbstractIfNotAbstract(Element element, ElementValidation validation) { if (!annotationHelper.isAbstract(element) && annotationHelper.isAbstract(element.getEnclosingElement())) { validation.addError("%s cannot be used on a non-abstract inner element whose outer element is abstract"); } }
public void isPublic(Element element, ElementValidation valid) { if (!annotationHelper.isPublic(element)) { valid.addError(element, "%s cannot be used on a non public element"); } }
public DeclaredType extractAnnotationClassParameter(Element element) { return extractAnnotationClassParameter(element, annotationName); }
protected boolean extendsType(Element element, String typeQualifiedName) { TypeMirror elementType = element.asType(); TypeElement typeElement = annotationHelper.typeElementFromQualifiedName(typeQualifiedName); if (typeElement != null) { TypeMirror expectedType = typeElement.asType(); return annotationHelper.isSubtype(elementType, expectedType); } return false; }
public void getChildFragmentManagerMethodIsAvailable(Element element, ElementValidation validation) { boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment"); if (childFragment) { TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); TypeElement fragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.FRAGMENT); TypeElement supportFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.SUPPORT_V4_FRAGMENT); TypeElement androidxFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.ANDROIDX_FRAGMENT); if (supportFragment != null && annotationHelper.isSubtype(enclosingElement, supportFragment)) { if (!methodIsAvailableIn(supportFragment, "getChildFragmentManager")) { validation.addError(element, "The 'childFragmentManager' parameter only can be used if the getChildFragmentManager() method is available in " + CanonicalNameConstants.SUPPORT_V4_FRAGMENT + ", update your support library version."); } } else if (androidxFragment != null && annotationHelper.isSubtype(enclosingElement, androidxFragment)) { if (!methodIsAvailableIn(androidxFragment, "getChildFragmentManager")) { validation.addError(element, "The 'childFragmentManager' parameter only can be used if the getChildFragmentManager() method is available in " + CanonicalNameConstants.ANDROIDX_FRAGMENT + ", update your support library version."); } } else if (fragment != null && annotationHelper.isSubtype(enclosingElement, fragment) && environment().getAndroidManifest().getMinSdkVersion() < 17) { validation.addError(element, "The 'childFragmentManager' parameter only can be used if the getChildFragmentManager() method is available in " + CanonicalNameConstants.FRAGMENT + " (from API 17). Increment 'minSdkVersion' or use " + CanonicalNameConstants.SUPPORT_V4_FRAGMENT + " or " + CanonicalNameConstants.ANDROIDX_FRAGMENT + "."); } } }