do is the e-commerce system, the main system to generate orders, respectively, added to a different queue to the other three subsystems to asynchronous processing, orders and merchandise is a one-to-many relationship, in the actual test, found that there is a subsystem from the queue to obtain the order entity, the list of goods has been empty, The developer of the subsystem has always said that it is not getting the value. Because of this problem, the test has not been passed, the last resort can only play logs, repeated troubleshooting. Finally found in this subsystem, the order entity from the queue, the product list is not empty, but after executing a method, the order entity class product is empty. After locating the problem, debugging the method, and later found that the entire method of logic is not a problem, but at the end of the method, the incoming parameter list is executed clean () operation.
because the business scenario of the actual project is more complex, here is a simple example to illustrate. Defines a city 's entity class and the country entity class
public class City {
Private Integer ID;
private String name;
the set and get methods are omitted
}
public class Country {
Private Integer ID;
private String name;
Private list<city> listcity; Country and City are a one-to-many relationship.
the set and get methods are omitted
}
Then write the test class to run:
public class Test {
public static void Main (string[] args) {
City City1=new City ();
City1.setid (1);
City1.setname (" Guangzhou ");
City City2=new City ();
City2.setid (2);
City2.setname (" Shanghai ");
List<city> listcity=new arraylist<> ();
Listcity.add (city1);
Listcity.add (City2);
Country Country=new Country ();
Country.setid (1);
Country.setname (" China ");
Country.setlistcity (listcity);
Getcity (Country.getlistcity ());
SYSTEM.OUT.PRINTLN ("List size:" +country.getlistcity (). Size ());
}
public static void Getcity (List<city> listcity) {
Listcity.clear ();
}
}
Run the program and the result is:list size:0
if you comment out listcity.clear () in the Getcity method , the result is:list Size:2
This is the cause of the bug that caused the project , in general, unless you have determined that the collection is not used by the program, the Clear () operation will not be performed at the end of the method .
Once you know the reason for this, it is now time to analyze why this method is called to cause this effect. View Jdkclean () source code:
by reading source discovery, when clean () is called, all the data in the collection is removed and the length of the collection is set to 0. This means that the collection becomes empty after the method is called.
Sweep
Focus on Java High Master
Bug caused by misuse of the clean () method in the list collection in Java