private static boolean hasAnnotationWithName(Element element, String simpleName) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { String annotationName = mirror.getAnnotationType().asElement().getSimpleName().toString(); if (simpleName.equals(annotationName)) { return true; } } return false; }
private static @Nullable AnnotationMirror getMirror(Element element, Class<? extends Annotation> annotation) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString().equals(annotation.getCanonicalName())) { return annotationMirror; } } return null; }
private static <T> T findAnnotationValue(Element element, String annotationClass, String valueName, Class<T> expectedType) { T ret = null; for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { DeclaredType annotationType = annotationMirror.getAnnotationType(); TypeElement annotationElement = (TypeElement) annotationType .asElement(); if (annotationElement.getQualifiedName().contentEquals( annotationClass)) { ret = extractValue(annotationMirror, valueName, expectedType); break; } } return ret; } private static <T> T extractValue(AnnotationMirror annotationMirror, String valueName, Class<T> expectedType) { Map<ExecutableElement, AnnotationValue> elementValues = new HashMap<ExecutableElement, AnnotationValue>( annotationMirror.getElementValues()); for (Entry<ExecutableElement, AnnotationValue> entry : elementValues .entrySet()) { if (entry.getKey().getSimpleName().contentEquals(valueName)) { Object value = entry.getValue().getValue(); return expectedType.cast(value); } } return null; }
public AnnotationMirror getAnnotationMirror() { List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (annotationElement.getQualifiedName().toString().equals(annotationName)) { return annotationMirror; } } return null; }
public boolean isAnnotatedWith(Element element, String qualifiedName) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString().equals(qualifiedName)) { return true; } } return false; }
/** * Retrieves the {@code @Constraint} meta-annotation from the given * constraint annotation. * * @param annotationType A constraint type. * * @return The Constraint meta-annotation. * * @throws IllegalArgumentException If the given constraint annotation type isn't annotated with * the {@code @Constraint} meta-annotation. */ private AnnotationMirror getConstraintMetaAnnotation(DeclaredType annotationType) { List<? extends AnnotationMirror> annotationMirrors = annotationType.asElement().getAnnotationMirrors(); AnnotationMirror constraintMetaAnnotation = annotationApiHelper.getMirror( annotationMirrors, BeanValidationTypes.CONSTRAINT ); if ( constraintMetaAnnotation == null ) { throw new IllegalArgumentException( "Given type " + annotationType + " isn't a constraint annotation type." ); } return constraintMetaAnnotation; }
public AnnotationMirror findAnnotationMirror(Element annotatedElement, String annotationName) { List<? extends AnnotationMirror> annotationMirrors = annotatedElement.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (isAnnotation(annotationElement, annotationName)) { return annotationMirror; } } return null; }
private boolean elementHasAnnotationSafe(Class<? extends Annotation> annotation, Element element) { List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().toString().equals(annotation.getName())) { return true; } } return false; }
private Set<AnnotationMirror> getComposingConstraints(DeclaredType constraintAnnotationType) { Name key = getName( constraintAnnotationType ); Set<AnnotationMirror> composingConstraints = composingConstraintsByConstraints.get( key ); if ( composingConstraints != null ) { return composingConstraints; } composingConstraints = CollectionHelper.newHashSet(); List<? extends AnnotationMirror> annotationMirrors = constraintAnnotationType.asElement() .getAnnotationMirrors(); for ( AnnotationMirror oneAnnotationMirror : annotationMirrors ) { AnnotationType annotationType = getAnnotationType( oneAnnotationMirror ); if ( annotationType == AnnotationType.CONSTRAINT_ANNOTATION ) { composingConstraints.add( oneAnnotationMirror ); } else if ( annotationType == AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION ) { List<? extends AnnotationValue> value = annotationApiHelper.getAnnotationArrayValue( oneAnnotationMirror, "value" ); for ( AnnotationValue annotationValue : value ) { composingConstraints.add( (AnnotationMirror) annotationValue ); } } } composingConstraintsByConstraints.put( key, composingConstraints ); return composingConstraints; }
private void extractAnnotations(AnnotationElementsHolder extractedModel, Set<String> annotationTypesToCheck, TypeElement rootTypeElement, Element ancestorEnclosedElement) { List<? extends AnnotationMirror> ancestorEnclosedElementAnnotations = ancestorEnclosedElement.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : ancestorEnclosedElementAnnotations) { DeclaredType annotationType = annotationMirror.getAnnotationType(); if (annotationTypesToCheck.contains(annotationType.toString())) { TypeElement annotation = (TypeElement) annotationType.asElement(); /* * rootTypeElement is one of the types that are being compiled * * ancestorEnclosedElement is the annotated element in an ancestor of * rootTypeElement * * annotation is a type representing the annotation on ancestorEnclosedElement */ extractedModel.putAncestorAnnotatedElement(annotation.getQualifiedName().toString(), ancestorEnclosedElement, rootTypeElement); } } }
private static AnnotationMirror getAnnotationMirror(Element typeElement, Class<? extends Annotation> annotationClass) { String clazzName = annotationClass.getName(); for (AnnotationMirror m : typeElement.getAnnotationMirrors()) { if (m.getAnnotationType().toString().equals(clazzName)) { return m; } } return null; }
static Optional<? extends AnnotationMirror> findAnnotationMirror( Element element, String qualifiedName) { return element.getAnnotationMirrors().stream() .filter(mirror -> namesEqual((TypeElement) mirror.getAnnotationType().asElement(), qualifiedName)) .findFirst(); }
public static boolean containsAnnotation(Element element, String... annotations) { assert element != null; assert annotations != null; List<String> annotationClassNames = new ArrayList<String>(); Collections.addAll( annotationClassNames, annotations ); List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for ( AnnotationMirror mirror : annotationMirrors ) { if ( annotationClassNames.contains( mirror.getAnnotationType().toString() ) ) { return true; } } return false; }
public boolean hasAnnotationWithFqcn(Element element, String annotationClassNameName) { if (element != null) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror .getAnnotationType() .asElement() .toString() .equals(annotationClassNameName)) { return true; } } } return false; }
public static AnnotationMirror getAnnotationMirrorOfType(Element element, Class<? extends Annotation> annotation) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { if (mirror.getAnnotationType().toString().equals(annotation.getName())) { return mirror; } } return null; }
/** * Returns {@code true} if the an annotation is found on the given element with the given class * name (not fully qualified). */ public boolean hasAnnotationWithName(Element element, String simpleName) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { final Element annnotationElement = mirror.getAnnotationType().asElement(); String annotationName = annnotationElement.getSimpleName().toString(); if (simpleName.equals(annotationName)) { return true; } } return false; }
private AnnotationMirror getAnnotationMirror(Element element, Class annotationClass) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString() .equals(annotationClass.getCanonicalName())) { return annotationMirror; } } errorLogger.logError("Unable to get %s annotation on model %", annotationClass.getSimpleName(), element.getSimpleName()); return null; }
private static AnnotationMirror findAnnotationMirror(Element element, ProcessingEnvironment processingEnv, Class<? extends Annotation> annotation) { for (AnnotationMirror ann : element.getAnnotationMirrors()) { if (processingEnv.getElementUtils().getBinaryName((TypeElement) ann.getAnnotationType().asElement()). contentEquals(annotation.getName())) { return ann; } } return null; }
private static Map<String, Object> getAnnotation(Element element, String name) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { if (mirror.getAnnotationType().toString().equals(name)) { return annotationKeyValues(mirror); } } throw new IllegalArgumentException(String.format("%s has no annotation %s", element, name)); }
private @Nullable NullabilityAnnotationInfo isAccessorNullableAccessor(Element element) { for (AnnotationMirror annotation : element.getAnnotationMirrors()) { TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement(); Name simpleName = annotationElement.getSimpleName(); Name qualifiedName = annotationElement.getQualifiedName(); if (isNullableAnnotation(simpleName, qualifiedName)) { return ImmutableNullabilityAnnotationInfo.of(annotationElement); } } return null; }