Example usage for org.springframework.core MethodParameter getParameterAnnotation

List of usage examples for org.springframework.core MethodParameter getParameterAnnotation

Introduction

In this page you can find the example usage for org.springframework.core MethodParameter getParameterAnnotation.

Prototype

@SuppressWarnings("unchecked")
@Nullable
public <A extends Annotation> A getParameterAnnotation(Class<A> annotationType) 

Source Link

Document

Return the parameter annotation of the given type, if available.

Usage

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_defaultParameterName() {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod3"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });

    assertThat(resolver.buildSpecification(req, param.getParameterAnnotation(Spec.class))).isNull();
    ;/*ww  w.ja  va 2s .  c o  m*/
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_customParameterName() {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });

    assertThat(resolver.buildSpecification(req, param.getParameterAnnotation(Spec.class))).isNull();
    ;//from w  w  w .  j ava2s .c  o  m
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void skipsEmptyWebParameterValues() {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod3"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("theParameter")).thenReturn(new String[] { "value1", "" });

    Specification<Object> resolved = resolver.buildSpecification(req, param.getParameterAnnotation(Spec.class));

    assertThat(resolved).isEqualTo(new EqualEnum<>("thePath", new String[] { "value1" }));
}

From source file:cn.guoyukun.spring.jpa.web.bind.method.annotation.PageableMethodArgumentResolver.java

private PageableDefaults getPageableDefaults(MethodParameter parameter) {
    //?/*w  w w .j a  v  a 2  s.  c  o  m*/
    PageableDefaults pageableDefaults = parameter.getParameterAnnotation(PageableDefaults.class);
    //?
    if (pageableDefaults == null) {
        pageableDefaults = parameter.getMethodAnnotation(PageableDefaults.class);
    }
    return pageableDefaults;
}

From source file:com.epam.ta.reportportal.ws.resolver.SortArgumentResolver.java

@Override
public Sort resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {

    /*/*from  ww w.j av a  2  s  .co m*/
       * Resolve sort argument in default way
     */
    Sort defaultSort = super.resolveArgument(parameter, mavContainer, webRequest, binderFactory);

    /*
       * Try to find parameter to be sorted in internal-external mapping
     */
    if (null != parameter.getParameterAnnotation(SortFor.class) && null != defaultSort) {

        Class<?> domainModelType = parameter.getParameterAnnotation(SortFor.class).value();
        CriteriaMap<?> map = criteriaMapFactory.getCriteriaMap(domainModelType);

        /*
        * Build Sort with search criteria from internal domain model
        */
        return new Sort(StreamSupport.stream(defaultSort.spliterator(), false).map(order -> {
            Optional<CriteriaHolder> criteriaHolder = map.getCriteriaHolderUnchecked(order.getProperty());

            BusinessRule.expect(criteriaHolder, Preconditions.IS_PRESENT)
                    .verify(ErrorType.INCORRECT_SORTING_PARAMETERS, order.getProperty());
            return new Order(order.getDirection(), criteriaHolder.get().getQueryCriteria());
        }).collect(toList()));
    } else {
        /*
         * Return default sort in case there are no SortFor annotation
         */
        return defaultSort;
    }

}

From source file:de.escalon.hypermedia.action.ActionInputParameter.java

/**
 * Creates input parameter descriptor./* w ww .j a  v  a  2s.co m*/
 *
 * @param methodParameter   to describe
 * @param value             used during sample invocation
 * @param conversionService to apply to value
 */
public ActionInputParameter(MethodParameter methodParameter, Object value,
        ConversionService conversionService) {
    this.methodParameter = methodParameter;
    this.value = value;
    this.requestBody = methodParameter.getParameterAnnotation(RequestBody.class);
    this.requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
    this.pathVariable = methodParameter.getParameterAnnotation(PathVariable.class);
    // always determine input constraints,
    // might be a nested property which is neither requestBody, requestParam nor pathVariable
    this.inputAnnotation = methodParameter.getParameterAnnotation(Input.class);
    if (inputAnnotation != null) {
        putInputConstraint(MIN, Integer.MIN_VALUE, inputAnnotation.min());
        putInputConstraint(MAX, Integer.MAX_VALUE, inputAnnotation.max());
        putInputConstraint(MIN_LENGTH, Integer.MIN_VALUE, inputAnnotation.minLength());
        putInputConstraint(MAX_LENGTH, Integer.MAX_VALUE, inputAnnotation.maxLength());
        putInputConstraint(STEP, 0, inputAnnotation.step());
        putInputConstraint(PATTERN, "", inputAnnotation.pattern());
    }

    this.conversionService = conversionService;
    this.typeDescriptor = TypeDescriptor.nested(methodParameter, 0);
}

From source file:de.escalon.hypermedia.spring.ActionInputParameter.java

/**
 * Creates action input parameter.//from www  . j a v  a 2  s  .c  o  m
 *
 * @param methodParameter
 *         to describe
 * @param value
 *         used during sample invocation
 * @param conversionService
 *         to apply to value
 */
public ActionInputParameter(MethodParameter methodParameter, Object value,
        ConversionService conversionService) {
    this.methodParameter = methodParameter;
    this.value = value;
    this.requestBody = methodParameter.getParameterAnnotation(RequestBody.class);
    this.requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
    this.pathVariable = methodParameter.getParameterAnnotation(PathVariable.class);
    this.requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
    // always determine input constraints,
    // might be a nested property which is neither requestBody, requestParam nor pathVariable
    this.inputAnnotation = methodParameter.getParameterAnnotation(Input.class);
    if (inputAnnotation != null) {
        putInputConstraint(Input.MIN, Integer.MIN_VALUE, inputAnnotation.min());
        putInputConstraint(Input.MAX, Integer.MAX_VALUE, inputAnnotation.max());
        putInputConstraint(Input.MIN_LENGTH, Integer.MIN_VALUE, inputAnnotation.minLength());
        putInputConstraint(Input.MAX_LENGTH, Integer.MAX_VALUE, inputAnnotation.maxLength());
        putInputConstraint(Input.STEP, 0, inputAnnotation.step());
        putInputConstraint(Input.PATTERN, "", inputAnnotation.pattern());
    }
    this.conversionService = conversionService;
    this.typeDescriptor = TypeDescriptor.nested(methodParameter, 0);
}

