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

Don't use Exception for normal behavior in org.springframework.core.annotation.AnnotationUtils.getValue [SPR-12604] #17205

Closed
spring-issuemaster opened this issue Jan 8, 2015 · 1 comment

Comments

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

commented Jan 8, 2015

Sebastian Dietrich opened SPR-12604 and commented

If annotation is null this method throws a NullPointerException and catches it to return null. This method is called frequently with annotation==null - at least in my case (using spring-data to findById)

Please rewrite it to:
public static Object getValue(Annotation annotation, String attributeName) {
if (annotation == null) return null;
try {
Method method = annotation.annotationType().getDeclaredMethod(attributeName);
ReflectionUtils.makeAccessible(method);
return method.invoke(annotation);
}
catch (Exception ex) {
return null;
}
}

here is the end of the corresponding stack:
AnnotationUtils.getValue(Annotation, String) line: 642
AnnotationUtils.getValue(Annotation) line: 630
JpaQueryMethod.getLockModeType() line: 164
PartTreeJpaQuery(AbstractJpaQuery).applyLockMode(Query, JpaQueryMethod) line: 155
PartTreeJpaQuery(AbstractJpaQuery).createQuery(Object[]) line: 164
JpaQueryExecution$SingleEntityExecution.doExecute(AbstractJpaQuery, Object[]) line: 202
JpaQueryExecution$SingleEntityExecution(JpaQueryExecution).execute(AbstractJpaQuery, Object[]) line: 74
...


Affects: 4.0.8

Referenced from: commits a7975c6, 0ddf8dd

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 9, 2015

Juergen Hoeller commented

Hmm, that method isn't really meant to be called with a null annotation reference to begin with... I'd recommend a not-null check in the corresponding Spring Data code.

That said, it doesn't hurt to defensively check for null in AnnotationUtils itself either. I'll revise it for 4.1.5.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.