001/*
002 * Copyright 2002-2018 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.FatalBeanException;
020import org.springframework.lang.Nullable;
021
022/**
023 * Exception thrown when the BeanFactory cannot load the specified class
024 * of a given bean.
025 *
026 * @author Juergen Hoeller
027 * @since 2.0
028 */
029@SuppressWarnings("serial")
030public class CannotLoadBeanClassException extends FatalBeanException {
031
032        @Nullable
033        private final String resourceDescription;
034
035        private final String beanName;
036
037        @Nullable
038        private final String beanClassName;
039
040
041        /**
042         * Create a new CannotLoadBeanClassException.
043         * @param resourceDescription description of the resource
044         * that the bean definition came from
045         * @param beanName the name of the bean requested
046         * @param beanClassName the name of the bean class
047         * @param cause the root cause
048         */
049        public CannotLoadBeanClassException(@Nullable String resourceDescription, String beanName,
050                        @Nullable String beanClassName, ClassNotFoundException cause) {
051
052                super("Cannot find class [" + beanClassName + "] for bean with name '" + beanName + "'" +
053                                (resourceDescription != null ? " defined in " + resourceDescription : ""), cause);
054                this.resourceDescription = resourceDescription;
055                this.beanName = beanName;
056                this.beanClassName = beanClassName;
057        }
058
059        /**
060         * Create a new CannotLoadBeanClassException.
061         * @param resourceDescription description of the resource
062         * that the bean definition came from
063         * @param beanName the name of the bean requested
064         * @param beanClassName the name of the bean class
065         * @param cause the root cause
066         */
067        public CannotLoadBeanClassException(@Nullable String resourceDescription, String beanName,
068                        @Nullable String beanClassName, LinkageError cause) {
069
070                super("Error loading class [" + beanClassName + "] for bean with name '" + beanName + "'" +
071                                (resourceDescription != null ? " defined in " + resourceDescription : "") +
072                                ": problem with class file or dependent class", cause);
073                this.resourceDescription = resourceDescription;
074                this.beanName = beanName;
075                this.beanClassName = beanClassName;
076        }
077
078
079        /**
080         * Return the description of the resource that the bean
081         * definition came from.
082         */
083        @Nullable
084        public String getResourceDescription() {
085                return this.resourceDescription;
086        }
087
088        /**
089         * Return the name of the bean requested.
090         */
091        public String getBeanName() {
092                return this.beanName;
093        }
094
095        /**
096         * Return the name of the class we were trying to load.
097         */
098        @Nullable
099        public String getBeanClassName() {
100                return this.beanClassName;
101        }
102
103}