Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2013 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.messaging.handler.invocation;
 
 
 import java.util.*;
Cache exception handling method mappings and provide options to look up a method that should handle an exception. If multiple methods match, they are sorted using org.springframework.core.ExceptionDepthComparator and the top match is returned.

Author(s):
Rossen Stoyanchev
Since:
4.0
 
 public abstract class AbstractExceptionHandlerMethodResolver {
 
 	private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class"currentTimeMillis");
 
 	private final Map<Class<? extends Throwable>, MethodmappedMethods = new ConcurrentHashMap<Class<? extends Throwable>, Method>(16);
 
 	private final Map<Class<? extends Throwable>, MethodexceptionLookupCache = new ConcurrentHashMap<Class<? extends Throwable>, Method>(16);


Protected constructor accepting exception-to-method mappings.
 
 	protected AbstractExceptionHandlerMethodResolver(Map<Class<? extends Throwable>, MethodmappedMethods) {
 		Assert.notNull(mappedMethods"Mapped Methods must not be null");
 		this..putAll(mappedMethods);
 	}

Extract the exceptions this method handles.This implementation looks for sub-classes of Throwable in the method signature. The method is static to ensure safe use from sub-class constructors.
 
 	@SuppressWarnings("unchecked")
 	protected static List<Class<? extends Throwable>> getExceptionsFromMethodSignature(Method method) {
 		List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>();
 		for (Class<?> paramType : method.getParameterTypes()) {
 			if (Throwable.class.isAssignableFrom(paramType)) {
 				result.add((Class<? extends Throwable>) paramType);
 			}
 		}
 		Assert.notEmpty(result"No exception types mapped to {" + method + "}");
 		return result;
 	}

Whether the contained type has any exception mappings.
 
 	public boolean hasExceptionMappings() {
 		return (this..size() > 0);
 	}

Find a method to handle the given exception. Use org.springframework.core.ExceptionDepthComparator if more than one match is found.

Parameters:
exception the exception
Returns:
a method to handle the exception or null
 
 	public Method resolveMethod(Exception exception) {
 		Class<? extends ExceptionexceptionType = exception.getClass();
 		Method method = this..get(exceptionType);
 		if (method == null) {
 			method = getMappedMethod(exceptionType);
 			this..put(exceptionTypemethod != null ? method : );
 		}
 		return method !=  ? method : null;
 	}

Return the method mapped to the given exception type or null.
 
 	private Method getMappedMethod(Class<? extends ExceptionexceptionType) {
 		List<Class<? extends Throwable>> matches = new ArrayList<Class<? extends Throwable>>();
 		for(Class<? extends ThrowablemappedException : this..keySet()) {
 			if (mappedException.isAssignableFrom(exceptionType)) {
 				matches.add(mappedException);
			}
		}
		if (!matches.isEmpty()) {
			Collections.sort(matchesnew ExceptionDepthComparator(exceptionType));
			return this..get(matches.get(0));
		}
		else {
			return null;
		}
	}
New to GrepCode? Check out our FAQ X