Skip to content

Commit ea5f8f5

Browse files
committed
Consistently handle NoClassDefFoundError in BeanUtils and related places
Issue: SPR-16369
1 parent 0c28928 commit ea5f8f5

File tree

7 files changed

+26
-26
lines changed

7 files changed

+26
-26
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationExceptio
107107
* @return the new instance
108108
* @throws BeanInstantiationException if the bean cannot be instantiated.
109109
* The cause may notably indicate a {@link NoSuchMethodException} if no
110-
* primary/default constructor was found - or an exception thrown from
111-
* the constructor invocation attempt, including a runtime-generated
112-
* {@link NoClassDefFoundError} in case of an unresolvable dependency.
110+
* primary/default constructor was found, a {@link NoClassDefFoundError}
111+
* or other {@link LinkageError} in case of an unresolvable class definition
112+
* (e.g. due to a missing dependency at runtime), or an exception thrown
113+
* from the constructor invocation itself.
113114
* @see Constructor#newInstance
114115
*/
115116
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
@@ -125,6 +126,9 @@ public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationExc
125126
catch (NoSuchMethodException ex) {
126127
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
127128
}
129+
catch (LinkageError err) {
130+
throw new BeanInstantiationException(clazz, "Unresolvable class definition", err);
131+
}
128132
}
129133

130134
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,8 @@ protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanNam
13791379
catch (ClassNotFoundException ex) {
13801380
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
13811381
}
1382-
catch (LinkageError ex) {
1383-
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
1382+
catch (LinkageError err) {
1383+
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
13841384
}
13851385
}
13861386

spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultNamespaceHandlerResolver.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -137,12 +137,12 @@ else if (handlerOrClassName instanceof NamespaceHandler) {
137137
return namespaceHandler;
138138
}
139139
catch (ClassNotFoundException ex) {
140-
throw new FatalBeanException("NamespaceHandler class [" + className + "] for namespace [" +
141-
namespaceUri + "] not found", ex);
140+
throw new FatalBeanException("Could not find NamespaceHandler class [" + className +
141+
"] for namespace [" + namespaceUri + "]", ex);
142142
}
143143
catch (LinkageError err) {
144-
throw new FatalBeanException("Invalid NamespaceHandler class [" + className + "] for namespace [" +
145-
namespaceUri + "]: problem with handler class file or dependent class", err);
144+
throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" +
145+
className + "] for namespace [" + namespaceUri + "]", err);
146146
}
147147
}
148148
}

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,10 @@ public static Class<?> resolveClassName(String className, @Nullable ClassLoader
298298
return forName(className, classLoader);
299299
}
300300
catch (ClassNotFoundException ex) {
301-
throw new IllegalArgumentException("Cannot find class [" + className + "]", ex);
301+
throw new IllegalArgumentException("Could not find class [" + className + "]", ex);
302302
}
303-
catch (LinkageError ex) {
304-
throw new IllegalArgumentException(
305-
"Error loading class [" + className + "]: problem with class file or dependent class.", ex);
303+
catch (LinkageError err) {
304+
throw new IllegalArgumentException("Unresolvable class definition for class [" + className + "]", err);
306305
}
307306
}
308307

spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -355,11 +355,8 @@ public Validator webFluxValidator() {
355355
String name = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
356356
clazz = ClassUtils.forName(name, getClass().getClassLoader());
357357
}
358-
catch (ClassNotFoundException ex) {
359-
throw new BeanInitializationException("Could not find default validator class", ex);
360-
}
361-
catch (LinkageError ex) {
362-
throw new BeanInitializationException("Could not load default validator class", ex);
358+
catch (ClassNotFoundException | LinkageError ex) {
359+
throw new BeanInitializationException("Failed to resolve default validator class", ex);
363360
}
364361
validator = (Validator) BeanUtils.instantiateClass(clazz);
365362
}

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -850,12 +850,12 @@ protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T>
850850
catch (ClassNotFoundException ex) {
851851
throw new BeanInitializationException(
852852
"Could not find DispatcherServlet's default strategy class [" + className +
853-
"] for interface [" + key + "]", ex);
853+
"] for interface [" + key + "]", ex);
854854
}
855855
catch (LinkageError err) {
856856
throw new BeanInitializationException(
857-
"Error loading DispatcherServlet's default strategy class [" + className +
858-
"] for interface [" + key + "]: problem with class file or dependent class", err);
857+
"Unresolvable class definition for DispatcherServlet's default strategy class [" +
858+
className + "] for interface [" + key + "]", err);
859859
}
860860
}
861861
return strategies;

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -662,7 +662,7 @@ public Validator mvcValidator() {
662662
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
663663
}
664664
catch (ClassNotFoundException | LinkageError ex) {
665-
throw new BeanInitializationException("Could not find default validator class", ex);
665+
throw new BeanInitializationException("Failed to resolve default validator class", ex);
666666
}
667667
validator = (Validator) BeanUtils.instantiateClass(clazz);
668668
}

0 commit comments

Comments
 (0)