Example usage for java.lang Throwable Throwable

List of usage examples for java.lang Throwable Throwable

Introduction

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

Prototype

public Throwable() 

Source Link

Document

Constructs a new throwable with null as its detail message.

Usage

From source file:com.ikon.util.StackTraceUtils.java

/**
 * Return the whole call trace.//from ww  w.  j a v  a2  s.co m
 */
public static String getTrace() {
    // The constructor for Throwable has a native function that fills the stack trace.
    StackTraceElement[] trace = (new Throwable()).getStackTrace();
    StringBuilder sb = new StringBuilder();

    // Once you have the trace you can pick out information you need.
    if (trace.length >= 2) {
        for (int i = 2; i < trace.length; i++) {
            if (trace[i].getClassName().startsWith("com.ikon")) {
                sb.append(trace[i]);
                sb.append("\n");
            }
        }
    }

    return sb.toString();
}

From source file:com.openkm.util.StackTraceUtils.java

/**
 * Return the whole call trace./*from w  w  w . jav a  2s . c  om*/
 */
public static String getTrace() {
    // The constructor for Throwable has a native function that fills the stack trace.
    StackTraceElement[] trace = (new Throwable()).getStackTrace();
    StringBuilder sb = new StringBuilder();

    // Once you have the trace you can pick out information you need.
    if (trace.length >= 2) {
        for (int i = 2; i < trace.length; i++) {
            if (trace[i].getClassName().startsWith("com.openkm")) {
                sb.append(trace[i]);
                sb.append("\n");
            }
        }
    }

    return sb.toString();
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void wtf(String message) {
    if (!isDebuggable())
        return;/* w  ww.java 2  s.  c  o m*/

    getMethodNames(new Throwable().getStackTrace());
    Log.wtf(className, createLog(message));
}

From source file:de.micromata.genome.util.jdbc.TraceCreationBasicDataSource.java

@Override
public Connection getConnection() throws SQLException {
    if (LOG.isDebugEnabled() == true) {
        LOG.debug("getConnection. max: " + getMaxTotal() + "; act: " + getNumActive());
    }// w  w w  . j a va2 s  .c om
    Connection con = super.getConnection();
    allocatedStacks.put(con, new Throwable().getStackTrace());
    ConnectionWrapper wrapped = new ConnectionWrapper(con) {

        @Override
        public void close() throws SQLException {
            super.close();
            allocatedStacks.remove(getNestedConnection());
            if (LOG.isDebugEnabled() == true) {
                LOG.debug("close. max: " + getMaxTotal() + "; act: " + getNumActive());
            }
        }

    };
    return wrapped;
}

From source file:de.micromata.genome.logging.LogStacktraceAttribute.java

/**
 * Instantiates a new log stacktrace attribute.
 *
 * @param skipLeadingStacktraces the skip leading stacktraces
 *///from  www  .  ja  va  2 s  . co m
public LogStacktraceAttribute(String... skipLeadingStacktraces) {
    super(GenomeAttributeType.Stacktrace, "");
    this.skipLeadingStacktraces = (String[]) ArrayUtils.add(skipLeadingStacktraces,
            "de.micromata.genome.logging.LogStacktraceAttribute");
    stackTraceElements = new Throwable().getStackTrace();
}

From source file:test.org.tradex.camel.TestCaseAppContextBuilder.java

/**
 * Creates a new application context using the file found at <code>ROOT_RESOURCE_DIR + clazz.getPackageName + fileName</code>
 * @param fileName The filename in the calculated directory
 * @param clazz The clazz we're launching the app context for
 * @return the app context/*from w w w .ja v  a2  s  .c o  m*/
 */
public static GenericXmlApplicationContext buildFor(String fileName, Class<?> clazz) {
    if (clazz == null)
        throw new IllegalArgumentException("Passed class was null", new Throwable());
    File springXml = new File(
            ROOT_RESOURCE_DIR + clazz.getPackage().getName().replace('.', '/') + "/" + fileName);
    if (!springXml.canRead()) {
        throw new RuntimeException("Failed to read Spring XML file at [" + springXml + "]", new Throwable());
    }
    return service(new GenericXmlApplicationContext(
            new FileSystemResource[] { new FileSystemResource(springXml.getAbsoluteFile()) }));
}

From source file:com.android.volley.VolleyLog.java

/**
 * Formats the caller's provided message and prepends useful info like
 * calling thread ID and method name.//from  ww w  .j  a v a  2s .  c om
 */
private static String buildMessage(String format, Object... args) {
    String msg = (args == null) ? format : String.format(Locale.US, format, args);
    StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();

    String caller = "<unknown>";
    // Walk up the stack looking for the first caller outside of VolleyLog.
    // It will be at least two frames up, so start there.
    for (int i = 2; i < trace.length; i++) {
        Class<?> clazz = trace[i].getClass();
        if (!clazz.equals(VolleyLog.class)) {
            String callingClass = trace[i].getClassName();
            callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
            callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);

            caller = callingClass + "." + trace[i].getMethodName();
            break;
        }
    }
    return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller, msg);
}

