Example usage for org.springframework.core MethodParameter getParameterAnnotations

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

Introduction

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

Prototype

public Annotation[] getParameterAnnotations() 

Source Link

Document

Return the annotations associated with the specific method/constructor parameter.

Usage

From source file:springfox.documentation.spring.web.readers.parameter.ParameterRequiredReader.java

private Boolean getAnnotatedRequired(MethodParameter methodParameter) {
    Set<Boolean> requiredSet = new HashSet<Boolean>();
    Annotation[] methodAnnotations = methodParameter.getParameterAnnotations();

    // when the type is Optional, the required property of @RequestParam/@RequestHeader doesn't matter,
    // since the value is always a non-null Optional after conversion
    boolean optional = isOptional(methodParameter);

    for (Annotation annotation : methodAnnotations) {
        if (annotation instanceof RequestParam) {
            requiredSet.add(!optional && isRequired((RequestParam) annotation));
        } else if (annotation instanceof RequestHeader) {
            requiredSet.add(!optional && ((RequestHeader) annotation).required());
        } else if (annotation instanceof PathVariable) {
            requiredSet.add(true);/* ww  w .  j  a  va2 s . c  o m*/
        } else if (annotation instanceof RequestBody) {
            requiredSet.add(!optional && ((RequestBody) annotation).required());
        } else if (annotation instanceof RequestPart) {
            requiredSet.add(!optional && ((RequestPart) annotation).required());
        }
    }
    return requiredSet.contains(true);
}

From source file:com.nec.harvest.resolver.AuthenticatedArgumentResolver.java

/**
 * Obtains the specified {@link java.lang.annotation.Annotation} on the specified {@link org.springframework.core.MethodParameter}.
 *
 * @param annotationClass the class of the {@link java.lang.annotation.Annotation} to find on the {@link org.springframework.core.MethodParameter}
 * @param parameter the {@link org.springframework.core.MethodParameter} to search for an {@link java.lang.annotation.Annotation}
 * @return the {@link java.lang.annotation.Annotation} that was found or null.
 *///from w  ww.j a  v  a  2 s.c om
private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) {
    T annotation = parameter.getParameterAnnotation(annotationClass);
    if (annotation != null) {
        return annotation;
    }

    Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
    for (Annotation toSearch : annotationsToSearch) {
        annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
        if (annotation != null) {
            return annotation;
        }
    }
    return null;
}

From source file:curly.commons.github.GitHubAuthenticationMethodHandler.java

private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) {
    T annotation = parameter.getParameterAnnotation(annotationClass);
    if (annotation != null) {
        return annotation;
    }/*from   w w w.  ja  va  2 s . co  m*/
    Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
    for (Annotation toSearch : annotationsToSearch) {
        annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
        if (annotation != null) {
            return annotation;
        }
    }
    return null;
}

From source file:ch.rasc.wampspring.method.PayloadArgumentResolver.java

protected void validate(Message<?> message, MethodParameter parameter, Object target) {
    if (this.validator == null) {
        return;/*from  w ww  . j av a  2s . co  m*/
    }
    for (Annotation ann : parameter.getParameterAnnotations()) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann);
            Object[] validationHints = hints instanceof Object[] ? (Object[]) hints : new Object[] { hints };
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target,
                    getParameterName(parameter));
            if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
                ((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
            } else {
                this.validator.validate(target, bindingResult);
            }
            if (bindingResult.hasErrors()) {
                throw new MethodArgumentNotValidException(message, parameter, bindingResult);
            }
            break;
        }
    }
}

From source file:at.fwd.swagger.spring.demo.plugins.MyParameterRequiredReader.java

