Search in sources :

Example 1 with Annotation

use of org.mapstruct.ap.internal.model.Annotation in project mapstruct by mapstruct.

the class AnnotationBasedComponentModelProcessor method process.

@Override
public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, Mapper mapper) {
    this.typeFactory = context.getTypeFactory();
    MapperConfiguration mapperConfiguration = MapperConfiguration.getInstanceOn(mapperTypeElement);
    String componentModel = mapperConfiguration.componentModel(context.getOptions());
    InjectionStrategyPrism injectionStrategy = mapperConfiguration.getInjectionStrategy();
    if (!getComponentModelIdentifier().equalsIgnoreCase(componentModel)) {
        return mapper;
    }
    for (Annotation typeAnnotation : getTypeAnnotations(mapper)) {
        mapper.addAnnotation(typeAnnotation);
    }
    if (!requiresGenerationOfDecoratorClass()) {
        mapper.removeDecorator();
    } else if (mapper.getDecorator() != null) {
        adjustDecorator(mapper, injectionStrategy);
    }
    List<Annotation> annotations = getMapperReferenceAnnotations();
    ListIterator<MapperReference> iterator = mapper.getReferencedMappers().listIterator();
    while (iterator.hasNext()) {
        MapperReference reference = iterator.next();
        iterator.remove();
        iterator.add(replacementMapperReference(reference, annotations, injectionStrategy));
    }
    if (injectionStrategy == InjectionStrategyPrism.CONSTRUCTOR) {
        buildConstructors(mapper);
    }
    return mapper;
}
Also used : InjectionStrategyPrism(org.mapstruct.ap.internal.prism.InjectionStrategyPrism) MapperReference(org.mapstruct.ap.internal.model.MapperReference) AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) MapperConfiguration(org.mapstruct.ap.internal.util.MapperConfiguration) Annotation(org.mapstruct.ap.internal.model.Annotation)

Example 2 with Annotation

use of org.mapstruct.ap.internal.model.Annotation in project mapstruct by mapstruct.

the class AnnotationBasedComponentModelProcessor method buildAnnotatedConstructorForDecorator.

private AnnotatedConstructor buildAnnotatedConstructorForDecorator(Decorator decorator) {
    List<AnnotationMapperReference> mapperReferencesForConstructor = new ArrayList<AnnotationMapperReference>(decorator.getFields().size());
    for (Field field : decorator.getFields()) {
        if (field instanceof AnnotationMapperReference) {
            mapperReferencesForConstructor.add((AnnotationMapperReference) field);
        }
    }
    List<Annotation> mapperReferenceAnnotations = getMapperReferenceAnnotations();
    removeDuplicateAnnotations(mapperReferencesForConstructor, mapperReferenceAnnotations);
    return new AnnotatedConstructor(decorator.getName(), mapperReferencesForConstructor, mapperReferenceAnnotations, additionalPublicEmptyConstructor());
}
Also used : AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) Field(org.mapstruct.ap.internal.model.Field) AnnotatedConstructor(org.mapstruct.ap.internal.model.AnnotatedConstructor) ArrayList(java.util.ArrayList) Annotation(org.mapstruct.ap.internal.model.Annotation)

Example 3 with Annotation

use of org.mapstruct.ap.internal.model.Annotation in project mapstruct by mapstruct.

the class AnnotationBasedComponentModelProcessor method adjustDecorator.

protected void adjustDecorator(Mapper mapper, InjectionStrategyPrism injectionStrategy) {
    Decorator decorator = mapper.getDecorator();
    for (Annotation typeAnnotation : getDecoratorAnnotations()) {
        decorator.addAnnotation(typeAnnotation);
    }
    decorator.removeConstructor();
    List<Annotation> annotations = getDelegatorReferenceAnnotations(mapper);
    List<Field> replacement = new ArrayList<Field>();
    if (!decorator.getMethods().isEmpty()) {
        for (Field field : decorator.getFields()) {
            replacement.add(replacementMapperReference(field, annotations, injectionStrategy));
        }
    }
    decorator.setFields(replacement);
}
Also used : Decorator(org.mapstruct.ap.internal.model.Decorator) Field(org.mapstruct.ap.internal.model.Field) ArrayList(java.util.ArrayList) Annotation(org.mapstruct.ap.internal.model.Annotation)

