Browse Source

Fixed an NPE in ImplicitPropertyAccessor due to putting null value in ConcurrentHashMap

Abhinav Sarkar 8 years ago
parent
commit
410dcdada2

+ 1
- 2
src/main/java/net/abhinavsarkar/spelhelper/ImplicitConstructorResolver.java View File

@@ -27,8 +27,7 @@ import org.springframework.expression.ConstructorResolver;
27 27
 import org.springframework.expression.EvaluationContext;
28 28
 import org.springframework.expression.spel.support.ReflectiveConstructorResolver;
29 29
 
30
-final class ImplicitConstructorResolver implements
31
-        ConstructorResolver {
30
+final class ImplicitConstructorResolver implements ConstructorResolver {
32 31
 
33 32
     private final ReflectiveConstructorResolver delegate = new ReflectiveConstructorResolver();
34 33
 

+ 5
- 4
src/main/java/net/abhinavsarkar/spelhelper/ImplicitMethodResolver.java View File

@@ -39,20 +39,21 @@ final class ImplicitMethodResolver implements MethodResolver {
39 39
     private final ReflectiveMethodResolver delegate = new ReflectiveMethodResolver();
40 40
 
41 41
     private static final MethodExecutor NULL_ME = new MethodExecutor() {
42
+        @Override
42 43
         public TypedValue execute(final EvaluationContext context, final Object target,
43 44
                 final Object... arguments) throws AccessException {
44
-            return null;
45
+            throw new UnsupportedOperationException("This method should never be called");
45 46
         }
46 47
     };
47 48
 
48
-    private static final class ImplicitMethodExecutor implements
49
-            MethodExecutor {
49
+    private static final class ImplicitMethodExecutor implements MethodExecutor {
50 50
         private final MethodExecutor executor;
51 51
 
52 52
         public ImplicitMethodExecutor(final MethodExecutor executor) {
53 53
             this.executor = executor;
54 54
         }
55 55
 
56
+        @Override
56 57
         public TypedValue execute(final EvaluationContext context, final Object target,
57 58
                 final Object... arguments) throws AccessException {
58 59
             Object[] modifiedArguments = new Object[arguments.length + 1];
@@ -66,7 +67,7 @@ final class ImplicitMethodResolver implements MethodResolver {
66 67
     public MethodExecutor resolve(
67 68
             final EvaluationContext context, final Object targetObject,
68 69
             final String name, final List<TypeDescriptor> argumentTypes)
69
-    throws AccessException {
70
+        throws AccessException {
70 71
         if (targetObject == null) {
71 72
             return null;
72 73
         }

+ 12
- 2
src/main/java/net/abhinavsarkar/spelhelper/ImplicitPropertyAccessor.java View File

@@ -34,13 +34,22 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
34 34
     private static final ConcurrentHashMap<String, MethodExecutor> CACHE =
35 35
         new ConcurrentHashMap<String, MethodExecutor>();
36 36
 
37
+    private static final MethodExecutor NULL_ME = new MethodExecutor() {
38
+        @Override
39
+        public TypedValue execute(final EvaluationContext context, final Object target,
40
+                final Object... arguments) throws AccessException {
41
+            throw new UnsupportedOperationException("This method should never be called");
42
+        }
43
+    };
44
+
45
+    @Override
37 46
     public boolean canRead(final EvaluationContext context,
38 47
             final Object target, final String name)
39 48
             throws AccessException {
40 49
         Assert.notNull(target, "target is null");
41 50
         String cacheKey = target.getClass().getName() + "." + name;
42 51
         if (CACHE.containsKey(cacheKey)) {
43
-            return CACHE.get(cacheKey) != null;
52
+            return CACHE.get(cacheKey) != NULL_ME;
44 53
         }
45 54
 
46 55
         for (MethodResolver mr : context.getMethodResolvers()) {
@@ -52,10 +61,11 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
52 61
             }
53 62
         }
54 63
 
55
-        CACHE.putIfAbsent(cacheKey, null);
64
+        CACHE.putIfAbsent(cacheKey, NULL_ME);
56 65
         return false;
57 66
     }
58 67
 
68
+    @Override
59 69
     public TypedValue read(final EvaluationContext context,
60 70
             final Object target, final String name)
61 71
             throws AccessException {

+ 4
- 2
src/main/java/net/abhinavsarkar/spelhelper/ReadOnlyGenericPropertyAccessor.java View File

@@ -23,19 +23,21 @@ import org.springframework.expression.AccessException;
23 23
 import org.springframework.expression.EvaluationContext;
24 24
 import org.springframework.expression.PropertyAccessor;
25 25
 
26
-abstract class ReadOnlyGenericPropertyAccessor implements
27
-        PropertyAccessor {
26
+abstract class ReadOnlyGenericPropertyAccessor implements PropertyAccessor {
28 27
 
28
+    @Override
29 29
     public final boolean canWrite(final EvaluationContext context,
30 30
             final Object target, final String name) throws AccessException {
31 31
         return false;
32 32
     }
33 33
 
34
+    @Override
34 35
     @SuppressWarnings("rawtypes")
35 36
     public final Class[] getSpecificTargetClasses() {
36 37
         return null;
37 38
     }
38 39
 
40
+    @Override
39 41
     public final void write(final EvaluationContext context, final Object target,
40 42
             final String name, final Object newValue) throws AccessException {
41 43
         throw new AccessException(MessageFormat.format(

Loading…
Cancel
Save