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;
}
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));
}
}
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();
}
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;
}
}
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();
}
Aggregations