001/*
002 * Copyright 2002-2016 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.beans.factory;
018
019import org.springframework.beans.BeansException;
020import org.springframework.util.ClassUtils;
021
022/**
023 * Thrown when a bean doesn't match the expected type.
024 *
025 * @author Rod Johnson
026 * @author Juergen Hoeller
027 */
028@SuppressWarnings("serial")
029public class BeanNotOfRequiredTypeException extends BeansException {
030
031        /** The name of the instance that was of the wrong type */
032        private String beanName;
033
034        /** The required type */
035        private Class<?> requiredType;
036
037        /** The offending type */
038        private Class<?> actualType;
039
040
041        /**
042         * Create a new BeanNotOfRequiredTypeException.
043         * @param beanName the name of the bean requested
044         * @param requiredType the required type
045         * @param actualType the actual type returned, which did not match
046         * the expected type
047         */
048        public BeanNotOfRequiredTypeException(String beanName, Class<?> requiredType, Class<?> actualType) {
049                super("Bean named '" + beanName + "' is expected to be of type '" + ClassUtils.getQualifiedName(requiredType) +
050                                "' but was actually of type '" + ClassUtils.getQualifiedName(actualType) + "'");
051                this.beanName = beanName;
052                this.requiredType = requiredType;
053                this.actualType = actualType;
054        }
055
056
057        /**
058         * Return the name of the instance that was of the wrong type.
059         */
060        public String getBeanName() {
061                return this.beanName;
062        }
063
064        /**
065         * Return the expected type for the bean.
066         */
067        public Class<?> getRequiredType() {
068                return this.requiredType;
069        }
070
071        /**
072         * Return the actual type of the instance found.
073         */
074        public Class<?> getActualType() {
075                return this.actualType;
076        }
077
078}