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;
}
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;
}
}
}
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;
}
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;
}
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;
}
Aggregations