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

Using the @ConditionalOnClass annotation in the method results in java.lang.ArrayStoreException #17282

Closed
brucelwl opened this issue Jun 21, 2019 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@brucelwl
Copy link

brucelwl commented Jun 21, 2019

Using the @ConditionalOnClass annotation in the method results in "java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy",

The main reason is to traverse all methods of the class in org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor, call the method object to get all annotations on the method, but throw an exception when the conditional class does not exist

image

See my code examples and exception information

image
image

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 21, 2019
@wilkinsona
Copy link
Member

Unfortunately, this is how the JVM works and there’s nothing Spring Boot or Framework can do about it. That is why the javadoc for @ConditionalOnClass urges you to be careful about using it on @Bean methods and recommends isolating the condition on a separate @Configuration class instead. Alternatively you could use the annotation’s name attribute at the cost of it then being stringly typed.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 21, 2019
@ccoderJava
Copy link

    @Bean
    @ConditionalOnClass(MongoTemplate.class)
    public MongoDataLayerAdapter mongoDataLayerAdapter() {
        return new MongoDataLayerAdapter();
    }

When I use this @Bean and @ConditionalOnClass to inject SpringBean. The injection needs to be done when the MongoTemplate exists.
But in fact, when the class MongoTemplate.class does not exist, the following exception stack information will appear:

Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class com.xxx.xxx.xxx.DataComplianceAutoConfiguration
	at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:162) ~[spring-core-5.3.21.jar:5.3.21]
	at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:403) ~[spring-context-5.3.21.jar:5.3.21]
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:326) ~[spring-context-5.3.21.jar:5.3.21]
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.21.jar:5.3.21]
	at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:600) ~[spring-context-5.3.21.jar:5.3.21]
	... 17 common frames omitted
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
	at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_201]
	at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_201]
	at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_201]
	at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_201]
	at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_201]
	at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_201]
	at java.lang.reflect.Executable.declaredAnnotations(Executable.java:599) ~[na:1.8.0_201]
	at java.lang.reflect.Executable.declaredAnnotations(Executable.java:597) ~[na:1.8.0_201]
	at java.lang.reflect.Executable.getDeclaredAnnotations(Executable.java:588) ~[na:1.8.0_201]
	at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:630) ~[na:1.8.0_201]
	at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:207) ~[na:1.8.0_201]
	at org.springframework.core.type.StandardAnnotationMetadata.isAnnotatedMethod(StandardAnnotationMetadata.java:170) ~[spring-core-5.3.21.jar:5.3.21]
	at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:153) ~[spring-core-5.3.21.jar:5.3.21]
	... 21 common frames omitted

Now I modified it to use name injection. And using the fully qualified package name of the class can bypass Java's reflect problem.

    @Bean
    @ConditionalOnClass(name = "org.springframework.data.mongodb.core.MongoTemplate")
    public MongoDataLayerAdapter mongoDataLayerAdapter() {
        return new MongoDataLayerAdapter();
    }

now. it works. 😃

@oneice2020
Copy link

Class Conditions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants