use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.
the class MapperCreationProcessor method getTemplateMappingOptions.
/**
* Returns the configuring forward method's options in case the given method is annotated with
* {@code @InheritConfiguration} and exactly one such configuring method can unambiguously be selected (as per the
* source/target type and optionally the name given via {@code @InheritConfiguration}). The method cannot be marked
* forward mapping itself (hence 'other'). And neither can it contain an {@code @InheritReverseConfiguration}
*/
private MappingOptions getTemplateMappingOptions(List<SourceMethod> rawMethods, SourceMethod method, List<SourceMethod> initializingMethods, MapperConfiguration mapperConfig) {
SourceMethod resultMethod = null;
InheritConfigurationPrism forwardPrism = InheritConfigurationPrism.getInstanceOn(method.getExecutable());
if (forwardPrism != null) {
List<SourceMethod> candidates = new ArrayList<SourceMethod>();
for (SourceMethod oneMethod : rawMethods) {
// method must be similar but not equal
if (method.canInheritFrom(oneMethod) && !(oneMethod.equals(method))) {
candidates.add(oneMethod);
}
}
String name = forwardPrism.name();
if (candidates.size() == 1) {
// no ambiguity: if no configuredBy is specified, or configuredBy specified and match
SourceMethod sourceMethod = first(candidates);
if (name.isEmpty()) {
resultMethod = sourceMethod;
} else if (sourceMethod.getName().equals(name)) {
resultMethod = sourceMethod;
} else {
reportErrorWhenNonMatchingName(sourceMethod, method, forwardPrism);
}
} else if (candidates.size() > 1) {
// ambiguity: find a matching method that matches configuredBy
List<SourceMethod> nameFilteredcandidates = new ArrayList<SourceMethod>();
for (SourceMethod candidate : candidates) {
if (candidate.getName().equals(name)) {
nameFilteredcandidates.add(candidate);
}
}
if (nameFilteredcandidates.size() == 1) {
resultMethod = first(nameFilteredcandidates);
} else if (nameFilteredcandidates.size() > 1) {
reportErrorWhenSeveralNamesMatch(nameFilteredcandidates, method, forwardPrism);
} else {
reportErrorWhenAmbigousMapping(candidates, method, forwardPrism);
}
}
}
return extractInitializedOptions(resultMethod, rawMethods, mapperConfig, initializingMethods);
}
use of org.mapstruct.ap.internal.model.source.SourceMethod 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();
}
use of org.mapstruct.ap.internal.model.source.SourceMethod 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).build();
}
use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.
the class XmlElementDeclSelector method getMatchingMethods.
@Override
public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod, List<SelectedMethod<T>> methods, List<Type> sourceTypes, Type targetType, SelectionCriteria criteria) {
List<SelectedMethod<T>> nameMatches = new ArrayList<SelectedMethod<T>>();
List<SelectedMethod<T>> scopeMatches = new ArrayList<SelectedMethod<T>>();
List<SelectedMethod<T>> nameAndScopeMatches = new ArrayList<SelectedMethod<T>>();
XmlElementRefInfo xmlElementRefInfo = findXmlElementRef(mappingMethod.getResultType(), criteria.getTargetPropertyName());
for (SelectedMethod<T> candidate : methods) {
if (!(candidate.getMethod() instanceof SourceMethod)) {
continue;
}
SourceMethod candidateMethod = (SourceMethod) candidate.getMethod();
XmlElementDeclPrism xmlElememtDecl = XmlElementDeclPrism.getInstanceOn(candidateMethod.getExecutable());
if (xmlElememtDecl == null) {
continue;
}
String name = xmlElememtDecl.name();
TypeMirror scope = xmlElememtDecl.scope();
boolean nameIsSetAndMatches = name != null && name.equals(xmlElementRefInfo.nameValue());
boolean scopeIsSetAndMatches = scope != null && typeUtils.isSameType(scope, xmlElementRefInfo.sourceType());
if (nameIsSetAndMatches) {
if (scopeIsSetAndMatches) {
nameAndScopeMatches.add(candidate);
} else {
nameMatches.add(candidate);
}
} else if (scopeIsSetAndMatches) {
scopeMatches.add(candidate);
}
}
if (nameAndScopeMatches.size() > 0) {
return nameAndScopeMatches;
} else if (scopeMatches.size() > 0) {
return scopeMatches;
} else if (nameMatches.size() > 0) {
return nameMatches;
} else {
return methods;
}
}
use of org.mapstruct.ap.internal.model.source.SourceMethod in project mapstruct by mapstruct.
the class LifecycleCallbackFactory method toLifecycleCallbackMethodRefs.
private static List<LifecycleCallbackMethodReference> toLifecycleCallbackMethodRefs(Method method, List<SelectedMethod<SourceMethod>> candidates, MappingBuilderContext ctx, Set<String> existingVariableNames) {
List<LifecycleCallbackMethodReference> result = new ArrayList<LifecycleCallbackMethodReference>();
for (SelectedMethod<SourceMethod> candidate : candidates) {
Parameter providingParameter = method.getContextProvidedMethods().getParameterForProvidedMethod(candidate.getMethod());
if (providingParameter != null) {
result.add(LifecycleCallbackMethodReference.forParameterProvidedMethod(candidate, providingParameter, method, existingVariableNames));
} else {
MapperReference mapperReference = findMapperReference(ctx.getMapperReferences(), candidate.getMethod());
result.add(LifecycleCallbackMethodReference.forMethodReference(candidate, mapperReference, method, existingVariableNames));
}
}
return result;
}
Aggregations