Skip to content

Commit

Permalink
Fix race condition in AnntationMethodHER
Browse files Browse the repository at this point in the history
Issues: SPR-9138
  • Loading branch information
rstoyanchev committed Apr 2, 2012
1 parent 64ee5e5 commit 0b02933
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExceptionResolver {

// dummy method placeholder
private static final Method NO_METHOD_FOUND = ClassUtils
.getMethodIfAvailable(System.class, "currentTimeMillis", null);
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis", (Class<?>[]) null);

private WebArgumentResolver[] customArgumentResolvers;

Expand Down Expand Up @@ -134,10 +133,9 @@ protected ModelAndView doResolveException(
private Method findBestExceptionHandlerMethod(Object handler, final Exception thrownException) {
final Class<?> handlerType = handler.getClass();
final Class<? extends Throwable> thrownExceptionType = thrownException.getClass();

Method handlerMethod = null;
Map<Class<? extends Throwable>, Method> handlers = exceptionHandlerCache
.get(handlerType);

Map<Class<? extends Throwable>, Method> handlers = exceptionHandlerCache.get(handlerType);

if (handlers != null) {
handlerMethod = handlers.get(thrownExceptionType);
Expand Down Expand Up @@ -173,7 +171,9 @@ public void doWith(Method method) {
}
});

return getBestMatchingMethod(resolverMethods, thrownException);
handlerMethod = getBestMatchingMethod(resolverMethods, thrownException);
handlers.put(thrownExceptionType, (handlerMethod == null ? NO_METHOD_FOUND : handlerMethod));
return handlerMethod;
}

/**
Expand Down Expand Up @@ -204,19 +204,19 @@ protected List<Class<? extends Throwable>> getHandledExceptions(Method method) {
}

/**
* Returns the best matching method. Uses the {@link DepthComparator}.
* Uses the {@link DepthComparator} to find the best matching method
* @return the best matching method or {@code null}.
*/
private Method getBestMatchingMethod(
Map<Class<? extends Throwable>, Method> resolverMethods, Exception thrownException) {

if (!resolverMethods.isEmpty()) {
Class<? extends Throwable> closestMatch =
ExceptionDepthComparator.findClosestMatch(resolverMethods.keySet(), thrownException);
return resolverMethods.get(closestMatch);
}
else {
if (resolverMethods.isEmpty()) {
return null;
}
Class<? extends Throwable> closestMatch =
ExceptionDepthComparator.findClosestMatch(resolverMethods.keySet(), thrownException);
Method method = resolverMethods.get(closestMatch);
return ((method == null) || (NO_METHOD_FOUND == method)) ? null : method;
}

/**
Expand All @@ -225,13 +225,13 @@ private Method getBestMatchingMethod(
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
NativeWebRequest webRequest, Exception thrownException) throws Exception {

Class[] paramTypes = handlerMethod.getParameterTypes();
Class<?>[] paramTypes = handlerMethod.getParameterTypes();
Object[] args = new Object[paramTypes.length];
Class<?> handlerType = handler.getClass();
for (int i = 0; i < args.length; i++) {
MethodParameter methodParam = new MethodParameter(handlerMethod, i);
GenericTypeResolver.resolveParameterType(methodParam, handlerType);
Class paramType = methodParam.getParameterType();
Class<?> paramType = methodParam.getParameterType();
Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
if (argValue != WebArgumentResolver.UNRESOLVED) {
args[i] = argValue;
Expand Down Expand Up @@ -267,7 +267,7 @@ protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWe
}

// Resolution of standard parameter types...
Class paramType = methodParameter.getParameterType();
Class<?> paramType = methodParameter.getParameterType();
Object value = resolveStandardArgument(paramType, webRequest, thrownException);
if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) {
throw new IllegalStateException("Standard argument type [" + paramType.getName() +
Expand All @@ -287,7 +287,7 @@ protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWe
* @param thrownException the exception thrown
* @return the argument value, or {@link org.springframework.web.bind.support.WebArgumentResolver#UNRESOLVED}
*/
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest,
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest,
Exception thrownException) throws Exception {

if (parameterType.isInstance(thrownException)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExceptionResolver {

// dummy method placeholder
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis", null);
private final Map<Class<?>, Map<Class<? extends Throwable>, Method>> exceptionHandlerCache =
new ConcurrentHashMap<Class<?>, Map<Class<? extends Throwable>, Method>>();
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis", (Class<?>[]) null);

private final Map<Class<?>, Map<Class<? extends Throwable>, Method>> exceptionHandlerCache =
new ConcurrentHashMap<Class<?>, Map<Class<? extends Throwable>, Method>>();

private WebArgumentResolver[] customArgumentResolvers;

Expand Down Expand Up @@ -157,7 +157,7 @@ private Method findBestExceptionHandlerMethod(Object handler, final Exception th
final Class<?> handlerType = ClassUtils.getUserClass(handler);
final Class<? extends Throwable> thrownExceptionType = thrownException.getClass();
Method handlerMethod = null;

Map<Class<? extends Throwable>, Method> handlers = exceptionHandlerCache.get(handlerType);

if (handlers != null) {
Expand All @@ -170,7 +170,7 @@ private Method findBestExceptionHandlerMethod(Object handler, final Exception th
handlers = new ConcurrentHashMap<Class<? extends Throwable>, Method>();
exceptionHandlerCache.put(handlerType, handlers);
}

final Map<Class<? extends Throwable>, Method> resolverMethods = handlers;

ReflectionUtils.doWithMethods(handlerType, new ReflectionUtils.MethodCallback() {
Expand Down Expand Up @@ -228,19 +228,19 @@ protected List<Class<? extends Throwable>> getHandledExceptions(Method method) {
}

/**
* Returns the best matching method. Uses the {@link DepthComparator}.
* Uses the {@link DepthComparator} to find the best matching method
* @return the best matching method or {@code null}.
*/
private Method getBestMatchingMethod(
Map<Class<? extends Throwable>, Method> resolverMethods, Exception thrownException) {

if (!resolverMethods.isEmpty()) {
Class<? extends Throwable> closestMatch =
ExceptionDepthComparator.findClosestMatch(resolverMethods.keySet(), thrownException);
return resolverMethods.get(closestMatch);
}
else {
if (resolverMethods.isEmpty()) {
return null;
}
Class<? extends Throwable> closestMatch =
ExceptionDepthComparator.findClosestMatch(resolverMethods.keySet(), thrownException);
Method method = resolverMethods.get(closestMatch);
return ((method == null) || (NO_METHOD_FOUND == method)) ? null : method;
}

/**
Expand All @@ -249,13 +249,13 @@ private Method getBestMatchingMethod(
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
NativeWebRequest webRequest, Exception thrownException) throws Exception {

Class[] paramTypes = handlerMethod.getParameterTypes();
Class<?>[] paramTypes = handlerMethod.getParameterTypes();
Object[] args = new Object[paramTypes.length];
Class<?> handlerType = handler.getClass();
for (int i = 0; i < args.length; i++) {
MethodParameter methodParam = new MethodParameter(handlerMethod, i);
GenericTypeResolver.resolveParameterType(methodParam, handlerType);
Class paramType = methodParam.getParameterType();
Class<?> paramType = methodParam.getParameterType();
Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
if (argValue != WebArgumentResolver.UNRESOLVED) {
args[i] = argValue;
Expand Down Expand Up @@ -290,7 +290,7 @@ protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWe
}

// Resolution of standard parameter types...
Class paramType = methodParameter.getParameterType();
Class<?> paramType = methodParameter.getParameterType();
Object value = resolveStandardArgument(paramType, webRequest, thrownException);
if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) {
throw new IllegalStateException(
Expand All @@ -311,7 +311,7 @@ protected Object resolveCommonArgument(MethodParameter methodParameter, NativeWe
* @param thrownException the exception thrown
* @return the argument value, or {@link WebArgumentResolver#UNRESOLVED}
*/
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest,
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest,
Exception thrownException) throws Exception {

if (parameterType.isInstance(thrownException)) {
Expand Down Expand Up @@ -395,7 +395,7 @@ else if (returnValue instanceof Model) {
return new ModelAndView().addAllObjects(((Model) returnValue).asMap());
}
else if (returnValue instanceof Map) {
return new ModelAndView().addAllObjects((Map) returnValue);
return new ModelAndView().addAllObjects((Map<String, Object>) returnValue);
}
else if (returnValue instanceof View) {
return new ModelAndView((View) returnValue);
Expand Down

0 comments on commit 0b02933

Please sign in to comment.