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.momock.util.Logger.java

public static void debug(String msg) {
    if (!enabled || logLevel > LEVEL_DEBUG)
        return;//  w w w  .  ja va  2 s  .c  om
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.d(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("DEBUG", msg));
    logStream.flush();
}

From source file:Main.java

private static String getLineNumber() {
    if (!isShowLineNumber) {
        return "";
    }//from   w  w  w .j  a  v a  2 s.  c  o m
    StackTraceElement[] stackTrace = new Throwable().getStackTrace();
    final int index = 2;
    String className = stackTrace[index].getFileName();
    int lineNum = stackTrace[index].getLineNumber();
    return "(" + className + ":" + lineNum + ") ";
}

From source file:net.logstash.logback.ConfigurationTest.java

private void verifyOutput(LoggingEventCompositeJsonEncoder encoder) throws IOException {
    LOGGER.info(Markers.append("markerFieldName", "markerFieldValue"), "message {} {} {} {}",
            new Object[] { "arg", StructuredArguments.keyValue("k1", "v1"),
                    StructuredArguments.keyValue("k2", "v2", "{0}=[{1}]"),
                    StructuredArguments.value("k3", "v3"), new Throwable() });

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    encoder.init(outputStream);/* w ww  . j av a2  s. co  m*/
    encoder.doEncode(listAppender.list.get(0));

    Map<String, Object> output = parseJson(outputStream.toString("UTF-8"));
    Assert.assertNotNull(output.get("@timestamp"));
    Assert.assertEquals(1, output.get("@version"));
    Assert.assertEquals("message arg k1=v1 k2=[v2] v3", output.get("customMessage"));
    Assert.assertEquals("message {} {} {} {}", output.get("customRawMessage"));
    Assert.assertEquals("n.l.l.ConfigurationTest", output.get("logger_name"));
    Assert.assertNotNull(output.get("thread_name"));
    Assert.assertEquals("INFO", output.get("level"));
    Assert.assertEquals(20000, output.get("level_value"));
    Assert.assertNotNull(output.get("caller"));
    Assert.assertTrue(
            ((String) output.get("stack_trace")).contains("n.l.logback.ConfigurationTest.verifyOutput"));
    Assert.assertEquals("customValue", output.get("customName"));
    Assert.assertEquals("patternValue", output.get("patternName"));
    Assert.assertEquals("markerFieldValue", output.get("markerFieldName"));
    Assert.assertTrue(output.get("relativeTime") instanceof Number);
    Assert.assertEquals("arg", output.get("prefix0"));
    Assert.assertEquals("v1", output.get("k1"));
    Assert.assertEquals("v2", output.get("k2"));
    Assert.assertEquals("v3", output.get("k3"));
}

From source file:org.ebayopensource.turmeric.eclipse.core.logging.SOALogger.java

/**
 * Info./*w  w w . ja  va  2  s .com*/
 *
 * @param msgs The messages
 * @see java.util.logging.Level#INFO
 */
public void info(Object... msgs) {
    StackTraceElement[] elements = new Throwable().getStackTrace();
    logp(Level.INFO, elements[1].getClassName(), elements[1].getMethodName(), StringUtil.toString(msgs));
}

From source file:com.taobao.itest.util.XlsUtil.java

private static String getTestClassPackage() {
    int atLeastCallCount = 5;
    StackTraceElement stack[] = (new Throwable()).getStackTrace();

    /**//w w  w  .  j  a va2  s  .  co  m
     * Back call stack will know the caller 0getTestClassName
     * 1getExcelRealPath 2: readData 3: readForObject 4: test class
     * 
     */
    if (stack.length < atLeastCallCount) {
        // Not less than 5 times the call, or take less than a call to the
        // test class
        return null;
    } else {
        StackTraceElement ste = stack[atLeastCallCount - 1];
        return ste.getClassName().substring(0, ste.getClassName().lastIndexOf('.'));
    }
}

From source file:org.apache.jk.common.JniHandler.java

/** send and get the response in the same buffer. This calls the
* method on the wrapped jk_bean object./*ww  w .j  av a2 s .  c  o  m*/
 */
protected int nativeDispatch(Msg msg, MsgContext ep, int code, int raw) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Sending packet " + code + " " + raw);

    if (raw == 0) {
        msg.end();

        if (log.isTraceEnabled())
            msg.dump("OUT:");
    }

    // Create ( or reuse ) the jk_endpoint ( the native pair of
    // MsgContext )
    long xEnv = ep.getJniEnv();
    long nativeContext = ep.getJniContext();
    if (nativeContext == 0 || xEnv == 0) {
        setNativeEndpoint(ep);
        xEnv = ep.getJniEnv();
        nativeContext = ep.getJniContext();
    }

    if (xEnv == 0 || nativeContext == 0 || nativeJkHandlerP == 0) {
        log.error("invokeNative: Null pointer ");
        return -1;
    }

    // Will process the message in the current thread.
    // No wait needed to receive the response, if any
    int status = apr.jkInvoke(xEnv, nativeJkHandlerP, nativeContext, code, msg.getBuffer(), 0, msg.getLen(),
            raw);

    if (status != 0 && status != 2) {
        log.error("nativeDispatch: error " + status, new Throwable());
    }

    if (log.isDebugEnabled())
        log.debug("Sending packet - done " + status);
    return status;
}

