Example usage for java.lang Throwable getClass

List of usage examples for java.lang Throwable getClass

Introduction

In this page you can find the example usage for java.lang Throwable getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:io.opentracing.contrib.elasticsearch.common.SpanDecorator.java

private static Map<String, Object> errorLogs(Throwable throwable) {
    Map<String, Object> errorLogs = new HashMap<>(4);
    errorLogs.put("event", Tags.ERROR.getKey());
    errorLogs.put("error.kind", throwable.getClass().getName());
    errorLogs.put("error.object", throwable);

    errorLogs.put("message", throwable.getMessage());

    StringWriter sw = new StringWriter();
    throwable.printStackTrace(new PrintWriter(sw));
    errorLogs.put("stack", sw.toString());

    return errorLogs;
}

From source file:info.magnolia.cms.util.ExceptionUtil.java

/**
 * Returns true if the given exception or any of the nested cause exceptions is an instance of the <tt>suspectedCause</tt> exception argument, or a subclass thereof.
 * This is equivalent to ExceptionUtils.indexOfThrowable(e, javax.jcr.AccessDeniedException.class) >= 0, only more readable, and possibly more performant.
 *//* w  ww .j a  v  a2s . com*/
public static boolean wasCausedBy(Throwable e, Class<? extends Throwable> suspectedCause) {
    if (e != null && suspectedCause.isAssignableFrom(e.getClass())) {
        return true;
    } else if (e == null) {
        return false;
    } else {
        return wasCausedBy(e.getCause(), suspectedCause);
    }
}

From source file:edu.cornell.mannlib.vedit.util.OperationUtils.java

/**
 * Takes a bean and clones it using reflection. Any fields without standard
 * getter/setter methods will not be copied.
 */// w ww  . ja v  a  2  s .  com
public static Object cloneBean(final Object bean, final Class<?> beanClass, final Class<?> iface) {
    if (bean == null) {
        throw new NullPointerException("bean may not be null.");
    }
    if (beanClass == null) {
        throw new NullPointerException("beanClass may not be null.");
    }
    if (iface == null) {
        throw new NullPointerException("iface may not be null.");
    }

    class CloneBeanException extends RuntimeException {
        public CloneBeanException(String message, Throwable cause) {
            super(message + " <" + cause.getClass().getSimpleName() + ">: bean=" + bean + ", beanClass="
                    + beanClass.getName() + ", iface=" + iface.getName(), cause);
        }
    }

    Object newBean;
    try {
        newBean = beanClass.getConstructor().newInstance();
    } catch (NoSuchMethodException e) {
        throw new CloneBeanException("bean has no 'nullary' constructor.", e);
    } catch (InstantiationException e) {
        throw new CloneBeanException("tried to create instance of an abstract class.", e);
    } catch (IllegalAccessException e) {
        throw new CloneBeanException("bean constructor is not accessible.", e);
    } catch (InvocationTargetException e) {
        throw new CloneBeanException("bean constructor threw an exception.", e);
    } catch (Exception e) {
        throw new CloneBeanException("failed to instantiate a new bean.", e);
    }

    for (Method beanMeth : iface.getMethods()) {
        String methName = beanMeth.getName();
        if (!methName.startsWith("get")) {
            continue;
        }
        if (beanMeth.getParameterTypes().length != 0) {
            continue;
        }
        String fieldName = methName.substring(3, methName.length());
        Class<?> returnType = beanMeth.getReturnType();

        Method setterMethod;
        try {
            setterMethod = iface.getMethod("set" + fieldName, returnType);
        } catch (NoSuchMethodException nsme) {
            continue;
        }

        Object fieldVal;
        try {
            fieldVal = beanMeth.invoke(bean, (Object[]) null);
        } catch (Exception e) {
            throw new CloneBeanException("failed to invoke " + beanMeth, e);
        }

        try {
            Object[] setArgs = new Object[1];
            setArgs[0] = fieldVal;
            setterMethod.invoke(newBean, setArgs);
        } catch (Exception e) {
            throw new CloneBeanException("failed to invoke " + setterMethod, e);
        }
    }
    return newBean;
}

From source file:com.redhat.lightblue.util.Error.java

/**
 * Helper that gets a new Error with msg set to the message of the given
 * Throwable and the errorCode set to the class name of the Throwable.
 *//*from ww w .ja v  a  2  s.c  o m*/
public static Error get(Throwable e) {
    return get(e.getClass().getSimpleName(), e);
}

From source file:com.comcast.viper.flume2storm.F2SConfigurationException.java

protected static final String buildErrorMessage(String parameter, Object value, Throwable throwable) {
    return new StringBuilder("Configuration attribute \"").append(parameter).append("\" has invalid value (\"")
            .append(value).append("\"). ").append(throwable.getClass().getSimpleName()).append(": ")
            .append(throwable.getLocalizedMessage()).toString();
}

From source file:Main.java

