Skip to content

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

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

Closed
spring-projects-issues opened this issue Mar 7, 2018 · 9 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues 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:

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

Backported to: 4.3.15

@spring-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

Madhura Bhave commented

It works as expected now. thank you!

@spring-projects-issues spring-projects-issues added type: bug A general bug status: backported An issue that has been backported to maintenance branches in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.5 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants