Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
21457667
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
21457667
编写于
1月 17, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Proper NoClassDefFoundError check against BeanUtils.instantiateClass
Issue: SPR-16369
上级
32b42799
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
21 deletion
+51
-21
spring-beans/src/main/java/org/springframework/beans/BeanUtils.java
...ns/src/main/java/org/springframework/beans/BeanUtils.java
+34
-12
spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java
...test/context/support/AbstractTestContextBootstrapper.java
+17
-9
未找到文件。
spring-beans/src/main/java/org/springframework/beans/BeanUtils.java
浏览文件 @
21457667
...
...
@@ -98,12 +98,18 @@ public abstract class BeanUtils {
}
/**
* Instantiate a class using its no-arg constructor.
* Instantiate a class using its 'primary' constructor (for Kotlin classes,
* potentially having default arguments declared) or its default constructor
* (for regular Java classes, expecting a standard no-arg setup).
* <p>Note that this method tries to set the constructor accessible
* if given a non-accessible (that is, non-public) constructor.
* @param clazz class to instantiate
* @param clazz
the
class to instantiate
* @return the new instance
* @throws BeanInstantiationException if the bean cannot be instantiated
* @throws BeanInstantiationException if the bean cannot be instantiated.
* The cause may notably indicate a {@link NoSuchMethodException} if no
* primary/default constructor was found - or an exception thrown from
* the constructor invocation attempt, including a runtime-generated
* {@link NoClassDefFoundError} in case of an unresolvable dependency.
* @see Constructor#newInstance
*/
public
static
<
T
>
T
instantiateClass
(
Class
<
T
>
clazz
)
throws
BeanInstantiationException
{
...
...
@@ -113,10 +119,7 @@ public abstract class BeanUtils {
}
try
{
Constructor
<
T
>
ctor
=
(
KotlinDetector
.
isKotlinType
(
clazz
)
?
KotlinDelegate
.
findPrimaryConstructor
(
clazz
)
:
clazz
.
getDeclaredConstructor
());
if
(
ctor
==
null
)
{
throw
new
BeanInstantiationException
(
clazz
,
"No default constructor found"
);
}
KotlinDelegate
.
getPrimaryConstructor
(
clazz
)
:
clazz
.
getDeclaredConstructor
());
return
instantiateClass
(
ctor
);
}
catch
(
NoSuchMethodException
ex
)
{
...
...
@@ -693,10 +696,26 @@ public abstract class BeanUtils {
private
static
class
KotlinDelegate
{
/**
* Return the Java constructor corresponding to the Kotlin primary constructor if any.
* Determine the Java constructor corresponding to the Kotlin primary constructor.
* @param clazz the {@link Class} of the Kotlin class
* @throws NoSuchMethodException if no such constructor found
* @since 5.0.3
* @see #findPrimaryConstructor
* @see Class#getDeclaredConstructor
*/
public
static
<
T
>
Constructor
<
T
>
getPrimaryConstructor
(
Class
<
T
>
clazz
)
throws
NoSuchMethodException
{
Constructor
<
T
>
ctor
=
findPrimaryConstructor
(
clazz
);
if
(
ctor
==
null
)
{
throw
new
NoSuchMethodException
();
}
return
ctor
;
}
/**
* Retrieve the Java constructor corresponding to the Kotlin primary constructor, if any.
* @param clazz the {@link Class} of the Kotlin class
* @see <a href="http://kotlinlang.org/docs/reference/classes.html#constructors">
*
http://kotlinlang.org/docs/reference/classes.html#constructors</a>
* http://kotlinlang.org/docs/reference/classes.html#constructors</a>
*/
@Nullable
public
static
<
T
>
Constructor
<
T
>
findPrimaryConstructor
(
Class
<
T
>
clazz
)
{
...
...
@@ -706,8 +725,10 @@ public abstract class BeanUtils {
return
null
;
}
Constructor
<
T
>
constructor
=
ReflectJvmMapping
.
getJavaConstructor
(
primaryCtor
);
Assert
.
notNull
(
constructor
,
()
->
"Failed to find Java constructor for Kotlin primary constructor: "
+
clazz
.
getName
());
if
(
constructor
==
null
)
{
throw
new
IllegalStateException
(
"Failed to find Java constructor for Kotlin primary constructor: "
+
clazz
.
getName
());
}
return
constructor
;
}
catch
(
UnsupportedOperationException
ex
)
{
...
...
@@ -718,7 +739,8 @@ public abstract class BeanUtils {
/**
* Instantiate a Kotlin class using the provided constructor.
* @param ctor the constructor of the Kotlin class to instantiate
* @param args the constructor arguments to apply (use null for unspecified parameter if needed)
* @param args the constructor arguments to apply
* (use {@code null} for unspecified parameter if needed)
*/
public
static
<
T
>
T
instantiateClass
(
Constructor
<
T
>
ctor
,
Object
...
args
)
throws
IllegalAccessException
,
InvocationTargetException
,
InstantiationException
{
...
...
spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java
浏览文件 @
21457667
...
...
@@ -28,6 +28,7 @@ import java.util.Set;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.BeanInstantiationException
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.core.annotation.AnnotationAwareOrderComparator
;
import
org.springframework.core.annotation.AnnotationUtils
;
...
...
@@ -178,18 +179,25 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
return
listeners
;
}
private
List
<
TestExecutionListener
>
instantiateListeners
(
Collection
<
Class
<?
extends
TestExecutionListener
>>
classes
List
)
{
List
<
TestExecutionListener
>
listeners
=
new
ArrayList
<>(
classes
List
.
size
());
for
(
Class
<?
extends
TestExecutionListener
>
listenerClass
:
classes
List
)
{
private
List
<
TestExecutionListener
>
instantiateListeners
(
Collection
<
Class
<?
extends
TestExecutionListener
>>
classes
)
{
List
<
TestExecutionListener
>
listeners
=
new
ArrayList
<>(
classes
.
size
());
for
(
Class
<?
extends
TestExecutionListener
>
listenerClass
:
classes
)
{
try
{
listeners
.
add
(
BeanUtils
.
instantiateClass
(
listenerClass
));
}
catch
(
NoClassDefFoundError
err
)
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
String
.
format
(
"Could not instantiate TestExecutionListener [%s]. "
+
"Specify custom listener classes or make the default listener classes "
+
"(and their required dependencies) available. Offending class: [%s]"
,
listenerClass
.
getName
(),
err
.
getMessage
()));
catch
(
BeanInstantiationException
ex
)
{
if
(
ex
.
getCause
()
instanceof
NoClassDefFoundError
)
{
// TestExecutionListener not applicable due to a missing dependency
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
String
.
format
(
"Skipping candidate TestExecutionListener [%s] due to a missing dependency. "
+
"Specify custom listener classes or make the default listener classes "
+
"and their required dependencies available. Offending class: [%s]"
,
listenerClass
.
getName
(),
ex
.
getCause
().
getMessage
()));
}
}
else
{
throw
ex
;
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录