Browse Source

Corrected PMD violations

Abhinav Sarkar 10 years ago
parent
commit
555b0cc383

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
1 1
 target/*
2
+.pmd

+ 12
- 13
src/main/java/net/abhinavsarkar/spelhelper/ImplicitMethodResolver.java View File

@@ -30,7 +30,7 @@ import org.springframework.expression.spel.support.ReflectiveMethodResolver;
30 30
 
31 31
 final class ImplicitMethodResolver implements MethodResolver {
32 32
 
33
-    private static final ConcurrentHashMap<String, MethodExecutor> cache =
33
+    private static final ConcurrentHashMap<String, MethodExecutor> CACHE =
34 34
         new ConcurrentHashMap<String, MethodExecutor>();
35 35
 
36 36
     private static final MethodExecutor NULL_ME = new MethodExecutor() {
@@ -44,7 +44,7 @@ final class ImplicitMethodResolver implements MethodResolver {
44 44
             MethodExecutor {
45 45
         private final MethodExecutor executor;
46 46
 
47
-        private ImplicitMethodExecutor(final MethodExecutor executor) {
47
+        public ImplicitMethodExecutor(final MethodExecutor executor) {
48 48
             this.executor = executor;
49 49
         }
50 50
 
@@ -65,8 +65,8 @@ final class ImplicitMethodResolver implements MethodResolver {
65 65
         }
66 66
         Class<?> type = targetObject.getClass();
67 67
         String cacheKey = type.getName() + "." + name;
68
-        if (cache.containsKey(cacheKey)) {
69
-            MethodExecutor executor = cache.get(cacheKey);
68
+        if (CACHE.containsKey(cacheKey)) {
69
+            MethodExecutor executor = CACHE.get(cacheKey);
70 70
             return executor == NULL_ME ? null : executor;
71 71
         }
72 72
 
@@ -75,25 +75,24 @@ final class ImplicitMethodResolver implements MethodResolver {
75 75
             int modifiers = method.getModifiers();
76 76
             if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
77 77
                 Class<?>[] parameterTypes = method.getParameterTypes();
78
-                Class<?> firstParameterType = parameterTypes[0];
78
+                Class<?> firstParamType = parameterTypes[0];
79 79
                 if (parameterTypes.length > 0
80
-                        && firstParameterType.isAssignableFrom(type)) {
81
-
82
-                    Class<?>[] modifiedArgumentTypes = new Class[argumentTypes.length + 1];
83
-                    modifiedArgumentTypes[0] = firstParameterType;
84
-                    System.arraycopy(argumentTypes, 0, modifiedArgumentTypes,
80
+                        && firstParamType.isAssignableFrom(type)) {
81
+                    Class<?>[] newArgumentTypes = new Class[argumentTypes.length + 1];
82
+                    newArgumentTypes[0] = firstParamType;
83
+                    System.arraycopy(argumentTypes, 0, newArgumentTypes,
85 84
                             1, argumentTypes.length);
86 85
                     MethodExecutor executor = new ReflectiveMethodResolver()
87 86
                             .resolve(context, method.getDeclaringClass(), name,
88
-                                    modifiedArgumentTypes);
87
+                                    newArgumentTypes);
89 88
                     MethodExecutor wrappedExecutor = executor == null ? null
90 89
                             : new ImplicitMethodExecutor(executor);
91
-                    cache.putIfAbsent(cacheKey, wrappedExecutor);
90
+                    CACHE.putIfAbsent(cacheKey, wrappedExecutor);
92 91
                     return wrappedExecutor;
93 92
                 }
94 93
             }
95 94
         }
96
-        cache.putIfAbsent(cacheKey, NULL_ME);
95
+        CACHE.putIfAbsent(cacheKey, NULL_ME);
97 96
         return null;
98 97
     }
99 98
 

+ 3
- 0
src/main/java/net/abhinavsarkar/spelhelper/ImplicitMethods.java View File

@@ -33,6 +33,9 @@ import java.util.Set;
33 33
  */
