001/*
002 * Copyright 2002-2017 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.messaging.handler.invocation;
018
019import org.springframework.core.MethodParameter;
020import org.springframework.lang.Nullable;
021import org.springframework.messaging.Message;
022import org.springframework.messaging.MessagingException;
023
024/**
025 * Common exception resulting from the invocation of
026 * {@link org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver}.
027 *
028 * @author Juergen Hoeller
029 * @since 4.3.6
030 * @see HandlerMethodArgumentResolver
031 */
032@SuppressWarnings("serial")
033public class MethodArgumentResolutionException extends MessagingException {
034
035        private final MethodParameter parameter;
036
037
038        /**
039         * Create a new instance providing the invalid {@code MethodParameter}.
040         */
041        public MethodArgumentResolutionException(Message<?> message, MethodParameter parameter) {
042                super(message, getMethodParameterMessage(parameter));
043                this.parameter = parameter;
044        }
045
046        /**
047         * Create a new instance providing the invalid {@code MethodParameter} and
048         * a prepared description.
049         */
050        public MethodArgumentResolutionException(Message<?> message, MethodParameter parameter, String description) {
051                super(message, getMethodParameterMessage(parameter) + ": " + description);
052                this.parameter = parameter;
053        }
054
055        /**
056         * Create a new instance providing the invalid {@code MethodParameter},
057         * prepared description, and a cause.
058         */
059        public MethodArgumentResolutionException(
060                        Message<?> message, MethodParameter parameter, String description, @Nullable Throwable cause) {
061
062                super(message, getMethodParameterMessage(parameter) + ": " + description, cause);
063                this.parameter = parameter;
064        }
065
066
067        /**
068         * Return the MethodParameter that was rejected.
069         */
070        public final MethodParameter getMethodParameter() {
071                return this.parameter;
072        }
073
074
075        private static String getMethodParameterMessage(MethodParameter parameter) {
076                return "Could not resolve method parameter at index " + parameter.getParameterIndex() +
077                                " in " + parameter.getExecutable().toGenericString();
078        }
079
080}