Corrected PMD violations

master
Abhinav Sarkar 2010-05-27 21:56:11 +05:30
parent ffebf29ec3
commit 555b0cc383
6 changed files with 50 additions and 42 deletions

1
.gitignore vendored
View File

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

View File

@ -30,7 +30,7 @@ import org.springframework.expression.spel.support.ReflectiveMethodResolver;
final class ImplicitMethodResolver implements MethodResolver { final class ImplicitMethodResolver implements MethodResolver {
private static final ConcurrentHashMap<String, MethodExecutor> cache = private static final ConcurrentHashMap<String, MethodExecutor> CACHE =
new ConcurrentHashMap<String, MethodExecutor>(); new ConcurrentHashMap<String, MethodExecutor>();
private static final MethodExecutor NULL_ME = new MethodExecutor() { private static final MethodExecutor NULL_ME = new MethodExecutor() {
@ -44,7 +44,7 @@ final class ImplicitMethodResolver implements MethodResolver {
MethodExecutor { MethodExecutor {
private final MethodExecutor executor; private final MethodExecutor executor;
private ImplicitMethodExecutor(final MethodExecutor executor) { public ImplicitMethodExecutor(final MethodExecutor executor) {
this.executor = executor; this.executor = executor;
} }
@ -65,8 +65,8 @@ final class ImplicitMethodResolver implements MethodResolver {
} }
Class<?> type = targetObject.getClass(); Class<?> type = targetObject.getClass();
String cacheKey = type.getName() + "." + name; String cacheKey = type.getName() + "." + name;
if (cache.containsKey(cacheKey)) { if (CACHE.containsKey(cacheKey)) {
MethodExecutor executor = cache.get(cacheKey); MethodExecutor executor = CACHE.get(cacheKey);
return executor == NULL_ME ? null : executor; return executor == NULL_ME ? null : executor;
} }
@ -75,25 +75,24 @@ final class ImplicitMethodResolver implements MethodResolver {
int modifiers = method.getModifiers(); int modifiers = method.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) { if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
Class<?>[] parameterTypes = method.getParameterTypes(); Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> firstParameterType = parameterTypes[0]; Class<?> firstParamType = parameterTypes[0];
if (parameterTypes.length > 0 if (parameterTypes.length > 0
&& firstParameterType.isAssignableFrom(type)) { && firstParamType.isAssignableFrom(type)) {
Class<?>[] newArgumentTypes = new Class[argumentTypes.length + 1];
Class<?>[] modifiedArgumentTypes = new Class[argumentTypes.length + 1]; newArgumentTypes[0] = firstParamType;
modifiedArgumentTypes[0] = firstParameterType; System.arraycopy(argumentTypes, 0, newArgumentTypes,
System.arraycopy(argumentTypes, 0, modifiedArgumentTypes,
1, argumentTypes.length); 1, argumentTypes.length);
MethodExecutor executor = new ReflectiveMethodResolver() MethodExecutor executor = new ReflectiveMethodResolver()
.resolve(context, method.getDeclaringClass(), name, .resolve(context, method.getDeclaringClass(), name,
modifiedArgumentTypes); newArgumentTypes);
MethodExecutor wrappedExecutor = executor == null ? null MethodExecutor wrappedExecutor = executor == null ? null
: new ImplicitMethodExecutor(executor); : new ImplicitMethodExecutor(executor);
cache.putIfAbsent(cacheKey, wrappedExecutor); CACHE.putIfAbsent(cacheKey, wrappedExecutor);
return wrappedExecutor; return wrappedExecutor;
} }
} }
} }
cache.putIfAbsent(cacheKey, NULL_ME); CACHE.putIfAbsent(cacheKey, NULL_ME);
return null; return null;
} }

View File

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

View File

@ -29,7 +29,7 @@ import org.springframework.util.Assert;
final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor { final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
private static final ConcurrentHashMap<String, MethodExecutor> cache = private static final ConcurrentHashMap<String, MethodExecutor> CACHE =
new ConcurrentHashMap<String, MethodExecutor>(); new ConcurrentHashMap<String, MethodExecutor>();
public boolean canRead(final EvaluationContext context, public boolean canRead(final EvaluationContext context,
@ -37,19 +37,19 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
throws AccessException { throws AccessException {
Assert.notNull(target, "target is null"); Assert.notNull(target, "target is null");
String cacheKey = target.getClass().getName() + "." + name; String cacheKey = target.getClass().getName() + "." + name;
if (cache.containsKey(cacheKey)) { if (CACHE.containsKey(cacheKey)) {
return cache.get(cacheKey) != null; return CACHE.get(cacheKey) != null;
} }
for (MethodResolver mr : context.getMethodResolvers()) { for (MethodResolver mr : context.getMethodResolvers()) {
MethodExecutor me = mr.resolve(context, target, name, new Class[0]); MethodExecutor me = mr.resolve(context, target, name, new Class[0]);
if (me != null) { if (me != null) {
cache.putIfAbsent(cacheKey, me); CACHE.putIfAbsent(cacheKey, me);
return true; return true;
} }
} }
cache.putIfAbsent(cacheKey, null); CACHE.putIfAbsent(cacheKey, null);
return false; return false;
} }
@ -58,7 +58,7 @@ final class ImplicitPropertyAccessor extends ReadOnlyGenericPropertyAccessor {
throws AccessException { throws AccessException {
if (canRead(context, target, name)) { if (canRead(context, target, name)) {
String cacheKey = target.getClass().getName() + "." + name; String cacheKey = target.getClass().getName() + "." + name;
return cache.get(cacheKey).execute(context, target, new Object[0]); return CACHE.get(cacheKey).execute(context, target, new Object[0]);
} }
throw new AccessException(MessageFormat.format( throw new AccessException(MessageFormat.format(
"Cannot read property: {0} of target: {1}", name, target)); "Cannot read property: {0} of target: {1}", name, target));

View File

@ -22,39 +22,42 @@ import java.util.Set;
final class InheritenceUtil { final class InheritenceUtil {
public static Set<Class<?>> getInheritance(final Class<?> in) { private InheritenceUtil() {
}
public static Set<Class<?>> getInheritance(final Class<?> clazz) {
LinkedHashSet<Class<?>> result = new LinkedHashSet<Class<?>>(); LinkedHashSet<Class<?>> result = new LinkedHashSet<Class<?>>();
result.add(in); result.add(clazz);
getInheritance(in, result); getInheritance(clazz, result);
return result; return result;
} }
/** /**
* Get inheritance of type. * Get inheritance of type.
* *
* @param in * @param clazz
* @param result * @param result
*/ */
private static void getInheritance(final Class<?> in, final Set<Class<?>> result) { private static void getInheritance(final Class<?> clazz, final Set<Class<?>> result) {
Class<?> superclass = getSuperclass(in); Class<?> superclass = getSuperclass(clazz);
if (superclass != null) { if (superclass != null) {
result.add(superclass); result.add(superclass);
getInheritance(superclass, result); getInheritance(superclass, result);
} }
getInterfaceInheritance(in, result); getInterfaceInheritance(clazz, result);
} }
/** /**
* Get interfaces that the type inherits from. * Get interfaces that the type inherits from.
* *
* @param in * @param clazz
* @param result * @param result
*/ */
private static void getInterfaceInheritance(final Class<?> in, private static void getInterfaceInheritance(final Class<?> clazz,
final Set<Class<?>> result) { final Set<Class<?>> result) {
for (Class<?> c : in.getInterfaces()) { for (Class<?> c : clazz.getInterfaces()) {
result.add(c); result.add(c);
getInterfaceInheritance(c, result); getInterfaceInheritance(c, result);
} }
@ -63,21 +66,21 @@ final class InheritenceUtil {
/** /**
* Get superclass of class. * Get superclass of class.
* *
* @param in * @param clazz
* @return * @return
*/ */
private static Class<?> getSuperclass(final Class<?> in) { private static Class<?> getSuperclass(final Class<?> clazz) {
if (in == null) { if (clazz == null) {
return null; return null;
} }
if (in.isArray() && in != Object[].class) { if (clazz.isArray() && clazz != Object[].class) {
Class<?> type = in.getComponentType(); Class<?> type = clazz.getComponentType();
while (type.isArray()) { while (type.isArray()) {
type = type.getComponentType(); type = type.getComponentType();
} }
return type; return type;
} }
return in.getSuperclass(); return clazz.getSuperclass();
} }
} }

View File

@ -126,10 +126,10 @@ public final class SpelHelper {
static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName(); static final String CONTEXT_LOOKUP_KEY = SpelHelper.class.getName();
private static final ExpressionParser PARSER = new SpelExpressionParser(); private static final ExpressionParser PARSER = new SpelExpressionParser();
private static final ThreadLocal<EvaluationContext> currentContext = private static final ThreadLocal<EvaluationContext> CURRENT_CONTEXT =
new ThreadLocal<EvaluationContext>(); new ThreadLocal<EvaluationContext>();
private volatile EvaluationContext context; private EvaluationContext context;
private final Set<Method> registeredFunctions = new HashSet<Method>(); private final Set<Method> registeredFunctions = new HashSet<Method>();
private final Map<String,Method> registeredMethods = private final Map<String,Method> registeredMethods =
new ConcurrentHashMap<String, Method>(); new ConcurrentHashMap<String, Method>();
@ -177,7 +177,9 @@ public final class SpelHelper {
*/ */
public SpelHelper registerFunctionsFromClass(final Class<?> clazz) { public SpelHelper registerFunctionsFromClass(final Class<?> clazz) {
registeredFunctions.addAll(filterFunctions(clazz)); registeredFunctions.addAll(filterFunctions(clazz));
context = null; synchronized (PARSER) {
context = null;
}
return this; return this;
} }
@ -214,9 +216,9 @@ public final class SpelHelper {
public <T> T evalExpression(final String expressionString, public <T> T evalExpression(final String expressionString,
final Object rootElement, final Class<T> desiredType) { final Object rootElement, final Class<T> desiredType) {
EvaluationContext evaluationContext = getEvaluationContext(rootElement); EvaluationContext evaluationContext = getEvaluationContext(rootElement);
currentContext.set(evaluationContext); CURRENT_CONTEXT.set(evaluationContext);
T value = evalExpression(expressionString, evaluationContext, desiredType); T value = evalExpression(expressionString, evaluationContext, desiredType);
currentContext.set(null); CURRENT_CONTEXT.set(null);
return value; return value;
} }
@ -320,7 +322,7 @@ public final class SpelHelper {
* Looks up an implicit constructor registered with this instance. * Looks up an implicit constructor registered with this instance.
* @param lookup key to lookup which should be of form: * @param lookup key to lookup which should be of form:
* `constructor.getDeclaringClass().getSimpleName()` * `constructor.getDeclaringClass().getSimpleName()`
* `+ Arrays.toString(constructor.getParameterTypes())` * `+ Arrays.toString(constructor.getParameterTypes())`
* @return The registered constructor if found, else null. * @return The registered constructor if found, else null.
*/ */
public Constructor<?> lookupImplicitConstructor(final String lookup) { public Constructor<?> lookupImplicitConstructor(final String lookup) {
@ -333,7 +335,7 @@ public final class SpelHelper {
* @return The current evaluation context. * @return The current evaluation context.
*/ */
public static EvaluationContext getCurrentContext() { public static EvaluationContext getCurrentContext() {
return currentContext.get(); return CURRENT_CONTEXT.get();
} }
private static List<Method> filterMethods(final Class<?> clazz) { private static List<Method> filterMethods(final Class<?> clazz) {