All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.microsphere.util.ExceptionUtils Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 io.microsphere.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import static io.microsphere.text.FormatUtils.format;
import static io.microsphere.util.ClassUtils.isAssignableFrom;
import static io.microsphere.util.ClassUtils.newInstance;

/**
 * {@link Exception} Utilities class
 *
 * @author Mercy
 * @since 1.0.0
 */
public abstract class ExceptionUtils extends BaseUtils {

    /**
     * 

Gets the stack trace from a Throwable as a String.

* *

The result of this method vary by JDK version as this method * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}. * On JDK1.3 and earlier, the cause exception will not be shown * unless the specified throwable alters printStackTrace.

* * @param throwable the Throwable to be examined * @return the stack trace as generated by the exception's * printStackTrace(PrintWriter) method */ public static String getStackTrace(final Throwable throwable) { final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); } public static TT wrap(T source, Class thrownType) { if (isAssignableFrom(thrownType, source.getClass())) { return (TT) source; } Object[] args = resolveArguments(source); return ClassUtils.newInstance(thrownType, args); } private static Object[] resolveArguments(T source) { String message = source.getMessage(); Throwable cause = source.getCause() == null ? source : source.getCause(); return message == null ? new Object[]{cause} : new Object[]{message, cause}; } public static T create(Class throwableClass, Throwable cause, String messagePattern, Object... args) { String message = format(messagePattern, args); return create(throwableClass, message, cause); } public static T create(Class throwableClass, String message, Throwable cause) { return newInstance(throwableClass, message, cause); } public static T create(Class throwableClass, Throwable cause) { return newInstance(throwableClass, cause); } public static T create(Class throwableClass, String message) { return newInstance(throwableClass, message); } public static T create(Class throwableClass) { return newInstance(throwableClass); } public static T create(Class throwableClass, Object... args) { return newInstance(throwableClass, args); } public static TT throwTarget(T source, Class thrownType) throws TT { throw wrap(source, thrownType); } }