Search in sources :

Example 96 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project mapstruct by mapstruct.

the class QualifierSelector method getQualifierAnnotationMirrors.

private Set<AnnotationMirror> getQualifierAnnotationMirrors(Method candidate) {
    // retrieve annotations
    Set<AnnotationMirror> qualiferAnnotations = new HashSet<AnnotationMirror>();
    // first from the method itself
    SourceMethod candidateSM = (SourceMethod) candidate;
    List<? extends AnnotationMirror> methodAnnotations = candidateSM.getExecutable().getAnnotationMirrors();
    for (AnnotationMirror methodAnnotation : methodAnnotations) {
        addOnlyWhenQualifier(qualiferAnnotations, methodAnnotation);
    }
    // then from the mapper (if declared)
    Type mapper = candidate.getDeclaringMapper();
    if (mapper != null) {
        List<? extends AnnotationMirror> mapperAnnotations = mapper.getTypeElement().getAnnotationMirrors();
        for (AnnotationMirror mapperAnnotation : mapperAnnotations) {
            addOnlyWhenQualifier(qualiferAnnotations, mapperAnnotation);
        }
    }
    return qualiferAnnotations;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) HashSet(java.util.HashSet)

Example 97 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project mapstruct by mapstruct.

the class QualifierSelector method getMatchingMethods.

