Example usage for java.lang StackTraceElement getClassName

List of usage examples for java.lang StackTraceElement getClassName

Introduction

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

Prototype

public String getClassName() 

Source Link

Document

Returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Usage

From source file:com.asakusafw.testdriver.TestDriverTestToolsBase.java

private static Method findCaller() {
    StackTraceElement[] trace = new Throwable().getStackTrace();
    for (StackTraceElement element : trace) {
        try {//from   www .  ja v  a 2  s . co m
            Class<?> aClass = Class.forName(element.getClassName());
            if (TestDriverTestToolsBase.class.isAssignableFrom(aClass)) {
                continue;
            }
            Method method = aClass.getDeclaredMethod(element.getMethodName());
            return method;
        } catch (Exception e) {
            continue;
        }
    }
    throw new IllegalStateException(
            "?????????????");
}

From source file:com.jaeksoft.searchlib.Logging.java

public final static void warn(String msg, StackTraceElement[] stackTrace) {
    logger.warn(msg);//  w  w  w .  ja  v a 2 s  .co m
    for (StackTraceElement element : stackTrace)
        if (element.getClassName().startsWith("com.jaeksoft"))
            logger.warn(element.toString());
}

From source file:jp.techie.achicoco.framework.util.LogUtil.java

/**
 * ??//from   w  w w . ja v  a 2 s . c  o  m
 * 
 * @param className ??
 * @return 
 */
protected static int getLineNumber(String className) {
    try {
        throw new Exception();
    } catch (Exception e) {
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        for (int i = 0; i < stackTraceElements.length; i++) {
            StackTraceElement element = stackTraceElements[i];
            if (element != null && className.equals(element.getClassName())) {
                return element.getLineNumber();
            }
        }
    }
    return 0;
}

From source file:fr.opensagres.xdocreport.core.logging.LogUtils.java

private static void doLog(Logger log, Level level, String msg, Throwable t) {
    LogRecord record = new LogRecord(level, msg);

    record.setLoggerName(log.getName());
    record.setResourceBundleName(log.getResourceBundleName());
    record.setResourceBundle(log.getResourceBundle());

    if (t != null) {
        record.setThrown(t);/*from  w  w w .ja v  a2s  .  c om*/
    }

    // try to get the right class name/method name - just trace
    // back the stack till we get out of this class
    StackTraceElement stack[] = (new Throwable()).getStackTrace();
    String cname = LogUtils.class.getName();
    for (int x = 0; x < stack.length; x++) {
        StackTraceElement frame = stack[x];
        if (!frame.getClassName().equals(cname)) {
            record.setSourceClassName(frame.getClassName());
            record.setSourceMethodName(frame.getMethodName());
            break;
        }
    }
    log.log(record);
}

From source file:de.micromata.genome.util.runtime.AssertUtils.java

/**
 * Gets the stack above.//from   w w w .  j  ava  2s .c  om
 *
 * @param above the above
 * @return the stack above
 */
public static StackTraceElement getStackAbove(final Class<?> above) {
    final StackTraceElement[] stel = Thread.currentThread().getStackTrace();
    boolean foundFirst = false;
    final String clsName = above.getCanonicalName();
    for (int i = 0; i < stel.length; ++i) {
        final StackTraceElement s = stel[i];
        final String scn = s.getClassName();
        if (clsName.equals(scn) == false) {
            if (foundFirst == true) {
                return s;
            }
        } else {
            foundFirst = true;
        }
    }
    if (above.getSuperclass() != null) {
        return getStackAbove(above.getSuperclass());
    }
    return null;
}

From source file:org.openstreetmap.josm.plugins.PluginHandlerTestIT.java

private static String findFaultyPlugin(Collection<PluginInformation> plugins, Throwable root) {
    for (PluginInformation p : plugins) {
        try {//from ww  w .  ja va  2s  .co  m
            ClassLoader cl = PluginHandler.getPluginClassLoader(p.getName());
            String pluginPackage = cl.loadClass(p.className).getPackage().getName();
            for (StackTraceElement e : root.getStackTrace()) {
                try {
                    String stackPackage = cl.loadClass(e.getClassName()).getPackage().getName();
                    if (stackPackage.startsWith(pluginPackage)) {
                        return p.name;
                    }
                } catch (ClassNotFoundException ex) {
                    System.err.println(ex.getMessage());
                    continue;
                }
            }
        } catch (ClassNotFoundException ex) {
            System.err.println(ex.getMessage());
            continue;
        }
    }
    return "<unknown>";
}

From source file:org.apache.tajo.exception.ExceptionUtil.java

/**
 * Return the string about the exception point; e.g.,)
 * <code>org.apache.tajo.storage.mysql.JdbcTablespace::createTable</code>
 *
 * @return A string representing the class and method names at which the exception occurs.
 *//*from  ww  w. j av a 2s.co m*/
public static String getExceptionPoint() {
    StackTraceElement stack = Thread.currentThread().getStackTrace()[3];
    return stack.getClassName() + "::" + stack.getMethodName();
}

From source file:tachyon.util.io.PathUtils.java

/**
 * Creates a unique path based off the caller.
 *
 * @return unique path based off the caller
 *//*  ww w .  j av a  2  s.  c  o m*/
public static final String uniqPath() {
    StackTraceElement caller = new Throwable().getStackTrace()[1];
    long time = System.nanoTime();
    return "/" + caller.getClassName() + "/" + caller.getMethodName() + "/" + time;
}

From source file:org.openmrs.module.hl7query.util.ExceptionUtil.java

/**
 * Wraps the exception message as a SimpleObject to be sent to client
 * //from   w w w . ja  v  a 2  s.  co m
 * @param ex
 * @param reason
 * @return
 */
public static AuthenticationErrorObject wrapErrorResponse(Exception ex, String reason) {
    LinkedHashMap map = new LinkedHashMap();
    if (reason != null && !reason.isEmpty()) {
        map.put("message", reason);
    } else
        map.put("message", ex.getMessage());
    StackTraceElement ste = ex.getStackTrace()[0];
    map.put("code", ste.getClassName() + ":" + ste.getLineNumber());
    map.put("detail", ExceptionUtils.getStackTrace(ex));
    return new AuthenticationErrorObject().add("error", map);
}

From source file:org.kei.android.phone.cellhistory.CellHistoryApp.java

@SuppressWarnings("unchecked")
public static void addLog(final Context c, final Object msg) {
    final CellHistoryApp ctx = CellHistoryApp.getApp(c);
    ctx.lock();/*www  .  j ava 2s.co  m*/
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    if (prefs.getBoolean(Preferences.PREFS_KEY_LOG_ENABLE, Preferences.PREFS_DEFAULT_LOG_ENABLE)) {
        String head = new SimpleDateFormat("yyyyMMdd [hhmmssa]: \n", Locale.US).format(new Date());
        try {
            throw new Exception();
        } catch (Exception e) {
            StackTraceElement ste = e.getStackTrace()[1];
            String name = ste.getClassName();
            int n = -1;
            if ((n = name.lastIndexOf('.')) != -1)
                name = name.substring(n + 1);
            head += name + "->" + ste.getMethodName() + "(" + ste.getLineNumber() + ")\n";
        }
        ctx.getLogBuffer().add(head + msg);
    }
    ctx.unlock();
}