@Override public Boolean visitType( TypeElement element, Void aVoid ) { return element.getQualifiedName().contentEquals( annotationType.getName() ); } }
@Nullable public static AnnotationMirror findAnnotation( List<? extends AnnotationMirror> annotationMirrors, String annotationTypeName) { for (AnnotationMirror annotation : annotationMirrors) { if (((TypeElement) annotation.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(annotationTypeName)) { return annotation; } } return null; }
private boolean isOptionalType(DeclaredType parametrizedType) { return parametrizedType.asElement().getSimpleName().contentEquals(OPTIONAL_TYPE_SIMPLE_NAME) && parametrizedType.getTypeArguments().size() == 1; } }
private static boolean find(String interfaceName, TypeElement typeElement, Set<Element> visited) { if (visited.contains(typeElement)) { return false; } visited.add(typeElement); //logger.finest("trying to find " + interfaceName + " in " + typeElement); if (typeElement.getQualifiedName().contentEquals(interfaceName)) { return true; } for (TypeMirror implemented : typeElement.getInterfaces()) { if (find(interfaceName, (TypeElement) ((DeclaredType) implemented).asElement())) { return true; } } while (typeElement.getSuperclass().getKind() != TypeKind.NONE) { typeElement = (TypeElement) ((DeclaredType) typeElement.getSuperclass()).asElement(); if (find(interfaceName, typeElement)) { return true; } } return false; }
/** * Check for the presence of an annotation with a specific simple name directly on this symbol. * Does *not* consider annotation inheritance. * * @param sym the symbol to check for the presence of the annotation * @param simpleName the simple name of the annotation to look for, e.g. "Nullable" or * "CheckReturnValue" */ public static boolean hasDirectAnnotationWithSimpleName(Symbol sym, String simpleName) { for (AnnotationMirror annotation : sym.getAnnotationMirrors()) { if (annotation.getAnnotationType().asElement().getSimpleName().contentEquals(simpleName)) { return true; } } return false; }
@Override public Void visitAnnotation(AnnotationMirror a, Void p) { builder.append('@').append(a.getAnnotationType()); Map<? extends ExecutableElement, ? extends AnnotationValue> values = a.getElementValues(); if (!values.isEmpty()) { builder.append('('); boolean notFirst = false; for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e : values.entrySet()) { if (notFirst) { builder.append(", "); } notFirst = true; Name name = e.getKey().getSimpleName(); boolean onlyValue = values.size() == 1 && name.contentEquals(ATTRIBUTE_VALUE); if (!onlyValue) { builder.append(name).append(" = "); } printValue(e.getValue()); } builder.append(')'); } return null; }
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(); }
@Override public String apply(VariableTree variableTree) { for (Compound c : ASTHelpers.getSymbol(variableTree).getAnnotationMirrors()) { if (((TypeElement) c.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(ASSISTED_ANNOTATION)) { // Assisted only has 'value', and value can only contain 1 element. Collection<Attribute> valueEntries = c.getElementValues().values(); if (!valueEntries.isEmpty()) { return Iterables.getOnlyElement(valueEntries).getValue().toString(); } } } return ""; } };
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 boolean namesEqual(TypeElement element, String qualifiedName) { return element != null && element.getQualifiedName().contentEquals(qualifiedName); } }
/** * Retrieves the attribute value from the annotation and assumes it's an array {@link Class classes}. * * @param element the element the annotation is on * @param annotation the annotation to get the value from * @param attributeName the name of the attribute to retrieve the class value array for * * @return a list of {@link TypeMirror} representing the value for the annotation attribute or an empty list */ public static List<TypeMirror> getClassArrayAnnotationValue(final Element element, final Class<? extends Annotation> annotation, @SuppressWarnings("SameParameterValue") final String attributeName) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { final DeclaredType annotationType = mirror.getAnnotationType(); if (annotationType.toString().equals(annotation.getName())) { final Map<? extends ExecutableElement, ? extends AnnotationValue> map = mirror.getElementValues(); for (ExecutableElement key : map.keySet()) { if (key.getSimpleName().contentEquals(attributeName)) { @SuppressWarnings("unchecked") final List<AnnotationValue> annotationValues = (List<AnnotationValue>) map.get(key).getValue(); final List<TypeMirror> result = new ArrayList<>(annotationValues.size()); for (AnnotationValue value : annotationValues) { result.add((TypeMirror) value.getValue()); } return result; } } } } return Collections.emptyList(); }
private TypeMirror createStringTypeMirror() { TypeElement element = typeElement; while (element.getSuperclass().getKind() != TypeKind.NONE) { logger.finest("finding toString in " + element); element = (TypeElement) ((DeclaredType) element.getSuperclass()).asElement(); } for (Element enclosed : typeElement.getEnclosedElements()) { if (enclosed.getKind() == ElementKind.METHOD) { ExecutableElement method = (ExecutableElement) enclosed; if (method.getSimpleName().contentEquals("toString")) { return method.getReturnType(); } } } throw new IllegalStateException("Cannot find toString method in Object"); } private static boolean find(String interfaceName, TypeElement typeElement) {
@Value.Lazy @Override public JacksonMode jacksonSerializeMode() { boolean wasJacksonSerialize = false; for (AnnotationMirror a : element().getAnnotationMirrors()) { TypeElement e = (TypeElement) a.getAnnotationType().asElement(); if (!wasJacksonSerialize && e.getQualifiedName().contentEquals(JACKSON_SERIALIZE)) { wasJacksonSerialize = true; } if (e.getQualifiedName().contentEquals(JACKSON_DESERIALIZE)) { for (ExecutableElement attr : a.getElementValues().keySet()) { if (attr.getSimpleName().contentEquals("builder")) { // If builder attribute is specified, we don't consider this as // our, immutables, business to generate anything. return JacksonMode.BUILDER; } } return JacksonMode.DELEGATED; } } return wasJacksonSerialize ? JacksonMode.DELEGATED : JacksonMode.NONE; }
private static Set<Modifier> getIncompatibleModifiers(AnnotationTree tree, VisitorState state) { for (Attribute.Compound c : ASTHelpers.getSymbol(tree).getAnnotationMirrors()) { if (((TypeElement) c.getAnnotationType().asElement()) .getQualifiedName() .contentEquals(GUAVA_ANNOTATION)) { @SuppressWarnings("unchecked") List<Attribute.Enum> modifiers = (List<Attribute.Enum>) c.member(state.getName("value")).getValue(); return ImmutableSet.copyOf( Iterables.transform( modifiers, (Attribute.Enum input) -> Modifier.valueOf(input.getValue().name.toString()))); } } IncompatibleModifiers annotation = ASTHelpers.getAnnotation(tree, IncompatibleModifiers.class); if (annotation != null) { return ImmutableSet.copyOf(annotation.value()); } return ImmutableSet.of(); }
/** * @param element a source element * @param annotation a type of annotation * @return the instance of that annotation on the element, or null if not found */ private AnnotationMirror findAnnotationMirror(Element element, 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 boolean typeInAnnotations(TypeElement t, List<String> annotations) { return annotations.stream() .anyMatch(annotation -> t.getQualifiedName().contentEquals(annotation)); } }
if (e != null) { for (AnnotationMirror _mirror : e.getAnnotationMirrors()) { if (_mirror.getAnnotationType().toString().equals(NbBundle.Messages.class.getCanonicalName())) { mirror = _mirror; for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().contentEquals("value")) {
private static ImmutableSet<String> getAnnotationValueAsStrings(Symbol sym, String guardedBy) { return sym.getRawAttributes().stream() .filter(a -> a.getAnnotationType().asElement().getSimpleName().contentEquals(guardedBy)) .flatMap( a -> // TODO(cushon): after the next release: // MoreAnnotations.getValue(a, "value").map(MoreAnnotations::asStrings) a.getElementValues().entrySet().stream() .filter(e -> e.getKey().getSimpleName().contentEquals("value")) .map(Map.Entry::getValue) .findFirst() .map(GuardedByUtils::asStrings) .orElse(Stream.empty())) .collect(toImmutableSet()); }
static boolean isAnnotatedWith(Element element, String annotation) { for (AnnotationMirror a : element.getAnnotationMirrors()) { TypeElement e = (TypeElement) a.getAnnotationType().asElement(); if (e.getQualifiedName().contentEquals(annotation)) { return true; } if (Annotations.hasJacksonPackagePrefix(annotation) && !annotation.equals(JACKSON_ANNOTATIONS_INSIDE) && isAnnotatedWith(e, JACKSON_ANNOTATIONS_INSIDE) && isAnnotatedWith(e, annotation)) { return true; } } return false; }
private boolean isJavaLangObject(TypeMirror type) { if (type.getKind() != TypeKind.DECLARED) { return false; } DeclaredType declaredType = (DeclaredType) type; TypeElement typeElement = (TypeElement) declaredType.asElement(); return typeElement.getQualifiedName().contentEquals("java.lang.Object"); } };