public static String getCauses(Throwable e) {
    try {/*from  ww w.j av  a  2s  .  co  m*/
        StringBuilder sb = new StringBuilder();
        while (e != null) {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(e.getClass().getSimpleName());
            e = e.getCause();
        }

        return sb.toString();

    } catch (Throwable derp) {
        return "derp " + derp.getClass().getSimpleName();
    }
}

From source file:co.marcin.novaguilds.util.LoggerUtils.java

public static void exception(Throwable e) {
    Throwable cause = e.getCause();
    error("", false);
    error("[NovaGuilds] Severe error: " + e.getClass().getSimpleName(), false);
    error("", false);
    error("Server Information:", false);
    error("  NovaGuilds: #" + VersionUtils.getBuildCurrent() + " (" + VersionUtils.getCommit() + ")", false);
    error("  Storage Type: "
            + (plugin.getConfigManager() == null || plugin.getConfigManager().getDataStorageType() == null
                    ? "null"
                    : plugin.getConfigManager().getDataStorageType().name()),
            false);/*from w  w w .  ja va2  s .  c o  m*/
    error("  Bukkit: " + Bukkit.getBukkitVersion(), false);
    error("  Java: " + System.getProperty("java.version"), false);
    error("  Thread: " + Thread.currentThread(), false);
    error("  Running CraftBukkit: "
            + Bukkit.getServer().getClass().getName().equals("org.bukkit.craftbukkit.CraftServer"), false);
    error("  Exception Message: ", false);
    error("   " + e.getMessage(), false);
    error("", false);

    error("Stack trace: ", false);
    printStackTrace(e.getStackTrace());
    error("", false);

    while (cause != null) {
        error("Caused by: " + cause.getClass().getName(), false);
        error("  " + cause.getMessage(), false);

        printStackTrace(cause.getStackTrace());
        error("", false);
        cause = cause.getCause();
    }

    error("End of Error.", false);
    error("", false);

    //notify all permitted players
    Message.CHAT_ERROROCCURED.broadcast(Permission.NOVAGUILDS_ERROR);
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsg.CFAsteriskXMsgSchemaMessageFormatter.java

public static String formatRspnException(String separator, Throwable t) {
    String retval = "<RspnException "
            + CFLibXmlUtil.formatRequiredXmlString(null, "Name", t.getClass().getName())
            + CFLibXmlUtil.formatRequiredXmlString(separator, "Message", t.getMessage()) + " />";
    return (retval);
}

From source file:Main.java

public static String getCompressedStackTrace(Throwable t, int startAt, int limit) {
    try {//  w  w w .  jav  a  2  s  .  co m
        StackTraceElement[] stackTrace = t.getStackTrace();
        if (stackTrace.length < startAt) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        for (int i = startAt; i < stackTrace.length && i < startAt + limit; i++) {
            StackTraceElement element = stackTrace[i];
            String classname = element.getClassName();
            String cnShort;
            boolean showLineNumber = true;
            boolean breakAfter = false;
            if (classname.startsWith("com.vuze.android.remote.")) {
                cnShort = classname.substring(24, classname.length());
            } else if (classname.equals("android.os.Handler")) {
                showLineNumber = false;
                cnShort = "Handler";
            } else if (classname.equals("android.os.Looper")) {
                showLineNumber = false;
                cnShort = "Looper";
                breakAfter = true;
            } else if (classname.length() < 9) { // include full if something like aa.ab.ac
                cnShort = classname;
            } else {
                int len = classname.length();
                int start = len > 14 ? len - 14 : 0;

                int pos = classname.indexOf('.', start);
                if (pos >= 0) {
                    start = pos + 1;
                }
                cnShort = classname.substring(start, len);
            }
            if (i != startAt) {
                sb.append(", ");
            }
            sb.append(cnShort);
            sb.append('.');
            sb.append(element.getMethodName());
            if (showLineNumber) {
                sb.append(':');
                sb.append(element.getLineNumber());
            }
            if (breakAfter) {
                break;
            }
        }
        Throwable cause = t.getCause();
        if (cause != null) {
            sb.append("\n|Cause ");
            sb.append(cause.getClass().getSimpleName());
            if (cause instanceof Resources.NotFoundException || cause instanceof RuntimeException) {
                sb.append(' ');
                sb.append(cause.getMessage());
            }
            sb.append(' ');
            sb.append(getCompressedStackTrace(cause, 0, 9));
        }
        return sb.toString();
    } catch (Throwable derp) {
        return "derp " + derp.getClass().getSimpleName();
    }
}

From source file:com.squarespace.gibson.GibsonUtils.java

private static void append(MessageDigest md, Throwable throwable) {
    if (throwable != null) {
        append(md, throwable.getClass());
        append(md, getStackTrace(throwable));

        Throwable cause = throwable.getCause();
        if (cause != null && cause != throwable) {
            append(md, cause);/*from  w  w  w.j  a v  a  2 s.  c  o  m*/
        }
    }
}