Example usage for java.lang StackTraceElement getMethodName

List of usage examples for java.lang StackTraceElement getMethodName

Introduction

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

Prototype

public String getMethodName() 

Source Link

Document

Returns the name of the method containing the execution point represented by this stack trace element.

Usage

From source file:org.kuali.coeus.sys.impl.validation.ErrorReporterImpl.java

public static String getMethodPath(int fromLevel, int toLevel) {
    StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    //increase the levels to avoid including the method that called this.
    fromLevel = fromLevel + 1;/* w  w w. j a v a2 s . c om*/
    toLevel = toLevel + 1;

    if (fromLevel <= 0) {
        throw new IllegalArgumentException("invalid fromLevel (" + fromLevel + " < 0)");
    }
    if (fromLevel > toLevel) {
        throw new IllegalArgumentException(
                "invalid levels (fromLevel " + fromLevel + " > toLevel " + toLevel + ")");
    }
    if (toLevel >= stackTraceElements.length) {
        throw new IllegalArgumentException(
                "invalid toLevel (" + toLevel + " >= " + stackTraceElements.length + ")");
    }

    StringBuffer result = new StringBuffer();
    int elementIndex = 0;
    for (StackTraceElement element : stackTraceElements) {
        if (elementIndex >= fromLevel && elementIndex >= toLevel) {
            if (result.length() > 0) {
                result.append(" from ");
            }
            result.append(element.getClassName()).append(".");
            result.append(element.getMethodName()).append("(");
            result.append(element.getFileName()).append(":");
            result.append(element.getLineNumber()).append(")");
        }
        elementIndex++;
    }
    return result.toString();

}

From source file:com.ichi2.anki.AnkiDroidApp.java

private static String getExceptionHash(Throwable th) {
    final StringBuilder res = new StringBuilder();
    Throwable cause = th;// w w  w  . j  a  v a2s. com
    while (cause != null) {
        final StackTraceElement[] stackTraceElements = cause.getStackTrace();
        for (final StackTraceElement e : stackTraceElements) {
            res.append(e.getClassName());
            res.append(e.getMethodName());
        }
        cause = cause.getCause();
    }
    return Integer.toHexString(res.toString().hashCode());
}

From source file:mSearch.tool.Log.java

private static void fehlermeldung_(int fehlerNummer, Exception ex, String[] texte) {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[2];
    final String klasse = methodCaller.getClassName() + "." + methodCaller.getMethodName();
    String kl;/*from  w w w.  ja  va  2s  . co  m*/
    try {
        kl = klasse;
        while (kl.contains(".")) {
            if (Character.isUpperCase(kl.charAt(0))) {
                break;
            } else {
                kl = kl.substring(kl.indexOf(".") + 1);
            }
        }
    } catch (Exception ignored) {
        kl = klasse;
    }
    addFehlerNummer(fehlerNummer, kl, ex != null);
    if (ex != null || Config.debug) {
        // Exceptions immer ausgeben
        resetProgress();
        String x, z;
        if (ex != null) {
            x = "!";
        } else {
            x = "=";
        }
        z = "*";
        logList.add(x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x
                + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x);

        try {
            // Stacktrace
            try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
                if (ex != null) {
                    ex.printStackTrace(pw);
                }
                pw.flush();
                sw.flush();
                logList.add(sw.toString());
            }
        } catch (Exception ignored) {
        }

        logList.add(z + " Fehlernr: " + fehlerNummer);
        if (ex != null) {
            logList.add(z + " Exception: " + ex.getMessage());
        }
        logList.add(z + " " + FEHLER + kl);
        for (String aTexte : texte) {
            logList.add(z + "           " + aTexte);
        }
        logList.add("");
        printLog();
    }
}

From source file:org.ubicompforall.cityexplorer.CityExplorer.java

/***
 * Debug method to include the filename, line-number and method of the caller
 *//*  w w w. j a v a2  s.  c  o m*/
public static void debug(int d, String msg) {

    if (DEBUG >= d) {
        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        int stackLevel = 2;
        while (stackLevel < st.length - 1 && (st[stackLevel].getMethodName().equals("debug")
                || st[stackLevel].getMethodName().matches("access\\$\\d+"))) {
            //|| st[stackLevel].getMethodName().matches("run")
            stackLevel++;
        }
        StackTraceElement e = st[stackLevel];
        if (d < 0) { //error
            Log.e(C, e.getMethodName() + ": " + msg + " at (" + e.getFileName() + ":" + e.getLineNumber()
                    + ")");
        } else { //debug
            Log.d(C, e.getMethodName() + ": " + msg + " at (" + e.getFileName() + ":" + e.getLineNumber()
                    + ")");
        } //if debug, else error
    } // if verbose enough

}

From source file:org.evosuite.regression.RegressionExceptionHelper.java

