Example usage for java.lang StackTraceElement getLineNumber

List of usage examples for java.lang StackTraceElement getLineNumber

Introduction

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

Prototype

public int getLineNumber() 

Source Link

Document

Returns the line number of the source line containing the execution point represented by this stack trace element.

Usage

From source file:sf.net.experimaestro.utils.JSUtils.java

/**
 * @see org.mozilla.javascript.RhinoException#getScriptStack()
 * @param ex/*from   w w w.j av a  2  s . c o m*/
 * @return A stack
 */
static public ScriptStackElement[] getScriptStackTrace(Throwable ex) {
    List<ScriptStackElement> list = new ArrayList<>();
    ScriptStackElement[][] interpreterStack = null;
    int interpreterStackIndex = 0;
    StackTraceElement[] stack = ex.getStackTrace();
    // Pattern to recover function name from java method name -
    // see Codegen.getBodyMethodName()
    // kudos to Marc Guillemot for coming up with this
    Pattern pattern = Pattern.compile("_c_(.*)_\\d+");
    for (StackTraceElement e : stack) {
        String fileName = e.getFileName();
        if (e.getMethodName().startsWith("_c_") && e.getLineNumber() > -1 && fileName != null
                && !fileName.endsWith(".java")) {
            String methodName = e.getMethodName();
            Matcher match = pattern.matcher(methodName);
            // the method representing the main script is always "_c_script_0" -
            // at least we hope so
            methodName = !"_c_script_0".equals(methodName) && match.find() ? match.group(1) : null;
            list.add(new ScriptStackElement(fileName, methodName, e.getLineNumber()));
        } else if ("org.mozilla.javascript.Interpreter".equals(e.getClassName())
                && "interpretLoop".equals(e.getMethodName()) && interpreterStack != null
                && interpreterStack.length > interpreterStackIndex) {
            for (ScriptStackElement elem : interpreterStack[interpreterStackIndex++]) {
                list.add(elem);
            }
        }
    }
    return list.toArray(new ScriptStackElement[list.size()]);

}

From source file:org.eclipse.epp.internal.logging.aeri.ui.model.Reports.java

public static String traceIdentityHash(ErrorReport report) {
    final Hasher hasher = Hashing.murmur3_128().newHasher();
    visit(report, new ModelSwitch<Hasher>() {

        @Override/*from w w  w.  j av a  2 s .c om*/
        public Hasher caseStackTraceElement(StackTraceElement element) {
            hasher.putString(element.getClassName(), UTF_8);
            hasher.putString(element.getMethodName(), UTF_8);
            hasher.putInt(element.getLineNumber());
            return null;
        }
    });
    String hash = hasher.hash().toString();
    return hash;
}

From source file:free.yhc.netmbuddy.utils.Utils.java

private static void log(Class<?> cls, LogLV lv, String msg) {
    if (null == msg)
        return;//  w  w w. j av a2  s . co m

    StackTraceElement ste = Thread.currentThread().getStackTrace()[5];
    msg = ste.getClassName() + "/" + ste.getMethodName() + "(" + ste.getLineNumber() + ") : " + msg;

    if (!LOGF) {
        switch (lv) {
        case V:
            Log.v(cls.getSimpleName(), msg);
            break;
        case D:
            Log.d(cls.getSimpleName(), msg);
            break;
        case I:
            Log.i(cls.getSimpleName(), msg);
            break;
        case W:
            Log.w(cls.getSimpleName(), msg);
            break;
        case E:
            Log.e(cls.getSimpleName(), msg);
            break;
        case F:
            Log.wtf(cls.getSimpleName(), msg);
            break;
        }
    } else {
        long timems = System.currentTimeMillis();
        sLogWriter.printf("<%s:%03d> [%s] %s\n", sLogTimeDateFormat.format(new Date(timems)), timems % 1000,
                lv.name(), msg);
        sLogWriter.flush();
    }
}

From source file:com.simiacryptus.util.Util.java

/**
 * Report.// w  w  w  . ja v  a 2 s.c  o m
 *
 * @param fragments the fragments
 * @throws IOException the io exception
 */