From source file:com.momock.util.Logger.java

public static void info(String msg) {
    if (!enabled || logLevel > LEVEL_INFO)
        return;//from ww  w. j a v  a 2 s.c o m
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.i(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("INFO", msg));
    logStream.flush();
}

From source file:com.sun.grizzly.http.jk.common.ChannelUn.java

public int receive(Msg msg, MsgContext ep) throws IOException {
    int rc = super.nativeDispatch(msg, ep, CH_READ, 1);

    if (rc != 0) {
        LoggerUtils.getLogger().log(Level.SEVERE, "receive error:   " + rc, new Throwable());
        return -1;
    }/* w  w  w.  jav  a 2s .co  m*/

    msg.processHeader();

    if (LoggerUtils.getLogger().isLoggable(Level.FINEST)) {
        LoggerUtils.getLogger().log(Level.FINEST, "receive:  total read = " + msg.getLen());
    }

    return msg.getLen();
}

From source file:ca.sqlpower.sqlobject.BaseSQLObjectTestCase.java

/**
 * XXX This test should use the {@link GenericNewValueMaker} as it has it's own mini
 * version inside it. This test should also be using the annotations to decide which 
 * setters can fire events./*from   w  w  w.j av a 2  s  .c o  m*/
 * 
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws SQLObjectException
 */
