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