@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod, List<SelectedMethod<T>> methods, List<Type> sourceTypes, Type targetType, SelectionCriteria criteria) {
    int numberOfQualifiersToMatch = 0;
    // Define some local collections and make sure that they are defined.
    List<TypeMirror> qualifierTypes = new ArrayList<TypeMirror>();
    if (criteria.getQualifiers() != null) {
        qualifierTypes.addAll(criteria.getQualifiers());
        numberOfQualifiersToMatch += criteria.getQualifiers().size();
    }
    List<String> qualfiedByNames = new ArrayList<String>();
    if (criteria.getQualifiedByNames() != null) {
        qualfiedByNames.addAll(criteria.getQualifiedByNames());
        numberOfQualifiersToMatch += criteria.getQualifiedByNames().size();
    }
    // add the mapstruct @Named annotation as annotation to look for
    if (!qualfiedByNames.isEmpty()) {
        qualifierTypes.add(namedAnnotationTypeMirror);
    }
    // Check there are qualfiers for this mapping: Mapping#qualifier or Mapping#qualfiedByName
    if (qualifierTypes.isEmpty()) {
        // When no qualifiers, disqualify all methods marked with a qualifier by removing them from the candidates
        List<SelectedMethod<T>> nonQualiferAnnotatedMethods = new ArrayList<SelectedMethod<T>>(methods.size());
        for (SelectedMethod<T> candidate : methods) {
            if (candidate.getMethod() instanceof SourceMethod) {
                Set<AnnotationMirror> qualifierAnnotations = getQualifierAnnotationMirrors(candidate.getMethod());
                if (qualifierAnnotations.isEmpty()) {
                    nonQualiferAnnotatedMethods.add(candidate);
                }
            } else {
                nonQualiferAnnotatedMethods.add(candidate);
            }
        }
        return nonQualiferAnnotatedMethods;
    } else {
        // Check all methods marked with qualfier (or methods in Mappers marked wiht a qualfier) for matches.
        List<SelectedMethod<T>> matches = new ArrayList<SelectedMethod<T>>(methods.size());
        for (SelectedMethod<T> candidate : methods) {
            if (!(candidate.getMethod() instanceof SourceMethod)) {
                continue;
            }
            // retrieve annotations
            Set<AnnotationMirror> qualifierAnnotationMirrors = getQualifierAnnotationMirrors(candidate.getMethod());
            // now count if all qualifiers are matched
            int matchingQualifierCounter = 0;
            for (AnnotationMirror qualifierAnnotationMirror : qualifierAnnotationMirrors) {
                for (TypeMirror qualifierType : qualifierTypes) {
                    // get the type of the annotation mirror.
                    DeclaredType qualifierAnnotationType = qualifierAnnotationMirror.getAnnotationType();
                    if (typeUtils.isSameType(qualifierType, qualifierAnnotationType)) {
                        // Match! we have an annotation which has the @Qualifer marker ( could be @Named as well )
                        if (typeUtils.isSameType(qualifierAnnotationType, namedAnnotationTypeMirror)) {
                            // Match! its an @Named, so do the additional check on name.
                            NamedPrism namedPrism = NamedPrism.getInstance(qualifierAnnotationMirror);
                            if (namedPrism.value() != null && qualfiedByNames.contains(namedPrism.value())) {
                                // Match! its an @Name and the value matches as well. Oh boy.
                                matchingQualifierCounter++;
                            }
                        } else {
                            // Match! its a self declared qualifer annoation (marked with @Qualifier)
                            matchingQualifierCounter++;
                        }
                        break;
                    }
                }
            }
            if (matchingQualifierCounter == numberOfQualifiersToMatch) {
                // Only if all qualifiers are matched with a qualifying annotation, add candidate
                matches.add(candidate);
            }
        }
        if (!matches.isEmpty()) {
            return matches;
        } else {
            return methods;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) NamedPrism(org.mapstruct.ap.internal.prism.NamedPrism) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) DeclaredType(javax.lang.model.type.DeclaredType)

Example 98 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project revapi by revapi.

the class AttributeValueChanged method doVisitAnnotation.

@Override
protected List<Difference> doVisitAnnotation(JavaAnnotationElement oldElement, JavaAnnotationElement newElement) {
    if (oldElement == null || newElement == null || !isAccessible(newElement.getParent())) {
        return null;
    }
    AnnotationMirror oldAnnotation = oldElement.getAnnotation();
    AnnotationMirror newAnnotation = newElement.getAnnotation();
    List<Difference> result = new ArrayList<>();
    Map<String, Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> oldAttrs = Util.keyAnnotationAttributesByName(oldAnnotation.getElementValues());
    Map<String, Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> newAttrs = Util.keyAnnotationAttributesByName(newAnnotation.getElementValues());
    for (Map.Entry<String, Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> oldE : oldAttrs.entrySet()) {
        String name = oldE.getKey();
        Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> oldValue = oldE.getValue();
        Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> newValue = newAttrs.get(name);
        if (newValue == null) {
            result.add(createDifference(Code.ANNOTATION_ATTRIBUTE_REMOVED, Code.attachmentsFor(oldElement.getParent(), newElement.getParent(), "annotationType", Util.toHumanReadableString(newElement.getAnnotation().getAnnotationType()), "annotation", Util.toHumanReadableString(newElement.getAnnotation()), "attribute", name, "value", Util.toHumanReadableString(oldValue.getValue()))));
        } else if (!Util.isEqual(oldValue.getValue(), newValue.getValue())) {
            result.add(createDifference(Code.ANNOTATION_ATTRIBUTE_VALUE_CHANGED, Code.attachmentsFor(oldElement.getParent(), newElement.getParent(), "annotationType", Util.toHumanReadableString(newElement.getAnnotation().getAnnotationType()), "annotation", Util.toHumanReadableString(newElement.getAnnotation()), "attribute", name, "oldValue", Util.toHumanReadableString(oldValue.getValue()), "newValue", Util.toHumanReadableString(newValue.getValue()))));
        }
        newAttrs.remove(name);
    }
    for (Map.Entry<String, Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> newE : newAttrs.entrySet()) {
        String name = newE.getKey();
        Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> newValue = newE.getValue();
        Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> oldValue = oldAttrs.get(name);
        if (oldValue == null) {
            result.add(createDifference(Code.ANNOTATION_ATTRIBUTE_ADDED, Code.attachmentsFor(oldElement.getParent(), newElement.getParent(), "annotationType", Util.toHumanReadableString(newElement.getAnnotation().getAnnotationType()), "annotation", Util.toHumanReadableString(newElement.getAnnotation()), "attribute", name, "value", Util.toHumanReadableString(newValue.getValue()))));
        }
    }
    return result.isEmpty() ? null : result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) AnnotationValue(javax.lang.model.element.AnnotationValue) Difference(org.revapi.Difference) Map(java.util.Map)

Example 99 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project antlr4 by tunnelvisionlabs.

the class RuleDependencyProcessor method findRuleDependencyProperty.

@Nullable
private Tuple2<AnnotationMirror, AnnotationValue> findRuleDependencyProperty(@NotNull Tuple2<RuleDependency, Element> dependency, @NotNull RuleDependencyProperty property) {
    TypeElement ruleDependencyTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependencyClassName);
    TypeElement ruleDependenciesTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependenciesClassName);
    List<? extends AnnotationMirror> mirrors = dependency.getItem2().getAnnotationMirrors();
    for (AnnotationMirror annotationMirror : mirrors) {
        if (processingEnv.getTypeUtils().isSameType(ruleDependencyTypeElement.asType(), annotationMirror.getAnnotationType())) {
            AnnotationValue element = findRuleDependencyProperty(dependency, annotationMirror, property);
            if (element != null) {
                return Tuple.create(annotationMirror, element);
            }
        } else if (processingEnv.getTypeUtils().isSameType(ruleDependenciesTypeElement.asType(), annotationMirror.getAnnotationType())) {
            Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationMirror.getElementValues();
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> value : values.entrySet()) {
                if ("value()".equals(value.getKey().toString())) {
                    AnnotationValue annotationValue = value.getValue();
                    if (!(annotationValue.getValue() instanceof List)) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected array of RuleDependency annotations for annotation property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
                        break;
                    }
                    List<?> annotationValueList = (List<?>) annotationValue.getValue();
                    for (Object obj : annotationValueList) {
                        if (!(obj instanceof AnnotationMirror)) {
                            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected RuleDependency annotation mirror for element of property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
                            break;
                        }
                        AnnotationValue element = findRuleDependencyProperty(dependency, (AnnotationMirror) obj, property);
                        if (element != null) {
                            return Tuple.create((AnnotationMirror) obj, element);
                        }
                    }
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("Unexpected annotation property %s.", value.getKey().toString()), dependency.getItem2(), annotationMirror, value.getValue());
                }
            }
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 100 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project querydsl by querydsl.

the class JPAConfiguration method getRealElementType.

private TypeMirror getRealElementType(Element element) {
    AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToOne.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToOne.class);
    }
    if (mirror != null) {
        return TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
    }
    mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToMany.class);
    if (mirror == null) {
        mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToMany.class);
    }
    if (mirror != null) {
        TypeMirror typeArg = TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
        TypeMirror erasure;
        if (element instanceof ExecutableElement) {
            erasure = ((ExecutableElement) element).getReturnType();
        } else {
            erasure = types.erasure(element.asType());
        }
        TypeElement typeElement = (TypeElement) types.asElement(erasure);
        if (typeElement != null && typeArg != null) {
            if (typeElement.getTypeParameters().size() == 1) {
                return types.getDeclaredType(typeElement, typeArg);
            } else if (typeElement.getTypeParameters().size() == 2) {
                if (element.asType() instanceof DeclaredType) {
                    TypeMirror first = ((DeclaredType) element.asType()).getTypeArguments().get(0);
                    return types.getDeclaredType(typeElement, first, typeArg);
                }
            }
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) OneToOne(javax.persistence.OneToOne) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ManyToMany(javax.persistence.ManyToMany) OneToMany(javax.persistence.OneToMany) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29