Create TOSTRING () to create automation

zhaozj2021-02-16  85

Abstract Experienced Java developers know that it is important to write the toString method. Record and debugging objects in the form of human understanding of human understanding is quite easy; in particular like this when processing distributed applications. Unfortunately, for many classes to implement the TSTRING method can be time consuming - especially for classes with many properties. Because Tostring behavior is quite rule, it is best to make the task that creates this method automatically. This utility provided herein can help you achieve this, while also alleviating your burden on development. Developers who participate in large projects usually have to write a useful toString method for several hours. Even if you do not provide each class, yourself, but each data container must have its own TSTRING method. Let each developer writes the toString method in their own way 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 specify a single format for the TOSTRING method and automate 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 falsed.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"); system.exit (0);} try {class targetclass = class.forname (args [0]); if (! Targetclass.isprimitive) ! && targetClass = String.class) {Field fields [] = targetClass.getDeclaredFields (); class cSuper = targetClass.getSuperclass (); // retrieve superclass output ( "StringBuffer buffer = new StringBuffer (500);"); / / Construct Buffer IF (CSUPER! = NULL && CSUPER! = ObjectFor (int J = 0; J

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

New Post(0)