Skip to content

Commit 7e55b08

Browse files
committed
BeanInstantiationException preserves Constructor/Method if available
Issue: SPR-14166
1 parent 3222664 commit 7e55b08

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanInstantiationException.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.beans;
1818

19+
import java.lang.reflect.Constructor;
20+
import java.lang.reflect.Method;
21+
1922
/**
2023
* Exception thrown when instantiation of a bean failed.
2124
* Carries the offending bean class.
@@ -28,6 +31,10 @@ public class BeanInstantiationException extends FatalBeanException {
2831

2932
private Class<?> beanClass;
3033

34+
private Constructor<?> constructor;
35+
36+
private Method constructingMethod;
37+
3138

3239
/**
3340
* Create a new BeanInstantiationException.
@@ -49,12 +56,60 @@ public BeanInstantiationException(Class<?> beanClass, String msg, Throwable caus
4956
this.beanClass = beanClass;
5057
}
5158

59+
/**
60+
* Create a new BeanInstantiationException.
61+
* @param constructor the offending constructor
62+
* @param msg the detail message
63+
* @param cause the root cause
64+
* @since 4.3
65+
*/
66+
public BeanInstantiationException(Constructor<?> constructor, String msg, Throwable cause) {
67+
super("Failed to instantiate [" + constructor.getDeclaringClass().getName() + "]: " + msg, cause);
68+
this.beanClass = constructor.getDeclaringClass();
69+
this.constructor = constructor;
70+
}
5271

5372
/**
54-
* Return the offending bean class.
73+
* Create a new BeanInstantiationException.
74+
* @param constructingMethod the delegate for bean construction purposes
75+
* (typically, but not necessarily, a static factory method)
76+
* @param msg the detail message
77+
* @param cause the root cause
78+
* @since 4.3
79+
*/
80+
public BeanInstantiationException(Method constructingMethod, String msg, Throwable cause) {
81+
super("Failed to instantiate [" + constructingMethod.getReturnType().getName() + "]: " + msg, cause);
82+
this.beanClass = constructingMethod.getReturnType();
83+
this.constructingMethod = constructingMethod;
84+
}
85+
86+
87+
/**
88+
* Return the offending bean class (never {@code null}).
89+
* @return the class that was to be instantiated
5590
*/
5691
public Class<?> getBeanClass() {
5792
return this.beanClass;
5893
}
5994

95+
/**
96+
* Return the offending constructor, if known.
97+
* @return the constructor in use, or {@code null} in case of a
98+
* factory method or in case of default instantiation
99+
* @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+
60115
}

spring-beans/src/main/java/org/springframework/beans/BeanUtils.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,16 @@ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws
147147
return ctor.newInstance(args);
148148
}
149149
catch (InstantiationException ex) {
150-
throw new BeanInstantiationException(ctor.getDeclaringClass(),
151-
"Is it an abstract class?", ex);
150+
throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
152151
}
153152
catch (IllegalAccessException ex) {
154-
throw new BeanInstantiationException(ctor.getDeclaringClass(),
155-
"Is the constructor accessible?", ex);
153+
throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
156154
}
157155
catch (IllegalArgumentException ex) {
158-
throw new BeanInstantiationException(ctor.getDeclaringClass(),
159-
"Illegal arguments for constructor", ex);
156+
throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
160157
}
161158
catch (InvocationTargetException ex) {
162-
throw new BeanInstantiationException(ctor.getDeclaringClass(),
163-
"Constructor threw exception", ex.getTargetException());
159+
throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
164160
}
165161
}
166162

spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,12 +171,12 @@ public Object run() {
171171
}
172172
}
173173
catch (IllegalArgumentException ex) {
174-
throw new BeanInstantiationException(factoryMethod.getReturnType(),
174+
throw new BeanInstantiationException(factoryMethod,
175175
"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
176176
"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
177177
}
178178
catch (IllegalAccessException ex) {
179-
throw new BeanInstantiationException(factoryMethod.getReturnType(),
179+
throw new BeanInstantiationException(factoryMethod,
180180
"Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
181181
}
182182
catch (InvocationTargetException ex) {
@@ -186,7 +186,7 @@ public Object run() {
186186
msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +
187187
"declaring the factory method as static for independence from its containing instance. " + msg;
188188
}
189-
throw new BeanInstantiationException(factoryMethod.getReturnType(), msg, ex.getTargetException());
189+
throw new BeanInstantiationException(factoryMethod, msg, ex.getTargetException());
190190
}
191191
}
192192

0 commit comments

Comments
 (0)