001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.mvc.method;
018
019import org.springframework.web.method.HandlerMethod;
020import org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy;
021
022/**
023 * A {@link org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy
024 * HandlerMethodMappingNamingStrategy} for {@code RequestMappingInfo}-based handler
025 * method mappings.
026 *
027 * If the {@code RequestMappingInfo} name attribute is set, its value is used.
028 * Otherwise the name is based on the capital letters of the class name,
029 * followed by "#" as a separator, and the method name. For example "TC#getFoo"
030 * for a class named TestController with method getFoo.
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.1
034 */
035public class RequestMappingInfoHandlerMethodMappingNamingStrategy
036                implements HandlerMethodMappingNamingStrategy<RequestMappingInfo> {
037
038        /** Separator between the type and method-level parts of a HandlerMethod mapping name */
039        public static final String SEPARATOR = "#";
040
041
042        @Override
043        public String getName(HandlerMethod handlerMethod, RequestMappingInfo mapping) {
044                if (mapping.getName() != null) {
045                        return mapping.getName();
046                }
047                StringBuilder sb = new StringBuilder();
048                String simpleTypeName = handlerMethod.getBeanType().getSimpleName();
049                for (int i = 0; i < simpleTypeName.length(); i++) {
050                        if (Character.isUpperCase(simpleTypeName.charAt(i))) {
051                                sb.append(simpleTypeName.charAt(i));
052                        }
053                }
054                sb.append(SEPARATOR).append(handlerMethod.getMethod().getName());
055                return sb.toString();
056        }
057
058}