Skip to content

Commit

Permalink
Fail early FactoryBean instantiation for LinkageError
Browse files Browse the repository at this point in the history
In 0288878 to resolve gh-22409, a little bug was introduced:
if there is LinkageError in FactoryBean instantiation, no first exception.

In JVM, if a class cannot be initialized, it acts like this:
- at the first time, it will show the real reason and stack
- then, only show "NoClassDefFoundError: Could not initialize class xxx"
  • Loading branch information
liudongmiao committed Jan 21, 2021
1 parent 584dabb commit 4e54c25
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,14 @@ private FactoryBean<?> getSingletonFactoryBeanForTypeCheck(String beanName, Root
throw ex;
}
catch (BeanCreationException ex) {
// LinkageError is unresolvable
Throwable cause = ex.getCause();
while (cause != null) {
if (cause instanceof LinkageError) {
throw ex;
}
cause = cause.getCause();
}
// Instantiation failure, maybe too early...
if (logger.isDebugEnabled()) {
logger.debug("Bean creation exception on singleton FactoryBean type check: " + ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@

package org.springframework.context.annotation;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Andy Wilkinson
*/
Expand Down Expand Up @@ -49,6 +56,23 @@ public void beanMethodFactoryBean() {
}
}

@Test
public void beanMethodFactoryBeanWithError() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(BeanMethodConfigurationWithError.class, StringTypeConfiguration.class);
try {
context.refresh();
} catch (BeanCreationException ex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
ex.printStackTrace(pw);
pw.flush();
String stackTrace = baos.toString();
assertThat(stackTrace.contains(SimpleLinkageErrorClass.class.getSimpleName() + ".<clinit>")).isTrue();
}
}
}


@Configuration
static class BeanMethodConfiguration {
Expand All @@ -60,6 +84,33 @@ public SimpleFactoryBean simpleFactoryBean(ApplicationContext applicationContext
}


@Configuration
static class BeanMethodConfigurationWithError {

@Bean
public SimpleFactoryBean simpleFactoryBean(ApplicationContext applicationContext) {
SimpleFactoryBean simpleFactoryBean = new SimpleFactoryBean(applicationContext);
// cause a linkage error
new SimpleLinkageErrorClass();
return simpleFactoryBean;
}
}


@Configuration
static class StringTypeConfiguration {

@Autowired
private String foo;

@Bean
public String foo() {
return "foo";
}

}


static class SimpleFactoryBean implements FactoryBean<Object> {

public SimpleFactoryBean(ApplicationContext applicationContext) {
Expand All @@ -76,4 +127,17 @@ public Class<?> getObjectType() {
}
}


static class SimpleLinkageErrorClass {

private static final int ERROR = checkError();

public SimpleLinkageErrorClass() {
}

private static int checkError() {
throw new NoSuchMethodError();
}
}

}

0 comments on commit 4e54c25

Please sign in to comment.