Search in sources :

Example 1 with Parameter

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

the class SourceMethod method getParameterNames.

@Override
public List<String> getParameterNames() {
    if (parameterNames == null) {
        List<String> names = new ArrayList<String>(parameters.size());
        for (Parameter parameter : parameters) {
            names.add(parameter.getName());
        }
        parameterNames = Collections.unmodifiableList(names);
    }
    return parameterNames;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter)

Example 2 with Parameter

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

the class SourceMethod method allParametersAreAssignable.

private static boolean allParametersAreAssignable(List<Parameter> fromParams, List<Parameter> toParams) {
    if (fromParams.size() == toParams.size()) {
        Set<Parameter> unaccountedToParams = new HashSet<Parameter>(toParams);
        for (Parameter fromParam : fromParams) {
            // each fromParam needs at least one match, and all toParam need to be accounted for at the end
            boolean hasMatch = false;
            for (Parameter toParam : toParams) {
                if (fromParam.getType().isAssignableTo(toParam.getType())) {
                    unaccountedToParams.remove(toParam);
                    hasMatch = true;
                }
            }
            if (!hasMatch) {
                return false;
            }
        }
        return unaccountedToParams.isEmpty();
    }
    return false;
}
Also used : Parameter(org.mapstruct.ap.internal.model.common.Parameter) HashSet(java.util.HashSet)

Example 3 with Parameter

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

the class MappingMethod method getImportTypes.

@Override
public Set<Type> getImportTypes() {
    Set<Type> types = new HashSet<Type>();
    for (Parameter param : parameters) {
        types.addAll(param.getType().getImportTypes());
    }
    types.addAll(getReturnType().getImportTypes());
    for (Type type : thrownTypes) {
        types.addAll(type.getImportTypes());
    }
    return types;
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) Parameter(org.mapstruct.ap.internal.model.common.Parameter) HashSet(java.util.HashSet)

Example 4 with Parameter

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

the class MethodRetrievalProcessor method retrieveLifecycleMethodsFromContext.

private ParameterProvidedMethods retrieveLifecycleMethodsFromContext(List<Parameter> contextParameters, TypeElement mapperToImplement, MapperConfiguration mapperConfig) {
    ParameterProvidedMethods.Builder builder = ParameterProvidedMethods.builder();
    for (Parameter contextParam : contextParameters) {
        if (contextParam.getType().isPrimitive()) {
            continue;
        }
        List<SourceMethod> contextParamMethods = retrieveMethods(contextParam.getType().getTypeElement(), mapperToImplement, mapperConfig, Collections.<SourceMethod>emptyList());
        List<SourceMethod> lifecycleMethods = new ArrayList<SourceMethod>(contextParamMethods.size());
        for (SourceMethod sourceMethod : contextParamMethods) {
            if (sourceMethod.isLifecycleCallbackMethod()) {
                lifecycleMethods.add(sourceMethod);
            }
        }
        builder.addMethodsForParameter(contextParam, lifecycleMethods);
    }
    return builder.build();
}
Also used : ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) ArrayList(java.util.ArrayList) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 5 with Parameter

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

the class MethodRetrievalProcessor method checkParameterAndReturnType.

