Search in sources :

Example 1 with DecoratedWithGem

use of org.mapstruct.ap.internal.gem.DecoratedWithGem 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)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 DecoratedWithGem (org.mapstruct.ap.internal.gem.DecoratedWithGem)1 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)1 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)1 Decorator (org.mapstruct.ap.internal.model.Decorator)1 DelegatingMethod (org.mapstruct.ap.internal.model.DelegatingMethod)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