public static boolean isAnnotationPresent( List<? extends AnnotationMirror> annotationMirrors, Class<? extends Annotation> annotationType) { return findAnnotation(annotationMirrors, annotationType) != null; }
private ImmutableList<TypeMirror> extractDeclaredImports(Element element) { return AnnotationMirrors.getTypesFromMirrors( types, importType, "value", element.getAnnotationMirrors()); } }
public static ImmutableList<TypeMirror> getTypesFromMirrors( String annotationQualifiedName, String annotationAttributeName, List<? extends AnnotationMirror> annotationMirrors) { ImmutableList.Builder<TypeMirror> builder = ImmutableList.builder(); for (AnnotationMirror annotationMirror : annotationMirrors) { TypeElement element = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (element.getQualifiedName().contentEquals(annotationQualifiedName)) { collectTypesFromAnnotationAttribute(annotationAttributeName, builder, annotationMirror); } } return builder.build(); }
AnnotationMirrors.findAnnotation(element.getAnnotationMirrors(), info.annotationType); annotationCode.put( info.deduplicationKey, AnnotationMirrors.toCharSequence(presentAnnotation).toString());
private Iterable<String> annotationsFrom(Element element) { List<String> annotations = new ArrayList<>(); for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { String a = AnnotationMirrors.toCharSequence(annotationMirror).toString(); if (!a.startsWith(ENCODE_PACKAGE_PREFIX, 1)) { annotations.add(a); } } return annotations; }
private void insertTypeAnnotationsIfPresent(TypeMirror type, int typeStart, int typeEnd) { List<? extends AnnotationMirror> annotations = AnnotationMirrors.from(type); if (!annotations.isEmpty()) { StringBuilder annotationBuffer = typeAnnotationsToBuffer(annotations, false); int insertionIndex = typeStart + buffer.substring(typeStart, typeEnd).lastIndexOf('.') + 1; buffer.insert(insertionIndex, annotationBuffer); } }
private StringBuilder typeAnnotationsToBuffer(List<? extends AnnotationMirror> annotations, boolean nestedTypeUse) { StringBuilder annotationBuffer = new StringBuilder(100); for (AnnotationMirror annotationMirror : annotations) { boolean canBeAppliedToMethodAsWell = !nestedTypeUse // just to short circuit computation early && Annotations.annotationMatchesTarget(annotationMirror.getAnnotationType().asElement(), ElementType.METHOD); if (canBeAppliedToMethodAsWell) { // skip this type annotation on top type continue; } CharSequence sequence = AnnotationMirrors.toCharSequence(annotationMirror, importsResolver); if (!nullableTypeAnnotation && sequence.toString().endsWith(EPHEMERAL_ANNOTATION_NULLABLE)) { this.nullableTypeAnnotation = true; } annotationBuffer .append(sequence) .append(' '); } return annotationBuffer; }
private void typeAnnotationHandle(TypeMirror argument) { if (!processNestedTypeUseAnnotations) { return; } List<? extends AnnotationMirror> annotations = AnnotationMirrors.from(argument); if (!annotations.isEmpty()) { String typeAnnotations = typeAnnotationsToBuffer(annotations, true).toString(); assignElementNullness(typeAnnotations); } }
private Set<String> extractServiceInterfaceNames(TypeElement typeElement) { ImmutableList<TypeMirror> typesMirrors = AnnotationMirrors.getTypesFromMirrors( Metainf.Service.class.getCanonicalName(), "value", typeElement.getAnnotationMirrors()); if (typesMirrors.isEmpty()) { return useIntrospectedInterfacesForServices(typeElement); } return useProvidedTypesForServices(typeElement, typesMirrors); }
@Nullable public static AnnotationMirror findAnnotation( List<? extends AnnotationMirror> annotationMirrors, Class<? extends Annotation> annotationType) { String annotationTypeName = annotationType.getCanonicalName(); return findAnnotation(annotationMirrors, annotationTypeName); }
private ImmutableMap<String, String> extractPlaceholderValues( AnnotationMirror annotation, Environment environment) { ImmutableMap.Builder<String, String> literals = ImmutableMap.builder(); for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : environment.processing() .getElementUtils() .getElementValuesWithDefaults(CachingElements.getDelegate(annotation)) .entrySet()) { String key = e.getKey().getSimpleName().toString(); String value = AnnotationMirrors.toCharSequence(e.getValue()).toString(); literals.put(key, value); } return literals.build(); }
void process() { if (startType.getKind().isPrimitive()) { // taking a shortcut for primitives String typeName = Ascii.toLowerCase(startType.getKind().name()); this.rawTypeName = typeName; this.returnTypeName = typeName; List<? extends AnnotationMirror> annotations = AnnotationMirrors.from(startType); if (!annotations.isEmpty()) { returnTypeName = typeAnnotationsToBuffer(annotations, false).append(typeName).toString(); } } else { this.buffer = new StringBuilder(100); caseType(startType); if (workaroundTypeString != null) { // to not mix the mess, we just replace buffer with workaround produced type string this.buffer = new StringBuilder(workaroundTypeString); } // It seems that array type annotations are not exposed in javac // Nested type argument's type annotations are not exposed as well (in javac) // So currently we instert only for top level, declared type (here), // and primitives (see above) TypeKind k = startType.getKind(); if (k == TypeKind.DECLARED || k == TypeKind.ERROR) { insertTypeAnnotationsIfPresent(startType, 0, rawTypeName.length()); } this.returnTypeName = buffer.toString(); } }
public static ImmutableList<TypeMirror> getTypesFromMirrors( Types types, TypeMirror annotationType, String annotationAttributeName, List<? extends AnnotationMirror> annotationMirrors) { ImmutableList.Builder<TypeMirror> builder = ImmutableList.builder(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (types.isSameType(annotationMirror.getAnnotationType(), annotationType)) { collectTypesFromAnnotationAttribute(annotationAttributeName, builder, annotationMirror); } } return builder.build(); }
private ImmutableList<TypeMirror> extractDeclaredImports(Element element) { return AnnotationMirrors.getTypesFromMirrors( types, importType, "value", element.getAnnotationMirrors()); } }
private Set<String> useProvidedTypesForServices(TypeElement typeElement, ImmutableList<TypeMirror> typesMirrors) { List<String> wrongTypes = Lists.newArrayList(); List<String> types = Lists.newArrayList(); for (TypeMirror typeMirror : typesMirrors) { if (typeMirror.getKind() != TypeKind.DECLARED || !processing().getTypeUtils().isAssignable(typeElement.asType(), typeMirror)) { wrongTypes.add(typeMirror.toString()); } else { types.add(typeMirror.toString()); } } if (!wrongTypes.isEmpty()) { processing().getMessager().printMessage( Diagnostic.Kind.ERROR, "@Metainf.Service(value = {...}) contains types that are not implemented by " + typeElement.getSimpleName() + ": " + wrongTypes, typeElement, AnnotationMirrors.findAnnotation(typeElement.getAnnotationMirrors(), Metainf.Service.class)); } return FluentIterable.from(types).toSet(); }
static List<CharSequence> getAnnotationLines( Element element, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, ElementType elementType, Function<String, String> importsResolver, @Nullable NullabilityAnnotationInfo nullability) { List<CharSequence> lines = Lists.newArrayList(); Set<String> seenAnnotations = new HashSet<>(); for (AnnotationMirror annotation : element.getAnnotationMirrors()) { TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement(); if (annotationTypeMatches(element, annotationElement, includeAnnotations, includeAllAnnotations, includeJacksonAnnotations, seenAnnotations, lines, importsResolver, elementType, nullability) && annotationMatchesTarget(annotationElement, elementType)) { lines.add(AnnotationMirrors.toCharSequence(annotation, importsResolver)); } } return lines; }
public List<CharSequence> getJsonQualiferAnnotations() { if (jsonQualifierAnnotations == null) { List<CharSequence> annotationStrings = Collections.emptyList(); for (AnnotationMirror m : element.getAnnotationMirrors()) { if (MetaAnnotated.from(m, protoclass().environment()).isJsonQualifier()) { if (annotationStrings.isEmpty()) { annotationStrings = Lists.newArrayList(); } annotationStrings.add(AnnotationMirrors.toCharSequence(m)); } } jsonQualifierAnnotations = annotationStrings; } return jsonQualifierAnnotations; }
lines.add(AnnotationMirrors.toCharSequence(metaAnnotation, importsResolver));