We want to implement a SonarQube rule to block the build if the database was called inside a for-loop/iteration
In the following code, we want to create a custom rule (complex) to fail if any EmployeeDao method was called inside a for-loop/iteration:
class OrganizationService {
@Inject
private EmployeeService employeeService;
public void doSomeLogic() {
List<Employee> employeeList = employeeService.getEmployees();
/*
... some logic here
*/
employeeList.forEach(employee -> employeeService.updateEmployee(employee));
}
}
class EmployeeService {
@Inject
private EmployeeDao employeeDao;
public List<Employee> getEmployees() {
return null;
}
public void updateEmployee(Employee employee) {
employeeDao.update(employee);
}
}
class EmployeeDao {
public void update(Employee employee) {
}
}
class Employee { }
We want sonar to fail for this line:
employeeList.forEach(employee -> employeeService.updateEmployee(employee));
Is this a valid approach? or sonar is not the correct tool in our situation?