From 2624b909060e0967e16771de7a35261decd5a4a9 Mon Sep 17 00:00:00 2001 From: Philippe Marschall Date: Mon, 9 Apr 2012 13:00:32 +0200 Subject: [PATCH] Avoid NPE in AutowiredAnnotationBeanPostProcessor Prior to this change, AABPP#determineRequiredStatus never checked the return value of ReflectionUtils#findMethod when searching for a '#required' attribute. This call returns null for annotations such as @Inject, @Value and @Resource, and subsequently causes a NullPointerException to be thrown when ReflectionUtils#invokeMethod is called. The NPE is caught immediately and #determineRequiredStatus returns defaulting to true, but this this approach is inefficient. It is also problematic for users who have set breakpoints on NPE -- they end up debugging into Spring internals, which is a false positive. This commit checks the return value of of ReflectionUtils#findMethod, and in the case of null, eagerly returns true. There is no change to external behavior, simply a more efficient and debugging-friendly implementation. Existing test cases already cover this change, given that it is purely a refactoring. Issue: SPR-9316 --- .../AutowiredAnnotationBeanPostProcessor.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index ae04086e345..a1489964d64 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -405,10 +405,16 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean protected boolean determineRequiredStatus(Annotation annotation) { try { Method method = ReflectionUtils.findMethod(annotation.annotationType(), this.requiredParameterName); + if (method == null) { + // annotations like @Inject, @Value and @Resource don't have a method + // (attribute) named "required" -> default to required status + return true; + } return (this.requiredParameterValue == (Boolean) ReflectionUtils.invokeMethod(method, annotation)); } catch (Exception ex) { - // required by default + // an exception was thrown during reflective invocation of the required + // attribute -> default to required status return true; } }