Example usage for org.springframework.core MethodParameter getDeclaringClass

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

Introduction

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

Prototype

public Class<?> getDeclaringClass() 

Source Link

Document

Return the class that declares the underlying Method or Constructor.

Usage

From source file:org.ngrinder.infra.spring.RemainedPathMethodArgumentResolver.java

/**
 * Get the request mapping annotation on the given parameter.
 * //from ww w .j a  va 2s  .co m
 * @param parameter
 *            parameter
 * @return {@link RequestMapping} annotation
 */
@SuppressWarnings("unchecked")
protected RequestMapping getDeclaringClassRequestMapping(MethodParameter parameter) {
    return (RequestMapping) parameter.getDeclaringClass().getAnnotation(RequestMapping.class);
}

From source file:com.ufukuzun.myth.dialect.handler.AjaxRequestResponseBodyReturnValueHandler.java

@Override
protected Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
        Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
    return jsonMessageConverter.read(targetType, parameter.getDeclaringClass(), inputMessage);
}

From source file:org.opentides.web.processor.FormBindMethodProcessor.java

/**
 * Resolve the argument from the model or if not found instantiate it with
 * its default. The model attribute is then populated with request values 
 * via data binding and validated. If no validation error, the model
 * is retrieved from database and merged with the submitted form.
 *   /*w ww  .  j a va2  s .  c o m*/
 * @throws BindException if data binding and validation result in an error
 * @throws Exception if WebDataBinder initialization fails.
 */
@SuppressWarnings("unchecked")
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception {

    FormBind annot = parameter.getParameterAnnotation(FormBind.class);
    Class<?> clazz = parameter.getDeclaringClass();
    String name = getName(annot, parameter);
    HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest();

    Method addForm = CacheUtil.getNewFormBindMethod(clazz);
    Object controller = beanFactory.getBean(parameter.getContainingClass());
    Object target = (addForm != null) ? addForm.invoke(controller, request)
            : BeanUtils.instantiateClass(parameter.getParameterType());
    MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap());
    WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name);
    if (binder.getTarget() != null) {
        binder.bind(mpvs);
        if (binder.getValidator() != null)
            binder.validate();
        if (binder.getBindingResult().hasErrors()) {
            throw new BindException(binder.getBindingResult());
        }
        String method = request.getMethod().toLowerCase();

        // id should be the last segment of the uri
        String uri = request.getRequestURI();
        String sid = uri.substring(uri.lastIndexOf("/") + 1);
        Long id = StringUtil.convertToLong(sid, 0);

        // if target extends BaseEntity and for update, link target to database record
        if (("put".equals(method) || "post".equals(method)) && id > 0
                && BaseEntity.class.isAssignableFrom(parameter.getParameterType())) {
            // now retrieve record from database for updating
            Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz);

            BaseEntity record = null;
            if (updateForm == null) {
                // no annotation, invoke from service
                Method getService = controller.getClass().getMethod("getService");
                if (getService == null) {
                    String message = "Cannot find method with @FormBind with update mode. "
                            + "Also, unable to find service associated to controller."
                            + "Please specify one that retrieves record from database.";
                    throw new InvalidImplementationException(message);
                }
                BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService
                        .invoke(controller);
                record = (BaseEntity) service.load(sid);
            } else {
                // with annotation, invoke annotation
                record = (BaseEntity) updateForm.invoke(controller, sid, request);
            }

            if (record != null) {
                WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name);
                updateBinder.bind(mpvs);
                mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel());
                return updateBinder.getTarget();
            } else {
                String message = "Unable to find " + parameter.getParameterType().getSimpleName() + " with id="
                        + sid + " for update.";
                throw new DataAccessException(message);
            }
        } else if ("post".equals(method)) {
            mavContainer.addAllAttributes(binder.getBindingResult().getModel());
            return binder.getTarget();
        } else {
            throw new InvalidImplementationException(
                    "@FormBind argument annotation can only be used on POST or PUT methods.");
        }
    }
    mavContainer.addAllAttributes(binder.getBindingResult().getModel());
    return binder.getTarget();
}