Example 4 with Annotation

use of org.mapstruct.ap.internal.model.Annotation in project mapstruct by mapstruct.

the class AnnotationBasedComponentModelProcessor method removeDuplicateAnnotations.

/**
 * Removes duplicate constructor parameter annotations. If an annotation is already present on the constructor, it
 * does not have be defined on the constructor parameter, too. For example, for CDI, the javax.inject.Inject
 * annotation is on the constructor and does not need to be on the constructor parameters.
 *
 * @param annotationMapperReferences annotations to annotate the constructor parameter with
 * @param mapperReferenceAnnotations annotations to annotate the constructor with
 */
private void removeDuplicateAnnotations(List<AnnotationMapperReference> annotationMapperReferences, List<Annotation> mapperReferenceAnnotations) {
    ListIterator<AnnotationMapperReference> mapperReferenceIterator = annotationMapperReferences.listIterator();
    Set<Type> mapperReferenceAnnotationsTypes = new HashSet<Type>();
    for (Annotation annotation : mapperReferenceAnnotations) {
        mapperReferenceAnnotationsTypes.add(annotation.getType());
    }
    while (mapperReferenceIterator.hasNext()) {
        AnnotationMapperReference annotationMapperReference = mapperReferenceIterator.next();
        mapperReferenceIterator.remove();
        List<Annotation> qualifiers = new ArrayList<Annotation>();
        for (Annotation annotation : annotationMapperReference.getAnnotations()) {
            if (!mapperReferenceAnnotationsTypes.contains(annotation.getType())) {
                qualifiers.add(annotation);
            }
        }
        mapperReferenceIterator.add(annotationMapperReference.withNewAnnotations(qualifiers));
    }
}
Also used : AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList) Annotation(org.mapstruct.ap.internal.model.Annotation) HashSet(java.util.HashSet)

Example 5 with Annotation

use of org.mapstruct.ap.internal.model.Annotation in project mapstruct by mapstruct.

the class AnnotationBasedComponentModelProcessor method buildAnnotatedConstructorForMapper.

private AnnotatedConstructor buildAnnotatedConstructorForMapper(Mapper mapper) {
    List<AnnotationMapperReference> mapperReferencesForConstructor = new ArrayList<AnnotationMapperReference>(mapper.getReferencedMappers().size());
    for (MapperReference mapperReference : mapper.getReferencedMappers()) {
        if (mapperReference.isUsed()) {
            mapperReferencesForConstructor.add((AnnotationMapperReference) mapperReference);
        }
    }
    List<Annotation> mapperReferenceAnnotations = getMapperReferenceAnnotations();
    removeDuplicateAnnotations(mapperReferencesForConstructor, mapperReferenceAnnotations);
    return new AnnotatedConstructor(mapper.getName(), mapperReferencesForConstructor, mapperReferenceAnnotations, additionalPublicEmptyConstructor());
}
Also used : AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) AnnotatedConstructor(org.mapstruct.ap.internal.model.AnnotatedConstructor) MapperReference(org.mapstruct.ap.internal.model.MapperReference) AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) ArrayList(java.util.ArrayList) Annotation(org.mapstruct.ap.internal.model.Annotation)

Aggregations

Annotation (org.mapstruct.ap.internal.model.Annotation)5 ArrayList (java.util.ArrayList)4 AnnotationMapperReference (org.mapstruct.ap.internal.model.AnnotationMapperReference)4 AnnotatedConstructor (org.mapstruct.ap.internal.model.AnnotatedConstructor)2 Field (org.mapstruct.ap.internal.model.Field)2 MapperReference (org.mapstruct.ap.internal.model.MapperReference)2 HashSet (java.util.HashSet)1 Decorator (org.mapstruct.ap.internal.model.Decorator)1 Type (org.mapstruct.ap.internal.model.common.Type)1 InjectionStrategyPrism (org.mapstruct.ap.internal.prism.InjectionStrategyPrism)1 MapperConfiguration (org.mapstruct.ap.internal.util.MapperConfiguration)1