In Java, get the current method name and line number ... ---logging and tracking can be done

Source: Internet
Author: User

jdk1.5 before:

(New Exception ()). Getstacktrace () [2]. Getmethodname ()

(New Exception ()). Getstacktrace () [2]. GetClassName () ()

(New Exception ()). Getstacktrace () [2]. Getlinenumber ()

After jdk1.5

Thread.CurrentThread (). Getstacktrace () [2]. Getmethodname ()

Thread.CurrentThread (). Getstacktrace () [2]. GetClassName () ()

Thread.CurrentThread (). Getstacktrace () [2]. Getlinenumber ()

Actually look at the source code

Thread.CurrentThread (). Getstacktrace () is used (new Exception ()). Getstacktrace ()

As follows:

/** * Returns An array of stack trace elements representing the stack dump * of this thread. This method would return a zero-length array if * The thread has not started or has terminated. * If The returned array is of Non-zero length then the "the" the "the" the "the" the "the" the top of the stack, which is the most recent * method invocation in the sequence. The last element of the array * represents the bottom of the stack, which are the least recent method * invocation in the S Equence. * <p>if There is a security manager, and this thread are not * "Current thread", then the security manager ' s * &L T;tt>checkpermission</tt> method are called with A * <tt>runtimepermission ("Getstacktrace") </tt> Permission * to = if it ' s OK to get the stack trace. * <p>some virtual machines may, under Some circumstances, omit one * or more stack frames from the stack trace. In the extreme case, * a virtual machine that has no stack trace information concerninG * This thread are permitted to return a zero-length array from this *. * @return An array of <tt>stacktraceelement</tt&gt, and * each represents one stack frame. * * @throws SecurityException * If a security manager exists and their * <tt>checkPermission</tt> method doesn ' t Allow * Getting the stack trace of thread. * @see securitymanager#checkpermission * @see runtimepermission * @see throwable#getstacktrace * * @since 1.5/Public St Acktraceelement[] Getstacktrace () {if (this!= thread.currentthread ()) {//Check for getstacktrace permission Securityma Nager security = System.getsecuritymanager (); if (security!= null) {security.checkpermission (securityconstants.get_stack_trace_permission);} if (!isalive ()) {Retu RN Empty_stack_trace; Return Dumpthreads (new thread[] {this}) [0]; else {//Don ' t need JVM help for the current thread return (new Exception ()). Getstacktrace ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.