public void testAllSettersGenerateEvents() throws IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, SQLObjectException {
    SQLObject so = getSQLObjectUnderTest();
    so.populate();

    propertiesToIgnoreForEventGeneration.addAll(getPropertiesToIgnoreForEvents());

    //Ignored because we expect the object to be populated.
    propertiesToIgnoreForEventGeneration.add("exportedKeysPopulated");
    propertiesToIgnoreForEventGeneration.add("importedKeysPopulated");
    propertiesToIgnoreForEventGeneration.add("columnsPopulated");
    propertiesToIgnoreForEventGeneration.add("indicesPopulated");

    CountingSQLObjectListener listener = new CountingSQLObjectListener();
    SQLPowerUtils.listenToHierarchy(so, listener);

    if (so instanceof SQLDatabase) {
        // should be handled in the Datasource
        propertiesToIgnoreForEventGeneration.add("name");
    }

    List<PropertyDescriptor> settableProperties;

    settableProperties = Arrays.asList(PropertyUtils.getPropertyDescriptors(so.getClass()));

    for (PropertyDescriptor property : settableProperties) {
        Object oldVal;
        if (propertiesToIgnoreForEventGeneration.contains(property.getName()))
            continue;

        try {
            oldVal = PropertyUtils.getSimpleProperty(so, property.getName());
            // check for a setter
            if (property.getWriteMethod() == null) {
                continue;
            }

        } catch (NoSuchMethodException e) {
            System.out.println(
                    "Skipping non-settable property " + property.getName() + " on " + so.getClass().getName());
            continue;
        }
        Object newVal; // don't init here so compiler can warn if the following code doesn't always give it a value
        if (property.getPropertyType() == Integer.TYPE || property.getPropertyType() == Integer.class) {
            if (oldVal != null) {
                newVal = ((Integer) oldVal) + 1;
            } else {
                newVal = 1;
            }
        } else if (property.getPropertyType() == String.class) {
            // make sure it's unique
            newVal = "new " + oldVal;

        } else if (property.getPropertyType() == Boolean.TYPE || property.getPropertyType() == Boolean.class) {
            if (oldVal == null) {
                newVal = Boolean.TRUE;
            } else {
                newVal = new Boolean(!((Boolean) oldVal).booleanValue());
            }
        } else if (property.getPropertyType() == SQLCatalog.class) {
            newVal = new SQLCatalog(new SQLDatabase(), "This is a new catalog");
        } else if (property.getPropertyType() == SPDataSource.class) {
            newVal = new JDBCDataSource(getPLIni());
            ((SPDataSource) newVal).setName("test");
            ((SPDataSource) newVal).setDisplayName("test");
            ((JDBCDataSource) newVal).setUser("a");
            ((JDBCDataSource) newVal).setPass("b");
            ((JDBCDataSource) newVal).getParentType().setJdbcDriver(MockJDBCDriver.class.getName());
            ((JDBCDataSource) newVal).setUrl("jdbc:mock:tables=tab1");
        } else if (property.getPropertyType() == JDBCDataSource.class) {
            newVal = new JDBCDataSource(getPLIni());
            ((SPDataSource) newVal).setName("test");
            ((SPDataSource) newVal).setDisplayName("test");
            ((JDBCDataSource) newVal).setUser("a");
            ((JDBCDataSource) newVal).setPass("b");
            ((JDBCDataSource) newVal).getParentType().setJdbcDriver(MockJDBCDriver.class.getName());
            ((JDBCDataSource) newVal).setUrl("jdbc:mock:tables=tab1");
        } else if (property.getPropertyType() == SQLTable.class) {
            newVal = new SQLTable();
        } else if (property.getPropertyType() == SQLColumn.class) {
            newVal = new SQLColumn();
        } else if (property.getPropertyType() == SQLIndex.class) {
            newVal = new SQLIndex();
        } else if (property.getPropertyType() == SQLRelationship.SQLImportedKey.class) {
            SQLRelationship rel = new SQLRelationship();
            newVal = rel.getForeignKey();
        } else if (property.getPropertyType() == SQLRelationship.Deferrability.class) {
            if (oldVal == SQLRelationship.Deferrability.INITIALLY_DEFERRED) {
                newVal = SQLRelationship.Deferrability.NOT_DEFERRABLE;
            } else {
                newVal = SQLRelationship.Deferrability.INITIALLY_DEFERRED;
            }
        } else if (property.getPropertyType() == SQLRelationship.UpdateDeleteRule.class) {
            if (oldVal == SQLRelationship.UpdateDeleteRule.CASCADE) {
                newVal = SQLRelationship.UpdateDeleteRule.RESTRICT;
            } else {
                newVal = SQLRelationship.UpdateDeleteRule.CASCADE;
            }
        } else if (property.getPropertyType() == SQLIndex.AscendDescend.class) {
            if (oldVal == SQLIndex.AscendDescend.ASCENDING) {
                newVal = SQLIndex.AscendDescend.DESCENDING;
            } else {
                newVal = SQLIndex.AscendDescend.ASCENDING;
            }
        } else if (property.getPropertyType() == Throwable.class) {
            newVal = new Throwable();
        } else if (property.getPropertyType() == BasicSQLType.class) {
            if (oldVal != BasicSQLType.OTHER) {
                newVal = BasicSQLType.OTHER;
            } else {
                newVal = BasicSQLType.TEXT;
            }
        } else if (property.getPropertyType() == UserDefinedSQLType.class) {
            newVal = new UserDefinedSQLType();
        } else if (property.getPropertyType() == SQLTypeConstraint.class) {
            if (oldVal != SQLTypeConstraint.NONE) {
                newVal = SQLTypeConstraint.NONE;
            } else {
                newVal = SQLTypeConstraint.CHECK;
            }
        } else if (property.getPropertyType() == String[].class) {
            newVal = new String[3];
        } else if (property.getPropertyType() == PropertyType.class) {
            if (oldVal != PropertyType.NOT_APPLICABLE) {
                newVal = PropertyType.NOT_APPLICABLE;
            } else {
                newVal = PropertyType.VARIABLE;
            }
        } else if (property.getPropertyType() == List.class) {
            newVal = Arrays.asList("one", "two");
        } else {
            throw new RuntimeException("This test case lacks a value for " + property.getName() + " (type "
                    + property.getPropertyType().getName() + ") from " + so.getClass() + " on property "
                    + property.getDisplayName());
        }

        int oldChangeCount = listener.getChangedCount();

        try {
            System.out.println("Setting property '" + property.getName() + "' to '" + newVal + "' ("
                    + newVal.getClass().getName() + ")");
            BeanUtils.copyProperty(so, property.getName(), newVal);

            // some setters fire multiple events (they change more than one property)
            assertTrue(
                    "Event for set " + property.getName() + " on " + so.getClass().getName() + " didn't fire!",
                    listener.getChangedCount() > oldChangeCount);
            if (listener.getChangedCount() == oldChangeCount + 1) {
                assertEquals("Property name mismatch for " + property.getName() + " in " + so.getClass(),
                        property.getName(), ((PropertyChangeEvent) listener.getLastEvent()).getPropertyName());
                assertEquals("New value for " + property.getName() + " was wrong", newVal,
                        ((PropertyChangeEvent) listener.getLastEvent()).getNewValue());
            }
        } catch (InvocationTargetException e) {
            System.out.println("(non-fatal) Failed to write property '" + property.getName() + " to type "
                    + so.getClass().getName());
        }
    }
}

From source file:com.momock.util.Logger.java

public static void warn(String msg) {
    if (!enabled || logLevel > LEVEL_WARN)
        return;//from  www  .java 2s.  c  o  m
    if (msg == null)
        msg = "";
    Throwable t = new Throwable();
    StackTraceElement trace = t.getStackTrace()[1];
    android.util.Log.w(logName, msg + " @ " + getSourceInfo(trace));
    checkLogFile();
    logStream.println(getLog("WARN", msg));
    logStream.flush();
}