001/**
002 * Copyright (C) 2006-2022 Talend Inc. - www.talend.com
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 * http://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 */
016package org.talend.sdk.component.runtime.base.lang.exception;
017
018import java.lang.reflect.InvocationTargetException;
019import java.util.Collection;
020import java.util.HashSet;
021import java.util.Objects;
022import java.util.Set;
023import java.util.stream.Stream;
024
025import org.talend.sdk.component.api.exception.ComponentException;
026
027public class InvocationExceptionWrapper {
028
029    /**
030     * Wrap the target exception in a Runtime exception
031     *
032     * @param e the exception to wrap in a way which will remove classloader specific exceptions.
033     */
034    public static RuntimeException toRuntimeException(final InvocationTargetException e) {
035        final Set<Throwable> visited = new HashSet<>();
036        visited.add(e.getTargetException());
037        return mapException(e.getTargetException(), visited);
038    }
039
040    private static RuntimeException mapException(final Throwable targetException, final Collection<Throwable> visited) {
041        if (targetException == null) {
042            return null;
043        }
044        if (ComponentException.class.isInstance(targetException)) {
045            return ComponentException.class.cast(targetException);
046        }
047        if (RuntimeException.class.isInstance(targetException)
048                && targetException.getClass().getName().startsWith("java.")) {
049            final RuntimeException cast = RuntimeException.class.cast(targetException);
050            if (cast.getCause() == null
051                    || (cast.getCause() != null && cast.getCause().getClass().getName().startsWith("java."))) {
052                return cast;
053            } // else, let it be wrapped to ensure all the stack is serializable
054        }
055        final ComponentException exception = new ComponentException(targetException.getClass().getName(),
056                targetException.getMessage(), targetException.getStackTrace(), mapCause(targetException, visited));
057        if (exception.getSuppressed() != null && exception.getSuppressed().length > 0) {
058            Stream
059                    .of(exception.getSuppressed())
060                    .map(it -> mapCause(it, new HashSet<>()))
061                    .filter(Objects::nonNull)
062                    .forEach(exception::addSuppressed);
063        }
064        return exception;
065    }
066
067    private static Throwable mapCause(final Throwable targetException, final Collection<Throwable> visited) {
068        final Throwable cause = targetException.getCause();
069        if (cause == null || !visited.add(cause)) {
070            return null;
071        }
072        return mapException(cause, visited);
073    }
074}