Java SQL Warning getCause(Throwable throwable)

Here you can find the source of getCause(Throwable throwable)

Description

get Cause

License

Apache License

Declaration

public static Throwable getCause(Throwable throwable) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

public class Main {
    protected static String[] CAUSE_METHOD_NAMES = { "getCause", "getNextException", "getTargetException",
            "getException", "getSourceException", "getRootCause", "getCausedByException", "getNested" };

    public static Throwable getCause(Throwable throwable) {
        return getCause(throwable, CAUSE_METHOD_NAMES);
    }/* w  w w. j a va2 s . co  m*/

    public static Throwable getCause(Throwable throwable, String[] methodNames) {
        Throwable cause = getCauseUsingWellKnownTypes(throwable);
        if (cause == null) {
            for (String methodName : methodNames) {
                cause = getCauseUsingMethodName(throwable, methodName);
                if (cause != null) {
                    break;
                }
            }

            if (cause == null) {
                cause = getCauseUsingFieldName(throwable, "detail");
            }
        }
        return cause;
    }

    private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) {
        if (throwable instanceof SQLException) {
            return ((SQLException) throwable).getNextException();
        } else if (throwable instanceof InvocationTargetException) {
            return ((InvocationTargetException) throwable).getTargetException();
        } else {
            return null;
        }
    }

    private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName) {
        Method method = null;
        try {
            method = throwable.getClass().getMethod(methodName, null);
        } catch (NoSuchMethodException ignored) {
        } catch (SecurityException ignored) {
        }

        if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) {
            try {
                return (Throwable) method.invoke(throwable, new Object[0]);
            } catch (IllegalAccessException ignored) {
            } catch (IllegalArgumentException ignored) {
            } catch (InvocationTargetException ignored) {
            }
        }
        return null;
    }

    private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) {
        Field field = null;
        try {
            field = throwable.getClass().getField(fieldName);
        } catch (NoSuchFieldException ignored) {
        } catch (SecurityException ignored) {
        }

        if (field != null && Throwable.class.isAssignableFrom(field.getType())) {
            try {
                return (Throwable) field.get(throwable);
            } catch (IllegalAccessException ignored) {
            } catch (IllegalArgumentException ignored) {
            }
        }
        return null;
    }
}

Related

  1. clearWarnings(Connection rawConnection)
  2. getCause(Throwable throwable)
  3. getCauseUsingWellKnownTypes(Throwable throwable)
  4. getCauseUsingWellKnownTypes(Throwable throwable)
  5. getExceptionCauseUsingWellKnownTypes(final Throwable exception)
  6. getFullStackTrace(Throwable t)