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