34 34
 public final class ImplicitMethods {
35 35
 
36
+    private ImplicitMethods() {
37
+    }
38
+
36 39
     /**
37 40
      * Provides implicit method `distinct` on the {@link List} class.
38 41
      *

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

@@ -29,7 +29,7 @@ import org.springframework.util.Assert;
29 29
 
30 30
 final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
31 31
 
32
-    private static final ConcurrentHashMap<String, MethodExecutor> cache =
32
+    private static final ConcurrentHashMap<String, MethodExecutor> CACHE =
33 33
         new ConcurrentHashMap<String, MethodExecutor>();
34 34
 
35 35
     public boolean canRead(final EvaluationContext context,
@@ -37,19 +37,19 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
37 37
             throws AccessException {
38 38
         Assert.notNull(target, "target is null");
39 39
         String cacheKey = target.getClass().getName() + "." + name;
40
-        if (cache.containsKey(cacheKey)) {
41
-            return cache.get(cacheKey) != null;
40
+        if (CACHE.containsKey(cacheKey)) {
41
+            return CACHE.get(cacheKey) != null;
42 42
         }
43 43
 
44 44
         for (MethodResolver mr : context.getMethodResolvers()) {
45 45
             MethodExecutor me = mr.resolve(context, target, name, new Class[0]);
46 46
             if (me != null) {
47
-                cache.putIfAbsent(cacheKey, me);
47
+                CACHE.putIfAbsent(cacheKey, me);
48 48
                 return true;
49 49
             }
50 50
         }
51 51
 
52
-        cache.putIfAbsent(cacheKey, null);
52
+        CACHE.putIfAbsent(cacheKey, null);
53 53
         return false;
54 54
     }
55 55
 
@@ -58,7 +58,7 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
58 58
             throws AccessException {
59 59
         if (canRead(context, target, name)) {
60 60
             String cacheKey = target.getClass().getName() + "." + name;
61
-            return cache.get(cacheKey).execute(context, target, new Object[0]);
61
+            return CACHE.get(cacheKey).execute(context, target, new Object[0]);
62 62
         }
63 63
         throw new AccessException(MessageFormat.format(
64 64
                 "Cannot read property: {0} of target: {1}", name, target));

+ 19
- 16
src/main/java/net/abhinavsarkar/spelhelper/InheritenceUtil.java View File

@@ -22,39 +22,42 @@ import java.util.Set;
22 22
 
23 23
 final class InheritenceUtil {
24 24
 
25
-    public static Set<Class<?>> getInheritance(final Class<?> in) {
25
+    private InheritenceUtil() {
26
+    }
27
+
28
+    public static Set<Class<?>> getInheritance(final Class<?> clazz) {
26 29
         LinkedHashSet<Class<?>> result = new LinkedHashSet<Class<?>>();
27
-        result.add(in);
28
-        getInheritance(in, result);
30
+        result.add(clazz);
31
+        getInheritance(clazz, result);
29 32
         return result;
30 33
     }
31 34
 
32 35
     /**
33 36
      * Get inheritance of type.
34 37
      *
35
-     * @param in
38
+     * @param clazz
36 39
      * @param result
37 40
      */
38
-    private static void getInheritance(final Class<?> in, final Set<Class<?>> result) {
39
-        Class<?> superclass = getSuperclass(in);
41
+    private static void getInheritance(final Class<?> clazz, final Set<Class<?>> result) {
42
+        Class<?> superclass = getSuperclass(clazz);
40 43
 
41 44
         if (superclass != null) {
42 45
             result.add(superclass);
43 46
             getInheritance(superclass, result);
44 47
         }
45 48
 
46
-        getInterfaceInheritance(in, result);
49
+        getInterfaceInheritance(clazz, result);
47 50
     }
48 51
 
49 52
     /**
50 53
      * Get interfaces that the type inherits from.
51 54
      *
52
-     * @param in
55
+     * @param clazz
53 56
      * @param result
54 57
      */
55
-    private static void getInterfaceInheritance(final Class<?> in,
58
+    private static void getInterfaceInheritance(final Class<?> clazz,
56 59
             final Set<Class<?>> result) {
57
-        for (Class<?> c : in.getInterfaces()) {
60
+        for (Class<?> c : clazz.getInterfaces()) {
58 61
             result.add(c);
59 62
             getInterfaceInheritance(c, result);
60 63
         }
@@ -63,21 +66,21 @@ final class InheritenceUtil {
63 66
     /**
64 67
      * Get superclass of class.
65 68
      *
66
-     * @param in
69
+     * @param clazz
67 70
      * @return
68 71
      */
69
-    private static Class<?> getSuperclass(final Class<?> in) {
70
-        if (in == null) {
72
+    private static Class<?> getSuperclass(final Class<?> clazz) {
73
+        if (clazz == null) {
71 74
             return null;
72 75
         }
73
-        if (in.isArray() && in != Object[].class) {
74
-            Class<?> type = in.getComponentType();
76
+        if (clazz.isArray() && clazz != Object[].class) {
77
+            Class<?> type = clazz.getComponentType();
75 78
             while (type.isArray()) {
76 79
                 type = type.getComponentType();
77 80
             }
78 81
             return type;
79 82
         }
80
-        return in.getSuperclass();
83
+        return clazz.getSuperclass();
81 84
     }
82 85
 
83 86
 }

+ 9
- 7
src/main/java/net/abhinavsarkar/spelhelper/SpelHelper.java View File

@@ -126,10 +126,10 @@ public final class SpelHelper {
126 126
     static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
127 127
 
128 128
     private static final ExpressionParser PARSER = new SpelExpressionParser();
129
-    private static final ThreadLocal<EvaluationContext> currentContext =
129
+    private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
130 130
         new ThreadLocal<EvaluationContext>();
131 131
 
132
-    private volatile EvaluationContext context;
132
+    private EvaluationContext context;
133 133
     private final Set<Method> registeredFunctions = new HashSet<Method>();
134 134
     private final Map<String,Method> registeredMethods =
135 135
         new ConcurrentHashMap<String, Method>();
@@ -177,7 +177,9 @@ public final class SpelHelper {
177 177
      */
178 178
     public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
179 179
         registeredFunctions.addAll(filterFunctions(clazz));
180
-        context = null;
180
+        synchronized (PARSER) {
181
+            context = null;
182
+        }
181 183
         return this;
182 184
     }
183 185
 
@@ -214,9 +216,9 @@ public final class SpelHelper {
214 216
     public <T> T evalExpression(final String expressionString,
215 217
             final Object rootElement, final Class<T> desiredType) {
216 218
         EvaluationContext evaluationContext = getEvaluationContext(rootElement);
217
-        currentContext.set(evaluationContext);
219
+        CURRENT_CONTEXT.set(evaluationContext);
218 220
         T value = evalExpression(expressionString, evaluationContext, desiredType);
219
-        currentContext.set(null);
221
+        CURRENT_CONTEXT.set(null);
220 222
         return value;
221 223
     }
222 224
 
@@ -320,7 +322,7 @@ public final class SpelHelper {
320 322
      * Looks up an implicit constructor registered with this instance.
321 323
      * @param lookup    key to lookup which should be of form:
322 324
      * `constructor.getDeclaringClass().getSimpleName()`
323
-     *  `+ Arrays.toString(constructor.getParameterTypes())`
325
+     * `+ Arrays.toString(constructor.getParameterTypes())`
324 326
      * @return  The registered constructor if found, else null.
325 327
      */
326 328
     public Constructor<?> lookupImplicitConstructor(final String lookup) {
@@ -333,7 +335,7 @@ public final class SpelHelper {
333 335
      * @return  The current evaluation context.
334 336
      */
335 337
     public static EvaluationContext getCurrentContext() {
336
-        return currentContext.get();
338
+        return CURRENT_CONTEXT.get();
337 339
     }
338 340
 
339 341
     private static List<Method> filterMethods(final Class<?> clazz) {

Loading…
Cancel
Save