package com.github.bedrin.jdbc.sniffer.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.github.bedrin.jdbc.sniffer.util.StringUtil.LINE_SEPARATOR;

/**
 * Created by bedrin on 22.03.2015.
 */
public class ExceptionUtil {

    protected ExceptionUtil() {

    }

    private final static Method addSuppressedMethod = getAddSuppressedMethod();

    private static Method getAddSuppressedMethod() {
        try {
            Class<Throwable> throwableClass = Throwable.class;
            return throwableClass.getMethod("addSuppressed", Throwable.class);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    public static void throwException(Throwable e) {
        ExceptionUtil.<RuntimeException>throwAny(e);
    }

    @SuppressWarnings("unchecked")
    private static <E extends Throwable> void throwAny(Throwable e) throws E {
        throw (E)e;
    }

    public static boolean addSuppressed(Throwable e, Throwable suppressed) {
        if (null == addSuppressedMethod) {
            return false;
        } else {
            try {
                addSuppressedMethod.invoke(e, suppressed);
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
            } catch (InvocationTargetException ite) {
                ite.printStackTrace();
            }
            return true;
        }
    }

    public static String generateMessage(String s, StackTraceElement[] closeStackTrace) {
        StringBuilder sb = new StringBuilder(s);
        for (StackTraceElement traceElement : closeStackTrace) {
            sb.append(LINE_SEPARATOR).append("\tat ").append(traceElement);
        }
        return sb.toString();
    }

}

@@ -39,16 +39,54 @@
 .util;%0A%0A
+import java.lang.reflect.Constructor;%0A
 import j
@@ -723,32 +723,814 @@
        %7D%0A    %7D%0A%0A
+    public static boolean throwException(String className, String message) %7B%0A        try %7B%0A            Class%3CThrowable%3E throwableClass = (Class%3CThrowable%3E)Class.forName(className);%0A            Constructor%3CThrowable%3E constructor = throwableClass.getConstructor(String.class);%0A            Throwable throwable = constructor.newInstance(message);%0A            throwException(throwable);%0A            return true;%0A        %7D catch (ClassNotFoundException e) %7B%0A            return false;%0A        %7D catch (NoSuchMethodException e) %7B%0A            return false;%0A        %7D catch (InvocationTargetException e) %7B%0A            return false;%0A        %7D catch (InstantiationException e) %7B%0A            return false;%0A        %7D catch (IllegalAccessException e) %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A%0A
     public stati

throw exception by given class name and message