From source file:at.diamonddogs.data.dataobjects.WebRequest.java

public WebRequest() {
    origin = new Throwable();
    timeCritical = true;
}

From source file:ma.glasnost.orika.test.converter.CloneableConverterNoSetAccessibleTestCase.java

@Test
public void cloneableConverterWithoutSetAccessible() throws DatatypeConfigurationException {

    final SecurityManager initialSm = System.getSecurityManager();
    try {/*from   w w w.  j a  v a 2 s .  co  m*/
        System.setSecurityManager(new SecurityManager() {
            public void checkPermission(java.security.Permission perm) {
                if ("suppressAccessChecks".equals(perm.getName())) {
                    for (StackTraceElement ste : new Throwable().getStackTrace()) {
                        if (ste.getClassName().equals(CloneableConverter.class.getCanonicalName())) {
                            throw new SecurityException("not permitted");
                        }
                    }
                }
            }
        });

        CloneableConverter cc = new CloneableConverter(SampleCloneable.class);

        MapperFactory factory = MappingUtil.getMapperFactory();
        factory.getConverterFactory().registerConverter(cc);

        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.YEAR, 10);
        XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar((GregorianCalendar) cal);
        cal.add(Calendar.MONTH, 3);

        ClonableHolder source = new ClonableHolder();
        source.value = new SampleCloneable();
        source.value.id = 5L;
        source.date = new Date(System.currentTimeMillis() + 100000);
        source.timestamp = new Timestamp(System.currentTimeMillis() + 50000);
        source.calendar = cal;
        source.xmlCalendar = xmlCal;

        ClonableHolder dest = factory.getMapperFacade().map(source, ClonableHolder.class);
        Assert.assertEquals(source.value, dest.value);
        Assert.assertNotSame(source.value, dest.value);
        Assert.assertEquals(source.date, dest.date);
        Assert.assertNotSame(source.date, dest.date);
        Assert.assertEquals(source.timestamp, dest.timestamp);
        Assert.assertNotSame(source.timestamp, dest.timestamp);
        Assert.assertEquals(source.calendar, dest.calendar);
        Assert.assertNotSame(source.calendar, dest.calendar);
        Assert.assertEquals(source.xmlCalendar, dest.xmlCalendar);
        Assert.assertNotSame(source.xmlCalendar, dest.xmlCalendar);
    } finally {
        System.setSecurityManager(initialSm);
    }
}

From source file:org.b3log.latke.util.Callstacks.java

/**
 * Prints call stack with the specified logging level.
 * /*from w  w w  .j  a v  a2 s  .co  m*/
 * @param logLevel the specified logging level
 * @param carePackages the specified packages to print, for example, ["org.b3log.latke", "org.b3log.solo"], {@code null} to care 
 * nothing
 * @param exceptablePackages the specified packages to skip, for example, ["com.sun", "java.io", "org.b3log.solo.filter"], 
 * {@code null} to skip nothing
 */
public static void printCallstack(final Level logLevel, final String[] carePackages,
        final String[] exceptablePackages) {
    if (null == logLevel) {
        LOGGER.log(Level.WARNING, "Requires parameter[logLevel]");

        return;
    }

    final Throwable throwable = new Throwable();
    final StackTraceElement[] stackElements = throwable.getStackTrace();

    if (null == stackElements) {
        LOGGER.log(Level.WARNING, "Empty call stack");

        return;
    }

    final StringBuilder stackBuilder = new StringBuilder("CallStack [").append(Strings.LINE_SEPARATOR);

    for (int i = 1; i < stackElements.length; i++) {
        final String stackElemClassName = stackElements[i].getClassName();

        if (!StringUtils.startsWithAny(stackElemClassName, carePackages)
                || StringUtils.startsWithAny(stackElemClassName, exceptablePackages)) {
            continue;
        }

        stackBuilder.append("    [className=").append(stackElements[i].getClassName()).append(", fileName=")
                .append(stackElements[i].getFileName()).append(", lineNumber=")
                .append(stackElements[i].getLineNumber()).append(", methodName=")
                .append(stackElements[i].getMethodName()).append(']').append(Strings.LINE_SEPARATOR);
    }
    stackBuilder.append("], fullDepth=[").append(stackElements.length).append("]");

    LOGGER.log(logLevel, stackBuilder.toString());
}