001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.Comparator; 023import java.util.List; 024 025import org.springframework.util.Assert; 026 027/** 028 * Comparator capable of sorting exceptions based on their depth from the thrown exception type. 029 * 030 * @author Juergen Hoeller 031 * @author Arjen Poutsma 032 * @since 3.0.3 033 */ 034public class ExceptionDepthComparator implements Comparator<Class<? extends Throwable>> { 035 036 private final Class<? extends Throwable> targetException; 037 038 039 /** 040 * Create a new ExceptionDepthComparator for the given exception. 041 * @param exception the target exception to compare to when sorting by depth 042 */ 043 public ExceptionDepthComparator(Throwable exception) { 044 Assert.notNull(exception, "Target exception must not be null"); 045 this.targetException = exception.getClass(); 046 } 047 048 /** 049 * Create a new ExceptionDepthComparator for the given exception type. 050 * @param exceptionType the target exception type to compare to when sorting by depth 051 */ 052 public ExceptionDepthComparator(Class<? extends Throwable> exceptionType) { 053 Assert.notNull(exceptionType, "Target exception type must not be null"); 054 this.targetException = exceptionType; 055 } 056 057 058 @Override 059 public int compare(Class<? extends Throwable> o1, Class<? extends Throwable> o2) { 060 int depth1 = getDepth(o1, this.targetException, 0); 061 int depth2 = getDepth(o2, this.targetException, 0); 062 return (depth1 - depth2); 063 } 064 065 private int getDepth(Class<?> declaredException, Class<?> exceptionToMatch, int depth) { 066 if (exceptionToMatch.equals(declaredException)) { 067 // Found it! 068 return depth; 069 } 070 // If we've gone as far as we can go and haven't found it... 071 if (exceptionToMatch == Throwable.class) { 072 return Integer.MAX_VALUE; 073 } 074 return getDepth(declaredException, exceptionToMatch.getSuperclass(), depth + 1); 075 } 076 077 078 /** 079 * Obtain the closest match from the given exception types for the given target exception. 080 * @param exceptionTypes the collection of exception types 081 * @param targetException the target exception to find a match for 082 * @return the closest matching exception type from the given collection 083 */ 084 public static Class<? extends Throwable> findClosestMatch( 085 Collection<Class<? extends Throwable>> exceptionTypes, Throwable targetException) { 086 087 Assert.notEmpty(exceptionTypes, "Exception types must not be empty"); 088 if (exceptionTypes.size() == 1) { 089 return exceptionTypes.iterator().next(); 090 } 091 List<Class<? extends Throwable>> handledExceptions = 092 new ArrayList<Class<? extends Throwable>>(exceptionTypes); 093 Collections.sort(handledExceptions, new ExceptionDepthComparator(targetException)); 094 return handledExceptions.get(0); 095 } 096 097}