Search in sources :

Example 1 with Decorator

use of org.mapstruct.ap.internal.model.Decorator 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 2 with Decorator

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

the class MapperCreationProcessor method getDecorator.

private Decorator getDecorator(TypeElement element, List<SourceMethod> methods, String implName, String implPackage) {
    DecoratedWithPrism decoratorPrism = DecoratedWithPrism.getInstanceOn(element);
    if (decoratorPrism == null) {
        return null;
    }
    TypeElement decoratorElement = (TypeElement) typeUtils.asElement(decoratorPrism.value());
    if (!typeUtils.isAssignable(decoratorElement.asType(), element.asType())) {
        messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_NO_SUBTYPE);
    }
    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>(methods.size());
    for (SourceMethod mappingMethod : methods) {
        boolean implementationRequired = true;
        for (ExecutableElement method : ElementFilter.methodsIn(decoratorElement.getEnclosedElements())) {
            if (elementUtils.overrides(method, mappingMethod.getExecutable(), decoratorElement)) {
                implementationRequired = false;
                break;
            }
        }
        Type declaringMapper = mappingMethod.getDeclaringMapper();
        if (implementationRequired && !(mappingMethod.isDefault() || mappingMethod.isStatic())) {
            if ((declaringMapper == null) || declaringMapper.equals(typeFactory.getType(element))) {
                mappingMethods.add(new DelegatingMethod(mappingMethod));
            }
        }
    }
    boolean hasDelegateConstructor = false;
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor : ElementFilter.constructorsIn(decoratorElement.getEnclosedElements())) {
        if (constructor.getParameters().isEmpty()) {
            hasDefaultConstructor = true;
        } else if (constructor.getParameters().size() == 1) {
            if (typeUtils.isAssignable(element.asType(), first(constructor.getParameters()).asType())) {
                hasDelegateConstructor = true;
            }
        }
    }
    if (!hasDelegateConstructor && !hasDefaultConstructor) {
        messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR);
    }
    Decorator decorator = new Decorator.Builder().elementUtils(elementUtils).typeFactory(typeFactory).mapperElement(element).decoratorPrism(decoratorPrism).methods(mappingMethods).hasDelegateConstructor(hasDelegateConstructor).options(options).versionInformation(versionInformation).implName(implName).implPackage(implPackage).extraImports(getExtraImports(element)).build();
    return decorator;
}
Also used : Decorator(org.mapstruct.ap.internal.model.Decorator) Type(org.mapstruct.ap.internal.model.common.Type) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ContainerMappingMethodBuilder(org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) EnumMappingMethod(org.mapstruct.ap.internal.model.EnumMappingMethod) IterableMappingMethod(org.mapstruct.ap.internal.model.IterableMappingMethod) ValueMappingMethod(org.mapstruct.ap.internal.model.ValueMappingMethod) MappingMethod(org.mapstruct.ap.internal.model.MappingMethod) StreamMappingMethod(org.mapstruct.ap.internal.model.StreamMappingMethod) ContainerMappingMethod(org.mapstruct.ap.internal.model.ContainerMappingMethod) BeanMappingMethod(org.mapstruct.ap.internal.model.BeanMappingMethod) ArrayList(java.util.ArrayList) DelegatingMethod(org.mapstruct.ap.internal.model.DelegatingMethod) DecoratedWithPrism(org.mapstruct.ap.internal.prism.DecoratedWithPrism) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

ArrayList (java.util.ArrayList)2 Decorator (org.mapstruct.ap.internal.model.Decorator)2 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 Annotation (org.mapstruct.ap.internal.model.Annotation)1 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)1 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)1 ContainerMappingMethodBuilder (org.mapstruct.ap.internal.model.ContainerMappingMethodBuilder)1 DelegatingMethod (org.mapstruct.ap.internal.model.DelegatingMethod)1 EnumMappingMethod (org.mapstruct.ap.internal.model.EnumMappingMethod)1 Field (org.mapstruct.ap.internal.model.Field)1 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)1 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)1 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)1 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)1 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)1 Type (org.mapstruct.ap.internal.model.common.Type)1 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)1 DecoratedWithPrism (org.mapstruct.ap.internal.prism.DecoratedWithPrism)1