/**
 * Get signature based on the root cause from the stack trace
 * (uses: location of the error triggering line from CUT)
 * @param CUT the class to expect the exception to be thrown from
 * @return signature string/*from  w w w  .  ja v a 2  s.c  o  m*/
 */
public static String getExceptionSignature(Throwable throwable, String CUT) {
    String signature = throwable.getClass().getSimpleName();

    StackTraceElement[] stackTrace = throwable.getStackTrace();
    for (StackTraceElement el : stackTrace) {
        String elClass = el.getClassName();
        if (!Objects.equals(elClass, CUT)) {
            continue;
        }

        String method = el.getMethodName();
        int line = el.getLineNumber();
        signature += ":" + method + "-" + line;
        break;
    }
    return signature;
}

From source file:com.snicesoft.basekit.LogKit.java

private static String generateTag(StackTraceElement caller) {
    //        String tag = "%s.%s(L:%d)";
    String tag = "%s.%s(" + caller.getFileName() + ":%d)";
    String callerClazzName = caller.getClassName();
    callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
    tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
    tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
    return tag;/*ww w  . j  ava  2 s. c om*/
}

From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java

/**
 * @return <code>true</code> if given {@link Throwable} is caused by GWT Mail example.
 *//*from  ww w .  ja v a 2  s  . co  m*/
private static boolean isMailExampleException(Throwable e) {
    StackTraceElement[] stackTrace = e.getStackTrace();
    StackTraceElement element = stackTrace[0];
    return "com.google.gwt.sample.mail.client.MailList".equals(element.getClassName())
            && "selectRow".equals(element.getMethodName());
}

From source file:AIR.Common.DB.AbstractDLL.java

private static void logQuery(String query) {
    _logger.info("Query : " + query);
    if (_logger.isDebugEnabled()) {
        try {/*from  ww w.  java2  s  . c o m*/
            StringBuilder traceBackMessage = new StringBuilder("Query traceback:\r\n");
            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (int i = 2; i < 9 && i < trace.length; i++) {
                StackTraceElement t = trace[i];
                traceBackMessage.append(String.format("    %s.%s (%d)\r\n", t.getClassName(), t.getMethodName(),
                        t.getLineNumber()));
            }
            _logger.debug(traceBackMessage.toString());
        } catch (Throwable t) {
            // Ignore!!
        }
    }
}

From source file:org.eclipse.wb.internal.core.DesignerPlugin.java

/**
 * We should not allow user code to terminate JVM.
 *///from   ww w.j  a v a2 s.  co  m
public static void installSecurityManager() {
    System.setSecurityManager(new SecurityManager() {
        @Override
        public void checkPermission(java.security.Permission perm) {
            if (isExitVM(perm)) {
                StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
                for (StackTraceElement element : stackTrace) {
                    String className = element.getClassName();
                    String methodName = element.getMethodName();
                    // ignore this class, because it has our class name prefix
                    if (className.equals(getClass().getName())) {
                        continue;
                    }
                    // ignore JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    if (className.equals("javax.swing.JFrame")
                            && methodName.equals("setDefaultCloseOperation")) {
                        return;
                    }
                    // prevent exit() from user invoked by "designer"
                    if (className.startsWith("org.eclipse.wb.")
                            || className.startsWith("com.google.gdt.eclipse.designer.")
                            || className.startsWith("net.rim.ejde.designer.")
                            || className.startsWith("java.awt.EventQueue")) {
                        // we often use test_exit() method as point to stop tests, allow it
                        if (methodName.startsWith("test_") && methodName.endsWith("_exit")) {
                            return;
                        }
                        // prevent exit()
                        throw new SecurityException("Exit from within user-loaded code");
                    }
                }
            }
        }

        private boolean isExitVM(java.security.Permission perm) {
            return perm instanceof RuntimePermission && StringUtils.startsWith(perm.getName(), "exitVM");
        }
    });
}

From source file:com.code19.library.L.java

private static String[] wrapperContent(String tag, Object... objects) {
    if (TextUtils.isEmpty(tag)) {
        tag = TAG;//from ww  w  .  jav  a 2s.c o m
    }
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    StackTraceElement targetElement = stackTrace[5];
    String className = targetElement.getClassName();
    String[] classNameInfo = className.split("\\.");
    if (classNameInfo.length > 0) {
        className = classNameInfo[classNameInfo.length - 1] + ".java";
    }
    String methodName = targetElement.getMethodName();
    int lineNumber = targetElement.getLineNumber();
    if (lineNumber < 0) {
        lineNumber = 0;
    }
    String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
    String msg = (objects == null) ? "Log with null object" : getObjectsString(objects);
    String headString = "[(" + className + ":" + lineNumber + ")#" + methodNameShort + " ] ";
    return new String[] { tag, msg, headString };
}