Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions