Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unresolvable class in 'basePackageClasses' annotation attribute results in misleading and hard to diagnose error [SPR-13177] #17769

Closed
spring-issuemaster opened this issue Jul 1, 2015 · 1 comment
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jul 1, 2015

Tim Reidel opened SPR-13177 and commented

I ran into an issue with my spring-based project that results in a hard-to diagnose error. Fortunately I also have the solution. :)

When starting my application using spring-boot I get the following exception trace:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [ XXXXXXX ]; nested exception is java.lang.IllegalArgumentException: Attribute 'basePackageClasses' not found
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:179)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at com.fancypants.common.application.Application.main(Application.java:36)
Caused by: java.lang.IllegalArgumentException: Attribute 'basePackageClasses' not found
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.core.annotation.AnnotationAttributes.doGet(AnnotationAttributes.java:108)
        at org.springframework.core.annotation.AnnotationAttributes.getClassArray(AnnotationAttributes.java:93)
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:127)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:265)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:229)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
        ... 10 more

where XXXXXXX = one of my configuration classes.

basePackageClasses is certainly defined in the @ComponentScan annotation. It turns out that one of the classes referenced in basePackageClasses is not defined. The effect of this is that in AnnotationReadingVisitorUtils (which helps to parse the annotation) a ClassNotFoundException is thrown. This is expected and normal since my configuration was wrong and I was missing some jars in the classpath.

The ClassNotFoundException is caught by this piece of code in AnnotationReadingVisitorUtils:

catch (Exception ex) {
	// Class not found - can't resolve class reference in annotation
	// attribute.
}

Nothing is done with the error with the effect being that basePackageClasses is not defined in the resulting AnnotiationAttributes object. Later on when the AnnotationAttributes object is queried for the basePackageClasses they aren't found, resulting in the misleading error above.

My suggestion is to throw an IllegalArgumentException in the catch block above, indicating which class was not found. I can likely submit a pull request to this effect if needed.


Affects: 4.1.6

Referenced from: commits b7acddb

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 6, 2015

Juergen Hoeller commented

I'm resolving this for 4.2 only at this point, since the solution is somewhat involved, storing the Exception instances in AnnotationAttributes and re-throwing them on access. This is unfortunately necessary since we're scanning a lot of annotations across an application's codebase and generally want to be lenient about non-resolvable attributes that we do not care about. So re-throwing at access time only raises an error for attributes that we are actually trying to parse.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.