From source file:org.zht.framework.web.bind.resolver.FormModelMethodArgumentResolver.java

private ServletRequest prepareServletRequest(Object target, NativeWebRequest request,
        MethodParameter parameter) {

    String modelPrefixName = parameter.getParameterAnnotation(FormModel.class).value();

    HttpServletRequest nativeRequest = (HttpServletRequest) request.getNativeRequest();
    MultipartRequest multipartRequest = WebUtils.getNativeRequest(nativeRequest, MultipartRequest.class);

    MockHttpServletRequest mockRequest = null;
    if (multipartRequest != null) {
        MockMultipartHttpServletRequest mockMultipartRequest = new MockMultipartHttpServletRequest();
        for (MultipartFile file : multipartRequest.getFileMap().values()) {
            mockMultipartRequest.addFile(
                    new MultipartFileWrapper(getNewParameterName(file.getName(), modelPrefixName), file));
        }/*from  w ww  . j av  a 2s . c  o m*/
        mockRequest = mockMultipartRequest;
    } else {
        mockRequest = new MockHttpServletRequest();
    }

    for (Entry<String, String> entry : getUriTemplateVariables(request).entrySet()) {
        String parameterName = entry.getKey();
        String value = entry.getValue();
        if (isFormModelAttribute(parameterName, modelPrefixName)) {
            mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
        }
    }

    for (Object parameterEntry : nativeRequest.getParameterMap().entrySet()) {
        Entry<String, String[]> entry = (Entry<String, String[]>) parameterEntry;
        String parameterName = entry.getKey();
        String[] value = entry.getValue();
        if (isFormModelAttribute(parameterName, modelPrefixName)) {
            mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
        }
    }

    return mockRequest;
}

From source file:de.escalon.hypermedia.spring.SpringActionInputParameter.java

/**
 * Creates action input parameter.//from   ww  w.j  a v  a2 s.  co m
 *
 * @param methodParameter
 *         to describe
 * @param value
 *         used during sample invocation
 * @param conversionService
 *         to apply to value
 */
public SpringActionInputParameter(MethodParameter methodParameter, Object value,
        ConversionService conversionService) {
    this.methodParameter = methodParameter;
    this.value = value;
    this.requestBody = methodParameter.getParameterAnnotation(RequestBody.class);
    this.requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
    this.pathVariable = methodParameter.getParameterAnnotation(PathVariable.class);
    this.requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
    // always determine input constraints,
    // might be a nested property which is neither requestBody, requestParam nor pathVariable
    this.inputAnnotation = methodParameter.getParameterAnnotation(Input.class);
    if (inputAnnotation != null) {
        putInputConstraint(Input.MIN, Integer.MIN_VALUE, inputAnnotation.min());
        putInputConstraint(Input.MAX, Integer.MAX_VALUE, inputAnnotation.max());
        putInputConstraint(Input.MIN_LENGTH, Integer.MIN_VALUE, inputAnnotation.minLength());
        putInputConstraint(Input.MAX_LENGTH, Integer.MAX_VALUE, inputAnnotation.maxLength());
        putInputConstraint(Input.STEP, 0, inputAnnotation.step());
        putInputConstraint(Input.PATTERN, "", inputAnnotation.pattern());
    }
    this.conversionService = conversionService;
    this.typeDescriptor = TypeDescriptor.nested(methodParameter, 0);
}

From source file:com.mmj.app.common.component.FormModelMethodArgumentResolver.java

private ServletRequest prepareServletRequest(Object target, NativeWebRequest request,
        MethodParameter parameter) {

    String modelPrefixName = parameter.getParameterAnnotation(FormBean.class).value();

    HttpServletRequest nativeRequest = (HttpServletRequest) request.getNativeRequest();
    MultipartRequest multipartRequest = WebUtils.getNativeRequest(nativeRequest, MultipartRequest.class);

    MockHttpServletRequest mockRequest = null;
    if (multipartRequest != null) {
        MockMultipartHttpServletRequest mockMultipartRequest = new MockMultipartHttpServletRequest();
        for (MultipartFile file : multipartRequest.getFileMap().values()) {
            mockMultipartRequest.addFile(
                    new MultipartFileWrapper(getNewParameterName(file.getName(), modelPrefixName), file));
        }// ww  w .ja va2 s  .c  om
        mockRequest = mockMultipartRequest;
    } else {
        mockRequest = new MockHttpServletRequest();
    }

    for (Entry<String, String> entry : getUriTemplateVariables(request).entrySet()) {
        String parameterName = entry.getKey();
        String value = entry.getValue();
        if (isFormModelAttribute(parameterName, modelPrefixName)) {
            mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
        }
    }

    for (Object parameterEntry : nativeRequest.getParameterMap().entrySet()) {
        Entry<String, String[]> entry = (Entry<String, String[]>) parameterEntry;
        String parameterName = entry.getKey();
        String[] value = entry.getValue();
        if (isFormModelAttribute(parameterName, modelPrefixName)) {
            mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value);
        }
    }

    return mockRequest;
}