Example usage for java.lang StackTraceElement isNativeMethod

List of usage examples for java.lang StackTraceElement isNativeMethod

Introduction

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

Prototype

public boolean isNativeMethod() 

Source Link

Document

Returns true if the method containing the execution point represented by this stack trace element is a native method.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.out.println("Is Native method ? :");

    Thread t = Thread.currentThread();
    StackTraceElement e = t.getStackTrace()[0];

    System.out.println(e.isNativeMethod());

}

From source file:Main.java

private static String getFunctionName() {
    StackTraceElement[] sts = Thread.currentThread().getStackTrace();
    if (sts == null) {
        return null;
    }//from   ww w.  j  a v a  2s . c o  m
    for (StackTraceElement st : sts) {
        if (st.isNativeMethod()) {
            continue;
        }
        if (st.getClassName().equals(Thread.class.getName())) {
            continue;
        }
        if (st.getFileName().equals("LogUtils.java")) {
            continue;
        }
        return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + "): "
                + st.getFileName() + ":" + st.getLineNumber() + "]";
    }
    return null;
}

From source file:Main.java

public static String getLocation(int back) {
    try {//  w w  w .  j  av  a 2s .  co m
        // Use Throwable to obtain stack trace, using Thread.currentThread().getStackTrace is expensive because of thread-safety */
        StackTraceElement e = (new Throwable()).getStackTrace()[back];
        if (e.isNativeMethod()) {
            return " [<native>]";
        }
        return " [" + e.getClassName().split("\\$")[0].replace('.', '/') + ".java:" + e.getLineNumber() + "]";
    } catch (ArrayIndexOutOfBoundsException e) {
    } catch (SecurityException e) {
    }
    return " [<unknown>]";
}

From source file:Main.java

private static String formatStackTraceElement(StackTraceElement ste) {
    return compressClassName(ste.getClassName()) + "." + ste.getMethodName() + (ste.isNativeMethod()
            ? "(Native Method)"
            : (ste.getFileName() != null && ste.getLineNumber() >= 0
                    ? "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")"
                    : (ste.getFileName() != null ? "(" + ste.getFileName() + ")" : "(Unknown Source)")));
}

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

public static Throwable newThrowable(java.lang.Throwable throwable) {
    Throwable mThrowable = factory.createThrowable();
    mThrowable.setMessage(throwable.getMessage());
    mThrowable.setClassName(throwable.getClass().getName());
    List<StackTraceElement> mStackTrace = mThrowable.getStackTrace();
    for (java.lang.StackTraceElement stackTraceElement : throwable.getStackTrace()) {
        StackTraceElement mStackTraceElement = factory.createStackTraceElement();
        mStackTraceElement.setFileName(stackTraceElement.getFileName());
        mStackTraceElement.setClassName(ensureNotBlank(stackTraceElement.getClassName(), throwable));
        mStackTraceElement.setMethodName(ensureNotBlank(stackTraceElement.getMethodName(), throwable));
        mStackTraceElement.setLineNumber(stackTraceElement.getLineNumber());
        mStackTraceElement.setNative(stackTraceElement.isNativeMethod());
        mStackTrace.add(mStackTraceElement);
    }//from   w  w  w  .  java2s . c  om
    java.lang.Throwable cause = throwable.getCause();
    if (cause != null) {
        if (cause == throwable) {
            log(WARN_CYCLIC_EXCEPTION, cause.toString());
            return mThrowable;
        }
        mThrowable.setCause(newThrowable(cause));
    }
    return mThrowable;
}

From source file:org.openflamingo.remote.thrift.thriftfs.ThriftHandlerBase.java

/**
 * Return a list of threads that currently exist with their stack traces
 *//*  w ww  . j a va 2 s.  c  om*/
public List<ThreadStackTrace> getThreadDump(RequestContext ctx) {
    List<ThreadStackTrace> dump = new ArrayList<ThreadStackTrace>();

    Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
        final Thread t = entry.getKey();
        final StackTraceElement[] frames = entry.getValue();

        ThreadStackTrace tst = new ThreadStackTrace();
        tst.threadName = t.getName();
        tst.threadStringRepresentation = String.valueOf(t);
        tst.isDaemon = t.isDaemon();
        tst.stackTrace = new ArrayList<StackTraceElement>();
        for (StackTraceElement ste : frames) {
            StackTraceElement tFrame = new StackTraceElement();
            tFrame.className = ste.getClassName();
            tFrame.fileName = ste.getFileName();
            tFrame.lineNumber = ste.getLineNumber();
            tFrame.methodName = ste.getMethodName();
            tFrame.isNativeMethod = ste.isNativeMethod();
            tFrame.stringRepresentation = String.valueOf(ste);
            tst.stackTrace.add(tFrame);
        }
        dump.add(tst);
    }
    return dump;
}

From source file:org.stenerud.kscrash.KSCrash.java

/** Report a Java exception.
 *
 * @param exception The exception.// w ww . ja  v  a2  s . c om
 */
public void reportJavaException(Throwable exception) {
    try {
        JSONArray array = new JSONArray();
        for (StackTraceElement element : exception.getStackTrace()) {
            JSONObject object = new JSONObject();
            object.put("file", element.getFileName());
            object.put("line", element.getLineNumber());
            object.put("class", element.getClassName());
            object.put("method", element.getMethodName());
            object.put("native", element.isNativeMethod());
            array.put(object);
        }
        reportUserException(exception.getClass().getName(), exception.getMessage(), "java",
                exception.getStackTrace()[0].getFileName(), exception.getStackTrace()[0].getLineNumber(), array,
                false, false);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:io.teak.sdk.Raven.java

public void reportException(Throwable t) {
    if (t == null) {
        return;// w  w w  .  ja  va 2  s .  co  m
    }

    HashMap<String, Object> additions = new HashMap<>();
    ArrayList<Object> exceptions = new ArrayList<>();
    HashMap<String, Object> exception = new HashMap<>();

    exception.put("type", t.getClass().getSimpleName());
    exception.put("value", t.getMessage());
    exception.put("module", t.getClass().getPackage().getName());

    HashMap<String, Object> stacktrace = new HashMap<>();
    ArrayList<Object> stackFrames = new ArrayList<>();

    StackTraceElement[] steArray = t.getStackTrace();
    for (int i = steArray.length - 1; i >= 0; i--) {
        StackTraceElement ste = steArray[i];
        HashMap<String, Object> frame = new HashMap<>();

        frame.put("filename", ste.getFileName());

        String method = ste.getMethodName();
        if (method.length() != 0) {
            frame.put("function", method);
        }

        int lineno = ste.getLineNumber();
        if (!ste.isNativeMethod() && lineno >= 0) {
            frame.put("lineno", lineno);
        }

        String module = ste.getClassName();
        frame.put("module", module);

        boolean in_app = true;
        if (module.startsWith("android.") || module.startsWith("java.") || module.startsWith("dalvik.")
                || module.startsWith("com.android.")) {
            in_app = false;
        }

        frame.put("in_app", in_app);

        stackFrames.add(frame);
    }
    stacktrace.put("frames", stackFrames);

    exception.put("stacktrace", stacktrace);

    exceptions.add(exception);
    additions.put("exception", exceptions);

    try {
        Report report = new Report(t.getMessage(), Level.ERROR, additions);
        report.sendToService();
    } catch (Exception e) {
        Log.e(LOG_TAG, "Unable to report Teak SDK exception. " + Log.getStackTraceString(t) + "\n"
                + Log.getStackTraceString(e));
    }
}