private Boolean getAnnotatedRequired(MethodParameter methodParameter) {
    System.out.println("*** methodParameter: " + methodParameter.getParameterName());

    Set<Boolean> requiredSet = new HashSet<Boolean>();
    Annotation[] methodAnnotations = methodParameter.getParameterAnnotations();

    // when the type is Optional, the required property of @RequestParam/@RequestHeader doesn't matter,
    // since the value is always a non-null Optional after conversion
    boolean optional = isOptional(methodParameter);

    for (Annotation annotation : methodAnnotations) {

        if (annotation instanceof RequestParam) {
            requiredSet.add(!optional && isRequired((RequestParam) annotation));
        } else if (annotation instanceof RequestHeader) {
            requiredSet.add(!optional && ((RequestHeader) annotation).required());
        } else if (annotation instanceof PathVariable) {
            requiredSet.add(true);//  w  ww.  j  a  v  a2 s  . c  o m
        } else if (annotation instanceof RequestBody) {
            requiredSet.add(!optional && ((RequestBody) annotation).required());
        } else if (annotation instanceof RequestPart) {
            requiredSet.add(!optional && ((RequestPart) annotation).required());
        } else if (annotation instanceof NotNull) {
            System.out.println("*** FOUND NOTNULL ANNOTATION");
            requiredSet.add(true);
        }
    }

    System.out.println("required: " + requiredSet.contains(true));

    return requiredSet.contains(true);
}

From source file:org.synyx.hades.extensions.web.PageableArgumentResolver.java

/**
 * Resolves the prefix to use to bind properties from. Will prepend a
 * possible {@link Qualifier} if available or return the configured prefix
 * otherwise.//from  ww  w  .  j  ava  2  s. c  om
 * 
 * @param parameter
 * @return
 */
private String getPrefix(MethodParameter parameter) {

    for (Annotation annotation : parameter.getParameterAnnotations()) {
        if (annotation instanceof Qualifier) {
            return new StringBuilder(((Qualifier) annotation).value()).append("_").append(prefix).toString();
        }
    }

    return prefix;
}

From source file:org.synyx.hades.extensions.web.PageableArgumentResolver.java

private Pageable getDefaultFromAnnotationOrFallback(MethodParameter methodParameter) {

    // search for PageableDefaults annotation
    for (Annotation annotation : methodParameter.getParameterAnnotations()) {
        if (annotation instanceof PageableDefaults) {
            PageableDefaults defaults = (PageableDefaults) annotation;
            // +1 is because we substract 1 later
            return new PageRequest(defaults.pageNumber() + 1, defaults.value());
        }/*ww w .j  ava 2s  . co  m*/
    }

    // Construct request with fallback request to ensure sensible
    // default values. Create fresh copy as Spring will manipulate the
    // instance under the covers
    return new PageRequest(fallbackPagable.getPageNumber(), fallbackPagable.getPageSize(),
            fallbackPagable.getSort());
}

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

private boolean validateParameter(MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation annot : annotations) {
        if (annot.annotationType().getSimpleName().startsWith("Valid")) {
            return true;
        }//from  w  ww.j  a va2s.  c  o m
    }

    return false;
}

From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java

/**
 * Validate the model attribute if applicable.
 * <p>//from  w  ww . j ava 2s  .c  o m
 * The default implementation checks for {@code @javax.validation.Valid}.
 * 
 * @param binder the DataBinder to be used
 * @param parameter the method parameter
 */
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    Annotation[] annotations = parameter.getParameterAnnotations();
    for (Annotation annot : annotations) {
        if (annot.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = AnnotationUtils.getValue(annot);
            binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
        }
    }
}

From source file:org.focusns.common.web.widget.mvc.method.WidgetModelAttributeMethodProcessor.java

@Override
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
    ///*  w ww. ja  va2 s  . com*/
    WebRequest webRequest = webRequestLocal.get();
    String groupsStr = webRequest.getParameter("groups");
    if (StringUtils.hasText(groupsStr)) {
        List<Object> hintList = new ArrayList<Object>();
        String[] groups = StringUtils.commaDelimitedListToStringArray(groupsStr);
        for (String group : groups) {
            try {
                hintList.add(ClassUtils.forName(group, getClass().getClassLoader()));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //
        hintList.add(Default.class);
        //
        Annotation[] annotations = parameter.getParameterAnnotations();
        for (Annotation annot : annotations) {
            if (annot.annotationType().getSimpleName().startsWith("Valid")) {
                Object hints = hintList.toArray(new Object[hintList.size()]);
                binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            }
        }
    }
}