Create TOSTRING () to create automation

zhaozj2021-02-08  260

Developers who participate in large projects usually have to write useful for hours

TOSTRING method. Even if you don't provide yourself for each class

Tostring method, but each data container must have its own TSTRING method. Let each developer write according to their own methods

The Tostring method may cause confusion; each developer will undoubtedly propose a unique format. As a result, use such an output during the debugging process will add unnecessary trouble, and there is nothing benefit. Therefore, each project should be

The TString method specifies a single format and automates them.

Make TOSTRING Create Automation I will demonstrate a utility, you can use it to implement TOSTRING automatic creation. This tool automatically generates a rule, a strong TSTRING method for the specified class, which eliminates the time used to develop the method. It also focuses on the format of toString (). If you change the format, you must regenerate the TSTRING method; however, this is still much easier than the manual change to hundreds of thousands.

It is also easy to maintain the generated code. If you add more properties to the class, you may also need to make some modifications to the TSTRING method. Because the toString method is automatically generated, you only have to run this utility again to complete your changes. This is more simple than manual methods, and the possibility of mistakes is also small.

The code does not intend to explain the Reflection API; the following code assumes that you understand the basic concepts of Reflection. To view the document of the Reflection API, you can access the reference resource section. The source code of the utility is as follows:

package fareed.publications.utilities; import java.lang.reflect *;. public class ToStringGenerator {public static void main (String [] args) {if (args.length == 0) {System.out.println ( "Provide the Class name as the commit line argument "); system.exit (0);} try {class targetclass = class.forname (args [0]); if (! targetclass.isprimitive () && targetclass! = string.class) { Field Fields [] = targetclass.getDeclaredfields (); Class Csuper = targetclass.getsuperclass (); // Retrieval Sumeric Output ("StringBuffer Buffer = New StringBuffer (500);"); // Construct Buffer IF (CSUPER! = NULL && CSUPER! = Object.class) {Output ("Buffer.Append ());"); // Super class toString ()} for (int J = 0; J

Output ("Buffer.Append (this." Fields [j] .getname () ".tostring ());"); Output ("Else Buffer.Append (/" value is null / ");"); } // else end} // cycle end OUTPUT ("Return Buffer.tostring ();");}} catch (classnotfoundexception e) {system.out.println ("Class Not Found In The Class Path"); System. Exit (0);}}}}}}}}} {system.out.println (data);} The format of the code output channel code also depends on your project tool requirements. Some developers may like to store these code into the user-defined file on the disk. Other developers are very satisfied with the System.out console, they can use the console to manually copy these code or embed the actual file. I left these options to you, this article only uses the simplest method: system.out statement.

This method has two significant limitations. The first limit is that it does not support the loop of the object. If the object A contains a reference to the object B, the object B also contains a reference to the object A, and this tool cannot be processed. However, this situation is rare to occur for many projects.

The second limit is to add or reduce the member variables asking to regenerate the TSTRING method. This is not a tool-specific problem that needs to be done if you do not need this tool.

Summary In this article, I explain a small automatic utility that can truly improve the efficiency of the developer, and it plays a small but very important role in the entire project.

转载请注明原文地址:https://www.9cbs.com/read-854.html

New Post(0)