From e1d19d6c24f48f35f528ddd1d4b07a45f2fb0399 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 21 Apr 2011 08:00:42 +0000 Subject: [PATCH] SPR-8238 + add handling for null arguments to prevent NPE in default key generation --- .../springframework/cache/support/DefaultKeyGenerator.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java b/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java index 6466fe4f2f..3dff46c9f5 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java @@ -22,7 +22,7 @@ import org.springframework.cache.KeyGenerator; /** * Default key generator. Returns 0 if no param is given, the param itself if only one is given or a hash code computed - * from all given params hash code. + * from all given params hash code. Uses a constant (53) for null objects. * * @author Costin Leau */ @@ -30,7 +30,7 @@ public class DefaultKeyGenerator implements KeyGenerator { public Object extract(Object target, Method method, Object... params) { if (params.length == 1) { - return params[0]; + return (params[0] == null ? 53 : params[0]); } if (params.length == 0) { @@ -40,7 +40,7 @@ public class DefaultKeyGenerator implements KeyGenerator { int hashCode = 17; for (Object object : params) { - hashCode = 31 * hashCode + object.hashCode(); + hashCode = 31 * hashCode + (object == null ? 53 : object.hashCode()); } return Integer.valueOf(hashCode);