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

Reading annotations in ConfigurationClassParser does not fall back to ASM on Google App Engine [SPR-16564] #21106

Closed
spring-issuemaster opened this Issue Mar 7, 2018 · 9 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Mar 7, 2018

Madhura Bhave opened SPR-16564 and commented

ConfigurationClassParser has a sanity test to ensure that annotations can be read and in case of an Exception, it falls back to ASM. On Google App Engine classType.getAnnotations() does not throw an Exception if a class is not found and instead the annotation value looks something like this value=sun.reflect.annotation.TypeNotPresentExceptionProxy.

This causes unexpected behavior in Spring Boot's OnClassCondition as the metadata is not an AnnotationMetadataReadingVisitor.


Affects: 4.3.14, 5.0.4

Issue Links:

  • #15712 @PropertySources fails on JDK 7 due to @Repeatable
  • #15274 Support @Configuration class parsing fallback to ASM

Referenced from: commits 7cafa67, 967a2ef, 50e980c

Backported to: 4.3.15

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 8, 2018

Juergen Hoeller commented

So classType.getAnnotations() would return concrete Annotation instances here (of the declared annotation type), but some of the Class attributes of those annotations would not contains an actual Class value but rather a TypeNotPresentExceptionProxy stub?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 8, 2018

Madhura Bhave commented

Yes exactly. For example, for a nested class that looks like this (where FooClass is not present):

@Configuration
@ConditionalOnClass(FooClass.class)
protected static class Test {
}

this is the log for classType.getAnnotations():

@org.springframework.context.annotation.Configuration(value=)
@org.springframework.boot.autoconfigure.condition.ConditionalOnClass(name=[], value=sun.reflect.annotation.TypeNotPresentExceptionProxy)
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 8, 2018

Juergen Hoeller commented

Hmm that's rather awkward to find out though: We'd have to iterate over all attributes of all declared annotations and see whether any of them is a TypeNotPresentExceptionProxy... which isn't even a proper API type. And we'd have to do that for every such declared annotation on a configuration class.

In what way does this break Boot's OnClassCondition? Which specific assumptions about AnnotationMetadataReadingVisitor do you have there? Could we adapt those to find about the TypeNotPresentExceptionProxy within OnClassCondition specifically, rather than making it a part of core configuration class processing?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 8, 2018

Madhura Bhave commented

When OnClassCondition checks for the presence of @ConditionalOnClass, it calls metadata.getAllAnnotationAttributes which, for StandardAnnotationMetadata returns null due to:

Failed to introspect annotations on [class Foo]: java.lang.IllegalStateException: Could not obtain annotation attribute value for public abstract java.lang.Class[] org.springframework.boot.autoconfigure.condition.ConditionalOnClass.value()

Boot then assumes that this class is not a candidate for @ConditionalOnClass (since the attributes are null). This is the original issue spring-projects/spring-boot#10553

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 12, 2018

Juergen Hoeller commented

I've introduced an AnnotationUtils.validateAnnotation(Annotation) method which we're applying in ConfigurationClassParser.asSourceClass to all declared annotations now before making a decision on reflection versus ASM. We check every declared Class attribute there, trying to read its value... which should fail on Google App Engine at that very point, otherwise indicating that getAnnotationAttributes calls will work fine later on.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 12, 2018

Juergen Hoeller commented

Madhura Bhave, this is included in the upcoming 5.0.5 snapshot now. Please give it a try and let me know whether it actually helps on GAE... If it doesn't, it'd be great to debug AnnotationUtils.validateAnnotation in terms of why it doesn't fail when invoking the affected attribute. In any case, this should be solvable at that level.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 12, 2018

Madhura Bhave commented

Thanks, Juergen. I tested it on GAE. Looks like it didn't work because AnnotationUtils.validateAnnotation checks for method.getReturnType() == Class.class and @ConditionalOnClass has a Class<?>[] value.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 12, 2018

Juergen Hoeller commented

Oops, indeed. I've extended that check to include Class arrays as well... Give it one more try with the upcoming snapshot please, hopefully it works for your purposes now!

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Mar 12, 2018

Madhura Bhave commented

It works as expected now. thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment