Search in sources :

Example 1 with MethodParameters

use of org.springframework.hateoas.core.MethodParameters in project spring-data-commons by spring-projects.

the class PagedResourcesAssemblerArgumentResolver method findMatchingPageableParameter.

/**
 * Returns finds the {@link MethodParameter} for a {@link Pageable} instance matching the given
 * {@link MethodParameter} requesting a {@link PagedResourcesAssembler}.
 *
 * @param parameter must not be {@literal null}.
 * @return
 */
@Nullable
private static MethodParameter findMatchingPageableParameter(MethodParameter parameter) {
    MethodParameters parameters = new MethodParameters(parameter.getMethod());
    List<MethodParameter> pageableParameters = parameters.getParametersOfType(Pageable.class);
    Qualifier assemblerQualifier = parameter.getParameterAnnotation(Qualifier.class);
    if (pageableParameters.isEmpty()) {
        return null;
    }
    if (pageableParameters.size() == 1) {
        MethodParameter pageableParameter = pageableParameters.get(0);
        MethodParameter matchingParameter = returnIfQualifiersMatch(pageableParameter, assemblerQualifier);
        if (matchingParameter == null) {
            LOGGER.info(SUPERFLOUS_QUALIFIER, PagedResourcesAssembler.class.getSimpleName(), Pageable.class.getName());
        }
        return pageableParameter;
    }
    if (assemblerQualifier == null) {
        throw new IllegalStateException(PARAMETER_AMBIGUITY);
    }
    for (MethodParameter pageableParameter : pageableParameters) {
        MethodParameter matchingParameter = returnIfQualifiersMatch(pageableParameter, assemblerQualifier);
        if (matchingParameter != null) {
            return matchingParameter;
        }
    }
    throw new IllegalStateException(PARAMETER_AMBIGUITY);
}
Also used : Pageable(org.springframework.data.domain.Pageable) Qualifier(org.springframework.beans.factory.annotation.Qualifier) MethodParameters(org.springframework.hateoas.core.MethodParameters) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable)

Aggregations

Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 MethodParameter (org.springframework.core.MethodParameter)1 Pageable (org.springframework.data.domain.Pageable)1 MethodParameters (org.springframework.hateoas.core.MethodParameters)1 Nullable (org.springframework.lang.Nullable)1