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;
018
019import java.lang.reflect.Constructor;
020import java.lang.reflect.Method;
021
022/**
023 * Exception thrown when instantiation of a bean failed.
024 * Carries the offending bean class.
025 *
026 * @author Juergen Hoeller
027 * @since 1.2.8
028 */
029@SuppressWarnings("serial")
030public class BeanInstantiationException extends FatalBeanException {
031
032        private Class<?> beanClass;
033
034        private Constructor<?> constructor;
035
036        private Method constructingMethod;
037
038
039        /**
040         * Create a new BeanInstantiationException.
041         * @param beanClass the offending bean class
042         * @param msg the detail message
043         */
044        public BeanInstantiationException(Class<?> beanClass, String msg) {
045                this(beanClass, msg, null);
046        }
047
048        /**
049         * Create a new BeanInstantiationException.
050         * @param beanClass the offending bean class
051         * @param msg the detail message
052         * @param cause the root cause
053         */
054        public BeanInstantiationException(Class<?> beanClass, String msg, Throwable cause) {
055                super("Failed to instantiate [" + beanClass.getName() + "]: " + msg, cause);
056                this.beanClass = beanClass;
057        }
058
059        /**
060         * Create a new BeanInstantiationException.
061         * @param constructor the offending constructor
062         * @param msg the detail message
063         * @param cause the root cause
064         * @since 4.3
065         */
066        public BeanInstantiationException(Constructor<?> constructor, String msg, Throwable cause) {
067                super("Failed to instantiate [" + constructor.getDeclaringClass().getName() + "]: " + msg, cause);
068                this.beanClass = constructor.getDeclaringClass();
069                this.constructor = constructor;
070        }
071
072        /**
073         * Create a new BeanInstantiationException.
074         * @param constructingMethod the delegate for bean construction purposes
075         * (typically, but not necessarily, a static factory method)
076         * @param msg the detail message
077         * @param cause the root cause
078         * @since 4.3
079         */
080        public BeanInstantiationException(Method constructingMethod, String msg, Throwable cause) {
081                super("Failed to instantiate [" + constructingMethod.getReturnType().getName() + "]: " + msg, cause);
082                this.beanClass = constructingMethod.getReturnType();
083                this.constructingMethod = constructingMethod;
084        }
085
086
087        /**
088         * Return the offending bean class (never {@code null}).
089         * @return the class that was to be instantiated
090         */
091        public Class<?> getBeanClass() {
092                return this.beanClass;
093        }
094
095        /**
096         * Return the offending constructor, if known.
097         * @return the constructor in use, or {@code null} in case of a
098         * factory method or in case of default instantiation
099         * @since 4.3
100         */
101        public Constructor<?> getConstructor() {
102                return this.constructor;
103        }
104
105        /**
106         * Return the delegate for bean construction purposes, if known.
107         * @return the method in use (typically a static factory method),
108         * or {@code null} in case of constructor-based instantiation
109         * @since 4.3
110         */
111        public Method getConstructingMethod() {
112                return this.constructingMethod;
113        }
114
115}