Search in sources :

Example 6 with Type

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

the class QualifierSelector method getQualifierAnnotationMirrors.

private Set<AnnotationMirror> getQualifierAnnotationMirrors(Method candidate) {
    // retrieve annotations
    Set<AnnotationMirror> qualiferAnnotations = new HashSet<>();
    // first from the method itself
    SourceMethod candidateSM = (SourceMethod) candidate;
    List<? extends AnnotationMirror> methodAnnotations = candidateSM.getExecutable().getAnnotationMirrors();
    for (AnnotationMirror methodAnnotation : methodAnnotations) {
        addOnlyWhenQualifier(qualiferAnnotations, methodAnnotation);
    }
    // then from the mapper (if declared)
    Type mapper = candidate.getDeclaringMapper();
    if (mapper != null) {
        List<? extends AnnotationMirror> mapperAnnotations = mapper.getTypeElement().getAnnotationMirrors();
        for (AnnotationMirror mapperAnnotation : mapperAnnotations) {
            addOnlyWhenQualifier(qualiferAnnotations, mapperAnnotation);
        }
    }
    return qualiferAnnotations;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) SourceMethod(org.mapstruct.ap.internal.model.source.SourceMethod) HashSet(java.util.HashSet)

Example 7 with Type

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

the class AnnotationBasedComponentModelProcessor method removeDuplicateAnnotations.

/**
 * Removes duplicate constructor parameter annotations. If an annotation is already present on the constructor, it
 * does not have be defined on the constructor parameter, too. For example, for CDI, the javax.inject.Inject
 * annotation is on the constructor and does not need to be on the constructor parameters.
 *
 * @param annotationMapperReferences annotations to annotate the constructor parameter with
 * @param mapperReferenceAnnotations annotations to annotate the constructor with
 */
private void removeDuplicateAnnotations(List<AnnotationMapperReference> annotationMapperReferences, List<Annotation> mapperReferenceAnnotations) {
    ListIterator<AnnotationMapperReference> mapperReferenceIterator = annotationMapperReferences.listIterator();
    Set<Type> mapperReferenceAnnotationsTypes = new HashSet<>();
    for (Annotation annotation : mapperReferenceAnnotations) {
        mapperReferenceAnnotationsTypes.add(annotation.getType());
    }
    while (mapperReferenceIterator.hasNext()) {
        AnnotationMapperReference annotationMapperReference = mapperReferenceIterator.next();
        mapperReferenceIterator.remove();
        List<Annotation> qualifiers = new ArrayList<>();
        for (Annotation annotation : annotationMapperReference.getAnnotations()) {
            if (!mapperReferenceAnnotationsTypes.contains(annotation.getType())) {
                qualifiers.add(annotation);
            }
        }
        mapperReferenceIterator.add(annotationMapperReference.withNewAnnotations(qualifiers));
    }
}
Also used : AnnotationMapperReference(org.mapstruct.ap.internal.model.AnnotationMapperReference) Type(org.mapstruct.ap.internal.model.common.Type) ArrayList(java.util.ArrayList) Annotation(org.mapstruct.ap.internal.model.Annotation) HashSet(java.util.HashSet)

Example 8 with Type

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

the class MethodRetrievalProcessor method getReferencedMethod.

private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType, ExecutableElement method, TypeElement mapperToImplement, List<Parameter> parameters) {
    Type returnType = typeFactory.getReturnType(methodType);
    List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
    Type usedMapperAsType = typeFactory.getType(usedMapper);
    Type mapperToImplementAsType = typeFactory.getType(mapperToImplement);
    if (!mapperToImplementAsType.canAccess(usedMapperAsType, method)) {
        return null;
    }
    Type definingType = typeFactory.getType(method.getEnclosingElement().asType());
    return new SourceMethod.Builder().setDeclaringMapper(usedMapper.equals(mapperToImplement) ? null : usedMapperAsType).setDefininingType(definingType).setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setTypeUtils(typeUtils).setTypeFactory(typeFactory).setVerboseLogging(options.isVerbose()).build();
}
Also used : Type(org.mapstruct.ap.internal.model.common.Type) DeclaredType(javax.lang.model.type.DeclaredType) ExecutableType(javax.lang.model.type.ExecutableType)

Example 9 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, MapperOptions mapperOptions, 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, mapperOptions, prototypeMethods, mapperToImplement);
    } else // otherwise add reference to existing mapper method
    if (isValidReferencedMethod(parameters) || isValidFactoryMethod(method, parameters, returnType) || isValidLifecycleCallbackMethod(method) || isValidPresenceCheckMethod(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 10 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, 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)

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