Search in sources :

Example 1 with DelegatingMethod

use of org.mapstruct.ap.internal.model.DelegatingMethod 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)

Example 2 with DelegatingMethod

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

the class MapperCreationProcessor method getDecorator.

private Decorator getDecorator(TypeElement element, List<SourceMethod> methods, MapperOptions mapperOptions) {
    DecoratedWithGem decoratedWith = DecoratedWithGem.instanceOn(element);
    if (decoratedWith == null) {
        return null;
    }
    TypeElement decoratorElement = (TypeElement) typeUtils.asElement(decoratedWith.value().get());
    if (!typeUtils.isAssignable(decoratorElement.asType(), element.asType())) {
        messager.printMessage(element, decoratedWith.mirror(), Message.DECORATOR_NO_SUBTYPE);
    }
    List<MappingMethod> mappingMethods = new ArrayList<>(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, decoratedWith.mirror(), Message.DECORATOR_CONSTRUCTOR);
    }
    Decorator decorator = new Decorator.Builder().elementUtils(elementUtils).typeFactory(typeFactory).mapperElement(element).decoratedWith(decoratedWith).methods(mappingMethods).hasDelegateConstructor(hasDelegateConstructor).options(options).versionInformation(versionInformation).implName(mapperOptions.implementationName()).implPackage(mapperOptions.implementationPackage()).extraImports(getExtraImports(element, mapperOptions)).suppressGeneratorTimestamp(mapperOptions.suppressTimestampInGenerated()).build();
    return decorator;
}
Also used : DecoratedWithGem(org.mapstruct.ap.internal.gem.DecoratedWithGem) 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) MapMappingMethod(org.mapstruct.ap.internal.model.MapMappingMethod) 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) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Aggregations

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