Example usage for org.springframework.core Conventions getVariableNameForReturnType

List of usage examples for org.springframework.core Conventions getVariableNameForReturnType

Introduction

In this page you can find the example usage for org.springframework.core Conventions getVariableNameForReturnType.

Prototype

public static String getVariableNameForReturnType(Method method, Class<?> resolvedType,
        @Nullable Object value) 

Source Link

Document

Determine the conventional variable name for the return type of the given method, taking the generic collection type, if any, into account, falling back on the given return value if the method declaration is not specific enough, e.g.

Usage

From source file:com.revolsys.ui.web.rest.interceptor.WebAnnotationMethodHandlerAdapter.java

protected final void addReturnValueAsModelAttribute(final Method handlerMethod, final Class<?> handlerType,
        final Object returnValue, final ExtendedModelMap implicitModel) {

    final ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
    String attrName = attr != null ? attr.value() : "";
    if ("".equals(attrName)) {
        final Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
        attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
    }//w  w  w  .  j av a2  s. c  o m
    implicitModel.addAttribute(attrName, returnValue);
}

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

public final Object invokeHandlerMethod(Method handlerMethod, Object handler, NativeWebRequest webRequest,
        ExtendedModelMap implicitModel) throws Exception {

    Method handlerMethodToInvoke = BridgeMethodResolver.findBridgedMethod(handlerMethod);
    try {/*  ww  w  .j ava2s  .c o m*/
        boolean debug = logger.isDebugEnabled();
        for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
            Object attrValue = this.sessionAttributeStore.retrieveAttribute(webRequest, attrName);
            if (attrValue != null) {
                implicitModel.addAttribute(attrName, attrValue);
            }
        }
        for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) {
            Method attributeMethodToInvoke = BridgeMethodResolver.findBridgedMethod(attributeMethod);
            Object[] args = resolveHandlerArguments(attributeMethodToInvoke, handler, webRequest,
                    implicitModel);
            if (debug) {
                logger.debug("Invoking model attribute method: " + attributeMethodToInvoke);
            }
            String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value();
            if (!"".equals(attrName) && implicitModel.containsAttribute(attrName)) {
                continue;
            }
            ReflectionUtils.makeAccessible(attributeMethodToInvoke);
            Object attrValue = attributeMethodToInvoke.invoke(handler, args);
            if ("".equals(attrName)) {
                Class resolvedType = GenericTypeResolver.resolveReturnType(attributeMethodToInvoke,
                        handler.getClass());
                attrName = Conventions.getVariableNameForReturnType(attributeMethodToInvoke, resolvedType,
                        attrValue);
            }
            if (!implicitModel.containsAttribute(attrName)) {
                implicitModel.addAttribute(attrName, attrValue);
            }
        }
        Object[] args = resolveHandlerArguments(handlerMethodToInvoke, handler, webRequest, implicitModel);
        if (debug) {
            logger.debug("Invoking request handler method: " + handlerMethodToInvoke);
        }
        ReflectionUtils.makeAccessible(handlerMethodToInvoke);
        return handlerMethodToInvoke.invoke(handler, args);
    } catch (IllegalStateException ex) {
        // Internal assertion failed (e.g. invalid signature):
        // throw exception with full handler method context...
        throw new HandlerMethodInvocationException(handlerMethodToInvoke, ex);
    } catch (InvocationTargetException ex) {
        // User-defined @ModelAttribute/@InitBinder/@RequestMapping method threw an exception...
        ReflectionUtils.rethrowException(ex.getTargetException());
        return null;
    }
}

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class handlerType, Object returnValue,
        ExtendedModelMap implicitModel) {

    ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
    String attrName = (attr != null ? attr.value() : "");
    if ("".equals(attrName)) {
        Class resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
        attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
    }//from  ww  w .j ava2s .  c  o m
    implicitModel.addAttribute(attrName, returnValue);
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

public final Object invokeHandlerMethod(Method handlerMethod, Object handler, NativeWebRequest webRequest,
        ExtendedModelMap implicitModel) throws Exception {

    Method handlerMethodToInvoke = BridgeMethodResolver.findBridgedMethod(handlerMethod);
    try {//from w w w  .  j a  v a 2s .  c o m
        boolean debug = logger.isDebugEnabled();
        for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
            Object attrValue = this.sessionAttributeStore.retrieveAttribute(webRequest, attrName);
            if (attrValue != null) {
                implicitModel.addAttribute(attrName, attrValue);
            }
        }
        for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) {
            Method attributeMethodToInvoke = BridgeMethodResolver.findBridgedMethod(attributeMethod);
            Object[] args = resolveHandlerArguments(attributeMethodToInvoke, handler, webRequest,
                    implicitModel);
            if (debug) {
                logger.debug("Invoking model attribute method: " + attributeMethodToInvoke);
            }
            String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value();
            if (!"".equals(attrName) && implicitModel.containsAttribute(attrName)) {
                continue;
            }
            ReflectionUtils.makeAccessible(attributeMethodToInvoke);
            Object attrValue = attributeMethodToInvoke.invoke(handler, args);
            if ("".equals(attrName)) {
                Class<?> resolvedType = GenericTypeResolver.resolveReturnType(attributeMethodToInvoke,
                        handler.getClass());
                attrName = Conventions.getVariableNameForReturnType(attributeMethodToInvoke, resolvedType,
                        attrValue);
            }
            if (!implicitModel.containsAttribute(attrName)) {
                implicitModel.addAttribute(attrName, attrValue);
            }
        }
        Object[] args = resolveHandlerArguments(handlerMethodToInvoke, handler, webRequest, implicitModel);
        if (debug) {
            logger.debug("Invoking request handler method: " + handlerMethodToInvoke);
        }
        ReflectionUtils.makeAccessible(handlerMethodToInvoke);
        return handlerMethodToInvoke.invoke(handler, args);
    } catch (IllegalStateException ex) {
        // Internal assertion failed (e.g. invalid signature):
        // throw exception with full handler method context...
        throw new HandlerMethodInvocationException(handlerMethodToInvoke, ex);
    } catch (InvocationTargetException ex) {
        // User-defined @ModelAttribute/@InitBinder/@RequestMapping method threw an exception...
        ReflectionUtils.rethrowException(ex.getTargetException());
        return null;
    }
}

From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java

protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
        Object returnValue, ExtendedModelMap implicitModel) {

    ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
    String attrName = (attr != null ? attr.value() : "");
    if ("".equals(attrName)) {
        Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
        attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
    }//ww w. j a v a  2  s . c  o  m
    implicitModel.addAttribute(attrName, returnValue);
}

From source file:org.springframework.web.method.annotation.ModelFactory.java

/**
 * Derive the model attribute name for the given return value based on:
 * <ol>//  www.j a va 2 s .c  o m
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
    ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
    if (ann != null && StringUtils.hasText(ann.value())) {
        return ann.value();
    } else {
        Method method = returnType.getMethod();
        Assert.state(method != null, "No handler method");
        Class<?> containingClass = returnType.getContainingClass();
        Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
        return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
    }
}