public static void report(@javax.annotation.Nonnull final Stream<String> fragments) throws IOException {
    @javax.annotation.Nonnull
    final File outDir = new File("reports");
    outDir.mkdirs();
    final StackTraceElement caller = com.simiacryptus.util.Util
            .getLast(Arrays.stream(Thread.currentThread().getStackTrace())//
                    .filter(x -> x.getClassName().contains("simiacryptus")));
    @javax.annotation.Nonnull
    final File report = new File(outDir, caller.getClassName() + "_" + caller.getLineNumber() + ".html");
    @javax.annotation.Nonnull
    final PrintStream out = new PrintStream(new FileOutputStream(report));
    out.println("<html><head></head><body>");
    fragments.forEach(out::println);
    out.println("</body></html>");
    out.close();
    Desktop.getDesktop().browse(report.toURI());
}

From source file:Debug.java

public static String getStackTrace(Throwable e, int limit, int skip) {
    StringBuffer result = new StringBuffer();

    if (e != null) {
        StackTraceElement stes[] = e.getStackTrace();
        if (stes != null) {
            for (int i = skip; i < stes.length && (limit < 0 || i < limit); i++) {
                StackTraceElement ste = stes[i];

                result.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName()
                        + ":" + ste.getLineNumber() + ")" + newline);
            }/*from www  .j  av a 2 s.com*/
            if (limit >= 0 && stes.length > limit)
                result.append("\t..." + newline);
        }

        //            e.printStackTrace(System.out);
        result.append(newline);
    }

    return result.toString();
}

From source file:org.lilyproject.avro.AvroConverter.java

private static AvroStackTraceElement convert(StackTraceElement el) {
    AvroStackTraceElement result = new AvroStackTraceElement();
    result.setClassName(el.getClassName());
    result.setMethodName(el.getMethodName());
    result.setFileName(el.getFileName());
    result.setLineNumber(el.getLineNumber());
    return result;
}

From source file:org.openmrs.module.webservices.rest.web.RestUtil.java

/**
 * Wraps the exception message as a SimpleObject to be sent to client
 * /*from   www.ja v  a2s.c  om*/
 * @param ex
 * @param reason
 * @return
 */
public static SimpleObject 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 SimpleObject().add("error", map);
}

From source file:com.kth.common.utils.etc.LogUtil.java

/** ? , ?? ?  ??. */
private static String getClassLineNumber(final Class<?> clazz) {
    StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    if (elements != null) {
        for (StackTraceElement e : elements) {
            if ((clazz.getName()).equals(e.getClassName())
                    || (clazz.getSimpleName()).equals(e.getClassName())) {
                return e.getClassName() + "(" + e.getMethodName() + ":" + e.getLineNumber() + ")";
            }/*from  ww w.j  av a2  s.co  m*/
        }
    }
    return "";
}

From source file:com.codepunk.codepunklib.util.log.FormattingLogger.java

/**
 * Returns the concrete information from the given {@link StackTraceElement} that is represented
 * by the given Placeholder./*from   w ww  .  j a  v  a2s. c om*/
 * @param element The {@link StackTraceElement}.
 * @param placeholder The {@link Placeholder} to replace.
 * @return The information represented by the Placeholder.
 */
private static Object getFromElement(StackTraceElement element, Placeholder placeholder) {
    try {
        switch (placeholder) {
        case FILE_NAME:
            return element.getFileName();
        case HASH_CODE:
            return element.hashCode();
        case LINE_NUMBER:
            return element.getLineNumber();
        case METHOD_NAME:
            return element.getMethodName();
        case PACKAGE: {
            return getClass(element).getPackage().getName();
        }
        case CLASS_NAME:
        case SIMPLE_CLASS_NAME:
        default: {
            String name;
            Class cls = getClass(element);
            do {
                if (cls == null) {
                    name = UNKNOWN_CLASS_NAME;
                } else if (placeholder == Placeholder.CLASS_NAME) {
                    name = cls.getName();
                } else {
                    name = cls.getSimpleName();
                }
            } while (TextUtils.isEmpty(name));
            return name;
        }
        }
    } catch (ClassNotFoundException e) {
        return UNKNOWN_CLASS_NAME;
    }
}

From source file:com.egt.core.aplicacion.Bitacora.java

private static String getStackTraceElementTrack(StackTraceElement trace) {
    return trace == null ? ""
            : " @ " + getSimpleClassName(trace.getClassName()) + "." + trace.getMethodName() + ":"
                    + trace.getLineNumber();
}