Today, I started to learn about Jakarta commons Lang. I am reading o'reilly's Jakarta commons cookbook.
The reflectiontostringbuilder class is used to implement the tostring () method in the class. It adopts the Java reflection mechanism (reflection) and uses the accessibleobject class in the reflection package to bypass access control and directly access the Private Members of the object. Therefore, when using this class, pay attention to the security policy of the runtime environment.
### Print attributes of JavaBean:1. Application Scenario: when calling WS, you often need to check whether the parameters in the call (request) parameter query are set correctly. In this case, you want to print the attributes and their values. 2. Method: override the tostring function in this request class and add the reflectiontostring. tostring (this) function of the common package to the function! Public String tostring () {return reflectiontostringbuilder. tostring (this, tostringstyle. multi_line_style);} print: system. Out. println (CQC. (This statement automatically calls the tostring method) 3.ps: function of tostring:
-Object in short, it is only a method specially added by Sun when developing Java to facilitate string operations of all classes. All objects have the tostring () method, because it is an existing method in the object, and all classes inherit the object, "all objects have this method"
-It is usually only for convenience of output, such as system. out. println (XX). If the "XX" in the brackets is not of the string type, the tostring () method of XX is automatically called. 4. the reflectiontostringbuilder class is used to implement the tostring () method in the class. It adopts the Java reflection mechanism (reflection) and uses the accessibleobject class in the reflection package to bypass access control and directly access the Private Members of the object. Therefore, when using this class, pay attention to the security policy of the runtime environment.
--------------------------------
After reading reflectiontostringbuilder, I found that there are too many output content. If you want to customize the content output by the tostring () method, you need to use the tostringbuilder and tostringstyle classes.
The append method in the tostringbuilder class can add basic types, arrays, and objects to the class. Only the added methods are output by the tostring method.
The tostringstyle class is used to format the output content.
Example:
Public String tostring (){
Return new tostringbuilder (this, tostringstyle. multi_line_style)
. Append ("name", name)
. Append ("Age", age)
. Tostring ();
}
For other functions, refer to the document of Jakarta commons.