# Automatically Injecting Robustness Statements into Distributed Applications
## 4 Dependency Analysis and Code Transformation  
### Data Dependency Analysis  
When aiming at transforming the code to introduce in it asynchronous calls, threads have be appropriately guarded to ensure safety and prevent issues due to the access of shared data. Our proposed transformation approach performs a thorough inspection of RMIs and the following instructions to identify potential dependencies. The analysis relies on [PERSON]'s Conditions [20], a set of criteria used to determine the independence of instructions in concurrent processing. [PERSON]'s Conditions involve examining two sets of variables for each instruction: the input set and the output set. The input set comprises all variables that the instruction reads, while the output set includes all variables that the instruction writes. The not empty intersection of the output set for the RMI with the input set of the subsequent instructions indicates that the subsequent instructions are dependent on the output of the RMI [38].  
Algorithm 1 outlines the steps for performing the data dependence analysis. Firstly, it retrieves the input set (variables read by the statement) and the output set (variables written by the statement) for the identified RMI. Secondly, for each statement following the RMI statement, it computes the intersection between these sets. If the intersection contains at least one element, the current instruction is identified as data-dependent, and synchronization has to be inserted at this point to ensure correctness. Otherwise, if no intersection was found, the next instruction will be analyzed. If no data-dependent statements were identified, then the last instruction of the method will be the point of code where synchronization will be handled.  
```
functionDataDependenceAnalysis(m,rmi) \(inputSetS_{1}\gets getInputSet(m,rmi)\) \(outputSetS_{1}\gets getOutputSet(m,rmi)\) forin\(\leftarrow\)m.getFollowingInstructions(rmi)do \(inputSetS_{2}\gets getInputSet(m,in)\) \(outputSetS_{2}\gets getOutputSet(m,in)\) if\(checkIntersections(inputSetS_{1},outputSetS_{1},inputSetS_{2},outputSetS_{2})\)then returnin endif endfor \(lastInstruction\gets getLastInstruction(m)\) returnlastInstruction endfunction
```  
**Algorithm 1** The operations performed to identify the data-dependent instructions.  
Listing 1 provides an example of code for which data dependence analysis was performed. Firstly, line 5 was identified as an RMI suitable for the asynchronous execution transformation, as the method service.getAuthors() is invoked on a type declared as a subtype of Remote. From line 6 onward, the analysis seeks to detect data-dependent statements, identifying such a statement in line 8 due to the use of the variable authors for subsequent Java Stream operations. This data-dependent statement marks the synchronization point, i.e., the original thread has to wait for the completion of the forked thread.  
Additionally, line 6 contains another RMI, a call to another method of a subtype of Remote. The analysis determined that the variable book is in the output set of the instruction. Then, a subsequent data-dependent statement was found in line 8 (book appears in line 10; however, it is in the same statement starting in line 6). Given that no statements are present between the remote call (line 6) and the dependent statement (line 8), then no parallelism can be achieved for the two said lines.  
```
1 publicclassClient{
2 privatestaticDataInterfaceservice=newServiceProxy();
3
4 publicstaticOptional<Author>extractMostReviewedAuthor(){
5 HashMap<String,Author>authors=service.getAuthors();
6 Bookbook=service.getMostReviewedBook();
7
8 returnauthors.values().stream()
9.filter(auth->auth.getBooks().stream()
10.anyMatch(b->b.getTitle().equals(book.get().getTitle())))
11.findFirst();
12}
13}
```  
Listing 1: An example of data-dependent instructions within a method declaration. The method calls at line 5 and 6 are remote method calls; line 8 is data-dependent on line 5 and 6.