Obtain the bitwise operator of the method caller and Java.

Source: Internet
Author: User
Tags bitwise operators
Caller of the get Method

If you write a super complex, super large project, and suddenly want to know which classes have called the important method in importantclass during later project debugging and modification, what would you do?

First, you may say: I cannot use it! In this case, you can return.
Method 1: The most common and effective method is to use the full-text search method name of IDE. Of course, this is the most effective and fast. However, if the important method is defined in many other classes, you will get a lot of junk search results. At the same time, you can only search for the results under the directory, and you will not be able to find any way to call the important method at runtime.

Practice 2: This is what I want to talk about.ProgramObtain method callers. The most intuitive example is that we use log4j every day. When you execute log. debug or other output operations, It outputs the class name, method name, and even the number of rows in the specified format. If we insert such a segment in the important method, we can know who called the important method at runtime.

The demo contains detailed comments and a complete example. We know thatCodeCopy to the link program and modify fullclassname.

/***/ /**
*
 */

/***/ /**
*@ AuthorZangmeng
*
*
* If we observe the output when an exception occurs, we will know the structure of the Java call stack.
* The last called method is at the top of the stack.
* The process of this method is to first obtain the call stack and then search from the top of the stack.
* Until we find the important class we care about.
* Then, the method behind this class is the caller of the important method of the important class we are concerned about.
* At this time, we check that the first class is not important class, and this class must be called in the important class.
* Important method class and method.
* Print it out.
* In the same way, we can also obtain a higher call relationship.
* Keep tracing the thread startup method.
*
 */
Public   Class Important ... {
Public   Void Important () ... {
String fullclassname =   " Important " ;
// First obtain the call stack
Stacktraceelement stack [] = ( New Throwable (). getstacktrace ();
// Search from the stack until we find our important class.
Int IX =   0 ;
While (Ix < Stack. length) ... {
Stacktraceelement Frame = Stack [ix];
String cname = Frame. getclassname ();
If (Cname. Equals (fullclassname )) ... {
Break;
}
IX ++ ;
}
// At this time, the IC is placed in the important class.
While (Ix < Stack. length) ... {
Stacktraceelement Frame = Stack [ix];
String cname = Frame. getclassname ();
If ( ! Cname. Equals (fullclassname )) ... {
// The first important class is the caller
System. Out. println ( " The class name of this method is called: " + Cname );
System. Out. println ( " In " + Cname + " Method Name of the method called in: " + Frame. getmethodname ());
}
IX ++ ;
}
/***/ /**
* Some important operation is followed :)
*/

System. Out. println ( " _ This is an important method, and I care for the infor OF THE invoker! __ " );
}
}
Bitwise operators in Java

It is often forgotten because it is not commonly used. Simply take the time to summarize and forget it.
For Java, bit operators are rarely used.
&: Bitwise AND.
|: By bit or.
^: Returns the bitwise XOR.
~ : Bitwise inversion.
<: Shifts left. 0 is inserted in the upper part; otherwise, 1 is inserted;
>>: Right shift. 0 is inserted in the upper part; otherwise, 1 is inserted;
>>>: Shifts right unsigned. No matter whether it is positive or negative, the value is 0 in the upper part.
What's strange is that Java seems to be probably not the same or operator, why? We want ~ And ^ in combination to replace the same or operator?
Http://www.matrix.org.cn/resource/article/43/43978_Java_Bitfields_Bitboards.html thisArticleI spoke about it quite well. I read it at the beginning and it felt very good, but it was too long. I should not read it at work. I will read it slowly after I leave it as a memo.

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.