001/*
002 * Copyright 2002-2018 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.Comparator;
022import java.util.List;
023
024import org.springframework.util.Assert;
025
026/**
027 * Comparator capable of sorting exceptions based on their depth from the thrown exception type.
028 *
029 * @author Juergen Hoeller
030 * @author Arjen Poutsma
031 * @since 3.0.3
032 */
033public class ExceptionDepthComparator implements Comparator<Class<? extends Throwable>> {
034
035        private final Class<? extends Throwable> targetException;
036
037
038        /**
039         * Create a new ExceptionDepthComparator for the given exception.
040         * @param exception the target exception to compare to when sorting by depth
041         */
042        public ExceptionDepthComparator(Throwable exception) {
043                Assert.notNull(exception, "Target exception must not be null");
044                this.targetException = exception.getClass();
045        }
046
047        /**
048         * Create a new ExceptionDepthComparator for the given exception type.
049         * @param exceptionType the target exception type to compare to when sorting by depth
050         */
051        public ExceptionDepthComparator(Class<? extends Throwable> exceptionType) {
052                Assert.notNull(exceptionType, "Target exception type must not be null");
053                this.targetException = exceptionType;
054        }
055
056
057        @Override
058        public int compare(Class<? extends Throwable> o1, Class<? extends Throwable> o2) {
059                int depth1 = getDepth(o1, this.targetException, 0);
060                int depth2 = getDepth(o2, this.targetException, 0);
061                return (depth1 - depth2);
062        }
063
064        private int getDepth(Class<?> declaredException, Class<?> exceptionToMatch, int depth) {
065                if (exceptionToMatch.equals(declaredException)) {
066                        // Found it!
067                        return depth;
068                }
069                // If we've gone as far as we can go and haven't found it...
070                if (exceptionToMatch == Throwable.class) {
071                        return Integer.MAX_VALUE;
072                }
073                return getDepth(declaredException, exceptionToMatch.getSuperclass(), depth + 1);
074        }
075
076
077        /**
078         * Obtain the closest match from the given exception types for the given target exception.
079         * @param exceptionTypes the collection of exception types
080         * @param targetException the target exception to find a match for
081         * @return the closest matching exception type from the given collection
082         */
083        public static Class<? extends Throwable> findClosestMatch(
084                        Collection<Class<? extends Throwable>> exceptionTypes, Throwable targetException) {
085
086                Assert.notEmpty(exceptionTypes, "Exception types must not be empty");
087                if (exceptionTypes.size() == 1) {
088                        return exceptionTypes.iterator().next();
089                }
090                List<Class<? extends Throwable>> handledExceptions = new ArrayList<>(exceptionTypes);
091                handledExceptions.sort(new ExceptionDepthComparator(targetException));
092                return handledExceptions.get(0);
093        }
094
095}