Search in sources :

Example 1 with ParameterProvidedMethods

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

use of org.mapstruct.ap.internal.model.source.ParameterProvidedMethods in project mapstruct by mapstruct.

the class ObjectFactoryMethodResolver method getAllAvailableMethods.

private static List<SourceMethod> getAllAvailableMethods(Method method, List<SourceMethod> sourceModelMethods) {
    ParameterProvidedMethods contextProvidedMethods = method.getContextProvidedMethods();
    if (contextProvidedMethods.isEmpty()) {
        return sourceModelMethods;
    }
    List<SourceMethod> methodsProvidedByParams = contextProvidedMethods.getAllProvidedMethodsInParameterOrder(method.getContextParameters());
    List<SourceMethod> availableMethods = new ArrayList<>(methodsProvidedByParams.size() + sourceModelMethods.size());
    for (SourceMethod methodProvidedByParams : methodsProvidedByParams) {
        // add only methods from context that do have the @ObjectFactory annotation
        if (methodProvidedByParams.hasObjectFactoryAnnotation()) {
            availableMethods.add(methodProvidedByParams);
        }
    }
    availableMethods.addAll(sourceModelMethods);
    return availableMethods;
}
Also used : ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) ArrayList(java.util.ArrayList) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 3 with ParameterProvidedMethods

use of org.mapstruct.ap.internal.model.source.ParameterProvidedMethods in project mapstruct by mapstruct.

the class MethodRetrievalProcessor method getMethodRequiringImplementation.

private SourceMethod getMethodRequiringImplementation(ExecutableType methodType, ExecutableElement method, List<Parameter> parameters, boolean containsTargetTypeParameter, MapperOptions mapperOptions, 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 = retrieveContextProvidedMethods(contextParameters, mapperToImplement, mapperOptions);
    BeanMappingOptions beanMappingOptions = BeanMappingOptions.getInstanceOn(BeanMappingGem.instanceOn(method), mapperOptions, method, messager, typeUtils, typeFactory);
    RepeatableMappings repeatableMappings = new RepeatableMappings();
    Set<MappingOptions> mappingOptions = repeatableMappings.getMappings(method, beanMappingOptions);
    IterableMappingOptions iterableMappingOptions = IterableMappingOptions.fromGem(IterableMappingGem.instanceOn(method), mapperOptions, method, messager, typeUtils);
    MapMappingOptions mapMappingOptions = MapMappingOptions.fromGem(MapMappingGem.instanceOn(method), mapperOptions, method, messager, typeUtils);
    EnumMappingOptions enumMappingOptions = EnumMappingOptions.getInstanceOn(method, mapperOptions, enumTransformationStrategies, messager);
    // We want to get as much error reporting as possible.
    // If targetParameter is not null it means we have an update method
    SubclassValidator subclassValidator = new SubclassValidator(messager, typeUtils);
    Set<SubclassMappingOptions> subclassMappingOptions = getSubclassMappings(sourceParameters, targetParameter != null ? null : resultType, method, beanMappingOptions, subclassValidator);
    return new SourceMethod.Builder().setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setMapper(mapperOptions).setBeanMappingOptions(beanMappingOptions).setMappingOptions(mappingOptions).setIterableMappingOptions(iterableMappingOptions).setMapMappingOptions(mapMappingOptions).setValueMappingOptionss(getValueMappings(method)).setEnumMappingOptions(enumMappingOptions).setSubclassMappings(subclassMappingOptions).setSubclassValidator(subclassValidator).setTypeUtils(typeUtils).setTypeFactory(typeFactory).setPrototypeMethods(prototypeMethods).setContextProvidedMethods(contextProvidedMethods).setVerboseLogging(options.isVerbose()).build();
}
Also used : ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) SubclassValidator(org.mapstruct.ap.internal.model.source.SubclassValidator) BeanMappingOptions(org.mapstruct.ap.internal.model.source.BeanMappingOptions) EnumMappingOptions(org.mapstruct.ap.internal.model.source.EnumMappingOptions) SubclassMappingOptions(org.mapstruct.ap.internal.model.source.SubclassMappingOptions) MappingOptions(org.mapstruct.ap.internal.model.source.MappingOptions) BeanMappingOptions(org.mapstruct.ap.internal.model.source.BeanMappingOptions) MapMappingOptions(org.mapstruct.ap.internal.model.source.MapMappingOptions) ValueMappingOptions(org.mapstruct.ap.internal.model.source.ValueMappingOptions) IterableMappingOptions(org.mapstruct.ap.internal.model.source.IterableMappingOptions) MapMappingOptions(org.mapstruct.ap.internal.model.source.MapMappingOptions) IterableMappingOptions(org.mapstruct.ap.internal.model.source.IterableMappingOptions) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType) SubclassMappingOptions(org.mapstruct.ap.internal.model.source.SubclassMappingOptions) EnumMappingOptions(org.mapstruct.ap.internal.model.source.EnumMappingOptions) Parameter(org.mapstruct.ap.internal.model.common.Parameter) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 4 with ParameterProvidedMethods

use of org.mapstruct.ap.internal.model.source.ParameterProvidedMethods in project mapstruct by mapstruct.

the class LifecycleCallbackFactory method getAllAvailableMethods.

private static List<SourceMethod> getAllAvailableMethods(Method method, List<SourceMethod> sourceModelMethods) {
    ParameterProvidedMethods contextProvidedMethods = method.getContextProvidedMethods();
    if (contextProvidedMethods.isEmpty()) {
        return sourceModelMethods;
    }
    List<SourceMethod> methodsProvidedByParams = contextProvidedMethods.getAllProvidedMethodsInParameterOrder(method.getContextParameters());
    List<SourceMethod> availableMethods = new ArrayList<SourceMethod>(methodsProvidedByParams.size() + sourceModelMethods.size());
    availableMethods.addAll(methodsProvidedByParams);
    availableMethods.addAll(sourceModelMethods);
    return availableMethods;
}
Also used : ParameterProvidedMethods(org.mapstruct.ap.internal.model.source.ParameterProvidedMethods) ArrayList(java.util.ArrayList) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod)

Example 5 with ParameterProvidedMethods

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

ParameterProvidedMethods (org.mapstruct.ap.internal.model.source.ParameterProvidedMethods)8 SourceMethod (org.mapstruct.ap.internal.model.source.SourceMethod)7 ArrayList (java.util.ArrayList)6 Parameter (org.mapstruct.ap.internal.model.common.Parameter)4 DeclaredType (javax.lang.model.type.DeclaredType)2 ExecutableType (javax.lang.model.type.ExecutableType)2 Type (org.mapstruct.ap.internal.model.common.Type)2 BeanMappingOptions (org.mapstruct.ap.internal.model.source.BeanMappingOptions)1 EnumMappingOptions (org.mapstruct.ap.internal.model.source.EnumMappingOptions)1 IterableMappingOptions (org.mapstruct.ap.internal.model.source.IterableMappingOptions)1 MapMappingOptions (org.mapstruct.ap.internal.model.source.MapMappingOptions)1 MappingOptions (org.mapstruct.ap.internal.model.source.MappingOptions)1 SubclassMappingOptions (org.mapstruct.ap.internal.model.source.SubclassMappingOptions)1 SubclassValidator (org.mapstruct.ap.internal.model.source.SubclassValidator)1 ValueMappingOptions (org.mapstruct.ap.internal.model.source.ValueMappingOptions)1