private boolean checkParameterAndReturnType(ExecutableElement method, List<Parameter> sourceParameters, Parameter targetParameter, List<Parameter> contextParameters, Type resultType, Type returnType, boolean containsTargetTypeParameter) {
    if (sourceParameters.isEmpty()) {
        messager.printMessage(method, Message.RETRIEVAL_NO_INPUT_ARGS);
        return false;
    }
    if (targetParameter != null && (sourceParameters.size() + contextParameters.size() + 1 != method.getParameters().size())) {
        messager.printMessage(method, Message.RETRIEVAL_DUPLICATE_MAPPING_TARGETS);
        return false;
    }
    if (isVoid(resultType)) {
        messager.printMessage(method, Message.RETRIEVAL_VOID_MAPPING_METHOD);
        return false;
    }
    if (returnType.getTypeMirror().getKind() != TypeKind.VOID && !resultType.isAssignableTo(returnType)) {
        messager.printMessage(method, Message.RETRIEVAL_NON_ASSIGNABLE_RESULTTYPE);
        return false;
    }
    for (Parameter sourceParameter : sourceParameters) {
        if (sourceParameter.getType().isTypeVar()) {
            messager.printMessage(method, Message.RETRIEVAL_TYPE_VAR_SOURCE);
            return false;
        }
    }
    Set<Type> contextParameterTypes = new HashSet<Type>();
    for (Parameter contextParameter : contextParameters) {
        if (!contextParameterTypes.add(contextParameter.getType())) {
            messager.printMessage(method, Message.RETRIEVAL_CONTEXT_PARAMS_WITH_SAME_TYPE);
            return false;
        }
    }
    if (returnType.isTypeVar() || resultType.isTypeVar()) {
        messager.printMessage(method, Message.RETRIEVAL_TYPE_VAR_RESULT);
        return false;
    }
    Type parameterType = sourceParameters.get(0).getType();
    if (parameterType.isIterableOrStreamType() && !resultType.isIterableOrStreamType()) {
        messager.printMessage(method, Message.RETRIEVAL_ITERABLE_TO_NON_ITERABLE);
        return false;
    }
    if (containsTargetTypeParameter) {
        messager.printMessage(method, Message.RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER);
        return false;
    }
    if (!parameterType.isIterableOrStreamType() && resultType.isIterableOrStreamType()) {
        messager.printMessage(method, Message.RETRIEVAL_NON_ITERABLE_TO_ITERABLE);
        return false;
    }
    if (parameterType.isPrimitive()) {
        messager.printMessage(method, Message.RETRIEVAL_PRIMITIVE_PARAMETER);
        return false;
    }
    if (resultType.isPrimitive()) {
        messager.printMessage(method, Message.RETRIEVAL_PRIMITIVE_RETURN);
        return false;
    }
    if (parameterType.isEnumType() && !resultType.isEnumType()) {
        messager.printMessage(method, Message.RETRIEVAL_ENUM_TO_NON_ENUM);
        return false;
    }
    if (!parameterType.isEnumType() && resultType.isEnumType()) {
        messager.printMessage(method, Message.RETRIEVAL_NON_ENUM_TO_ENUM);
        return false;
    }
    for (Type typeParameter : resultType.getTypeParameters()) {
        if (typeParameter.isTypeVar()) {
            messager.printMessage(method, Message.RETRIEVAL_TYPE_VAR_RESULT);
            return false;
        }
        if (typeParameter.isWildCardExtendsBound()) {
            messager.printMessage(method, Message.RETRIEVAL_WILDCARD_EXTENDS_BOUND_RESULT);
            return false;
        }
    }
    for (Type typeParameter : parameterType.getTypeParameters()) {
        if (typeParameter.isWildCardSuperBound()) {
            messager.printMessage(method, Message.RETRIEVAL_WILDCARD_SUPER_BOUND_SOURCE);
            return false;
        }
        if (typeParameter.isTypeVar()) {
            messager.printMessage(method, Message.RETRIEVAL_TYPE_VAR_SOURCE);
            return false;
        }
    }
    return true;
}
Also used : 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) HashSet(java.util.HashSet)

Aggregations

Parameter (org.mapstruct.ap.internal.model.common.Parameter)16 ArrayList (java.util.ArrayList)8 Type (org.mapstruct.ap.internal.model.common.Type)8 DeclaredType (javax.lang.model.type.DeclaredType)4 ExecutableType (javax.lang.model.type.ExecutableType)4 HashSet (java.util.HashSet)3 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)3 ParameterBinding (org.mapstruct.ap.internal.model.common.ParameterBinding)2 ParameterProvidedMethods (org.mapstruct.ap.internal.model.source.ParameterProvidedMethods)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 ArrayType (javax.lang.model.type.ArrayType)1 PrimitiveType (javax.lang.model.type.PrimitiveType)1 TypeMirror (javax.lang.model.type.TypeMirror)1 TypeVariable (javax.lang.model.type.TypeVariable)1 WildcardType (javax.lang.model.type.WildcardType)1 PropertyEntry.forSourceReference (org.mapstruct.ap.internal.model.source.PropertyEntry.forSourceReference)1