Search in sources :

Example 21 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class ForgedMethod method matches.

@Override
public boolean matches(List<Type> sourceTypes, Type targetType) {
    if (!targetType.equals(returnType)) {
        return false;
    }
    if (parameters.size() != sourceTypes.size()) {
        return false;
    }
    Iterator<Type> srcTypeIt = sourceTypes.iterator();
    Iterator<Parameter> paramIt = parameters.iterator();
    while (srcTypeIt.hasNext() && paramIt.hasNext()) {
        Type sourceType = srcTypeIt.next();
        Parameter param = paramIt.next();
        if (!sourceType.equals(param.getType())) {
            return false;
        }
    }
    return true;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 22 with Type

use of org.mapstruct.ap.internal.model.common.Type 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 23 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class MapperCreationProcessor method getExtraImports.

private SortedSet<Type> getExtraImports(TypeElement element) {
    SortedSet<Type> extraImports = new TreeSet<Type>();
    MapperConfiguration mapperConfiguration = MapperConfiguration.getInstanceOn(element);
    for (TypeMirror extraImport : mapperConfiguration.imports()) {
        Type type = typeFactory.getType(extraImport);
        extraImports.add(type);
    }
    // Add original package if a dest package has been set
    if (!"default".equals(mapperConfiguration.implementationPackage())) {
        extraImports.add(typeFactory.getType(element));
    }
    return extraImports;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet) MapperConfiguration(org.mapstruct.ap.internal.util.MapperConfiguration)

Example 24 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method getMethod.

private SourceMethod getMethod(TypeElement usedMapper, ExecutableElement method, TypeElement mapperToImplement, MapperConfiguration mapperConfig, List<SourceMethod> prototypeMethods) {
    ExecutableType methodType = typeFactory.getMethodType((DeclaredType) usedMapper.asType(), method);
    List<Parameter> parameters = typeFactory.getParameters(methodType, method);
    Type returnType = typeFactory.getReturnType(methodType);
    boolean methodRequiresImplementation = method.getModifiers().contains(Modifier.ABSTRACT);
    boolean containsTargetTypeParameter = SourceMethod.containsTargetTypeParameter(parameters);
    // add method with property mappings if an implementation needs to be generated
    if ((usedMapper.equals(mapperToImplement)) && methodRequiresImplementation) {
        return getMethodRequiringImplementation(methodType, method, parameters, containsTargetTypeParameter, mapperConfig, prototypeMethods, mapperToImplement);
    } else // otherwise add reference to existing mapper method
    if (isValidReferencedMethod(parameters) || isValidFactoryMethod(method, parameters, returnType) || isValidLifecycleCallbackMethod(method, returnType)) {
        return getReferencedMethod(usedMapper, methodType, method, mapperToImplement, parameters);
    } else {
        return null;
    }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 25 with Type

use of org.mapstruct.ap.internal.model.common.Type in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method getMethodRequiringImplementation.

private SourceMethod getMethodRequiringImplementation(ExecutableType methodType, ExecutableElement method, List<Parameter> parameters, boolean containsTargetTypeParameter, MapperConfiguration mapperConfig, List<SourceMethod> prototypeMethods, TypeElement mapperToImplement) {
    Type returnType = typeFactory.getReturnType(methodType);
    List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
    List<Parameter> sourceParameters = Parameter.getSourceParameters(parameters);
    List<Parameter> contextParameters = Parameter.getContextParameters(parameters);
    Parameter targetParameter = extractTargetParameter(parameters);
    Type resultType = selectResultType(returnType, targetParameter);
    boolean isValid = checkParameterAndReturnType(method, sourceParameters, targetParameter, contextParameters, resultType, returnType, containsTargetTypeParameter);
    if (!isValid) {
        return null;
    }
    ParameterProvidedMethods contextProvidedMethods = retrieveLifecycleMethodsFromContext(contextParameters, mapperToImplement, mapperConfig);
    return new SourceMethod.Builder().setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setMappings(getMappings(method)).setIterableMapping(IterableMapping.fromPrism(IterableMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setMapMapping(MapMapping.fromPrism(MapMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setBeanMapping(BeanMapping.fromPrism(BeanMappingPrism.getInstanceOn(method), method, messager, typeUtils)).setValueMappings(getValueMappings(method)).setTypeUtils(typeUtils).setMessager(messager).setTypeFactory(typeFactory).setMapperConfiguration(mapperConfig).setPrototypeMethods(prototypeMethods).setContextProvidedMethods(contextProvidedMethods).build();
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Aggregations

Type (org.mapstruct.ap.internal.model.common.Type)36 ArrayList (java.util.ArrayList)13 Parameter (org.mapstruct.ap.internal.model.common.Parameter)11 DeclaredType (javax.lang.model.type.DeclaredType)10 ExecutableType (javax.lang.model.type.ExecutableType)6 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)6 HashSet (java.util.HashSet)5 TypeMirror (javax.lang.model.type.TypeMirror)4 TreeSet (java.util.TreeSet)3 BeanMappingMethod (org.mapstruct.ap.internal.model.BeanMappingMethod)3 ContainerMappingMethod (org.mapstruct.ap.internal.model.ContainerMappingMethod)3 IterableMappingMethod (org.mapstruct.ap.internal.model.IterableMappingMethod)3 MapMappingMethod (org.mapstruct.ap.internal.model.MapMappingMethod)3 MappingMethod (org.mapstruct.ap.internal.model.MappingMethod)3 StreamMappingMethod (org.mapstruct.ap.internal.model.StreamMappingMethod)3 ValueMappingMethod (org.mapstruct.ap.internal.model.ValueMappingMethod)3 Map (java.util.Map)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 ArrayType (javax.lang.model.type.ArrayType)2