Technorati Tag: Modifier, java, toString
For the first time I wrote this kind of blog on this topic, I have many questions I don't know or can't write well. If I can't write well, I 'd like to ask you to point it out. I hope you can leave your suggestions, let me work hard. When I write this kind of blog, I can make a small improvement. First of all, thank you for reading this article.
When learning java reflection, I have been confused about getting and interpreting modifiers-Member. getModifiers () returns a specific bytecode and uses Modifier. toStringint mod) to explain this bytecode mod, will return the modifier string form. How is this mechanism implemented ?? Why is this mechanism used ??
For Member. getModifiers), a specific bytecode is returned, which is an integer expressed in hexadecimal notation. Let's take a look at how to explain this specific bytecode, that is, the implementation of Modifier. toString (int mod.
The Modifier class defines a series of public static constants in hexadecimal notation) to indicate various modifiers, as follows:
public static final int PUBLIC = 0x00000001;public static final int PRIVATE = 0x00000002;public static final int PROTECTED = 0x00000004;public static final int STATIC = 0x00000008;public static final int FINAL = 0x00000010;public static final int SYNCHRONIZED = 0x00000020;public static final int VOLATILE = 0x00000040;public static final int TRANSIENT = 0x00000080;public static final int NATIVE = 0x00000100;public static final int INTERFACE = 0x00000200;public static final int ABSTRACT = 0x00000400;public static final int STRICT = 0x00000800;
We can see that the values of each static constant are several power values of 2, so that we can use logic and & operations to determine whether the constant is contained. The following is the implementation of Modifier. toString (int mod:
Public static String toString (int mod ){
StringBuffer sb = new StringBuffer ();
Int len;
If (mod & PUBLIC )! = 0) sb. append ("public ");
If (mod & PROTECTED )! = 0) sb. append ("protected ");
If (mod & PRIVATE )! = 0) sb. append ("private ");
/* Note that the following sequence cannot be changed. The following sequence is specified on the White Paper */
If (mod & ABSTRACT )! = 0) sb. append ("abstract ");
If (mod & STATIC )! = 0) sb. append ("static ");
If (mod & FINAL )! = 0) sb. append ("final ");
If (mod & TRANSIENT )! = 0) sb. append ("transient ");
If (mod & VOLATILE )! = 0) sb. append ("volatile ");
If (mod & SYNCHRONIZED )! = 0) sb. append ("synchronized ");
If (mod & NATIVE )! = 0) sb. append ("native ");
If (mod & STRICT )! = 0) sb. append ("strictfp ");
If (mod & INTERFACE )! = 0) sb. append ("interface ");
If (len = sb. length ()> 0)/* judge whether there is a modifier */
Return sb. toString (). substring (0, len-1 );
Return "";
}
In addition, in Modifier, a series of private Static constants are as follows:
static final int BRIDGE = 0x00000040;static final int VARARGS = 0x00000080;static final int SYNTHETIC = 0x00001000;static final int ANNOTATION = 0x00002000;static final int ENUM = 0x00004000;
These private) are not exposed to public APIs, or because these private) constants have different meanings for field and method, and there is no other way to distinguish between these two meanings; or because these private) constants are not keywords in java. At the same time, you can take a look at the functions of these private) constants. The following is the implementation of Modifier. isSynthetic:
static boolean isSynthetic(int mod) { return (mod & SYNTHETIC) != 0;}
Related functions in Field and Method:
// Public boolean isSynthetic () {return Modifier. isSynthetic (getModifiers ();} // The public boolean isBridge () {return (getModifiers () & Modifier. BRIDGE )! = 0;} public boolean isVarArgs () {return (getModifiers () & Modifier. VARARGS )! = 0;} // The function public boolean isEnumConstant () {return (getModifiers () & Modifier. ENUM) in Filed )! = 0 ;}
Now we will discuss why we use this bytecode to obtain and interpret modifiers. Note: The following are some of my guesses, which are not necessarily correct. If you know the answers to one or more questions, please give me some reference suggestions. Thank you very much)
First, because the computer uses binary for computation, it directly uses the hexadecimal form to express modifiers, which is conducive to providing the computation speed. The use of specific bytecode helps to quickly determine which modifiers are contained.
In this way, I would like to extend my thoughts on whether strings can be used for obtaining and interpreting modifiers. I personally think this method is not desirable: first, the execution efficiency of strings is far less than the execution efficiency of binary code; second, it is difficult to specify the output sequence of various modifiers by using strings. How can we implement string-based implementation ?? If you know, I hope you can give your reference.
Can enumeration be selected for implementation ?? I am also thinking about it. I feel like it is feasible. I don't know where it is. How can I determine which enumerated constants are contained ?? If you know one or two of the minor questions, please kindly advise.