# Refactoring for Java-Structured Concurrency
## 4 Design
### Transformation  
This section elaborates on the refactoring transformation, which takes the method \(m\) as input. It introduces structured concurrency by modifying the AST source code. The key steps include adjusting the variable scope with the results of the _scope Analysis_, inserting and deleting AST nodes according to the immutability of the AST nodes, introducing the structured concurrency API, and performing a consistency check to ensure the correctness of the refactoring. Among them, the immutability of AST nodes means that because the AST is a syntax-based tree structure, each part of the code corresponds to a node. When modifying the code structure, a new node must be created first, and the information of the original node must be copied to the new node instead of directly modifying the parent-child relationships of the original node. Algorithm 3 presents a detailed delineation of the transformation.  
1. _ReStruct_ obtains a copy (\(m\)') of the method \(m\) and uses it as the input for the subsequent consistency check (it will be presented in the next section) (line 1). Then, it imports the necessary structured concurrency packages and creates a _scope_ object to represent the structured concurrency context (lines 2-3);
2. It obtains the result of the _scopeAnalysis_ and records it as _set_. Then, it iterates the collection _set_, redefines the variables therein, and deletes their definitions in the original program (lines 7-9);It checks if \(ss\) and \(gs\) have a common parent node, _twr_. If not, it creates a _try-with-resource_ node as _twr_ and inserts _twr_ after the definitions above (lines 7-10);
4. It adds the _scope_ object to the resource of _twr_ and submits the task using the method _scope.fork_ (lines 11-12);
5. For methods of type \(K_{2}\), because of the immutability of the AST node, it iterates \(os\), using the method _AST.copyNode_ to duplicate each statement and insert the copy into _twr_. Finally, it removes the original node from the AST (lines 13-16);
6. It inserts methods _scope.join_ and _scope.throwlfFailed_ and replaces _Future.get_ with _Future.resultNow_ (lines 17-18). Then, it tries to remove statements \(cs\) and \(ds\) from the AST (line 19);
7. Finally, it performs a consistency check on the refactored method (\(m\)). If it fails the consistency check, it returns _false_ (lines 19-21).  
```
Input: Method: \(m\) Output: True or False
1 FunctionTransformation(\(m\)):
2 m' \(\leftarrow\) m ;
3 Import packages;
4 scope \(\leftarrow\) Create StructuredScope object;
5 set \(\leftarrow\) scopeAnalysis(m) ;
6 foreachvar in setdo
7 Define var and remove the original one;
8\(\mathit{twr}\)\(\leftarrow\) Try to obtain _twr_ in \(m\);
9 iftwr == nullthen
10\(\mathit{twr}\)\(\leftarrow\) Create tryStatement in AST;
11 Add scope to _twr_'s resources;
12 Using _scope.fork_ to submit task;
13 ifminstanceOf\(K_{2}\)then
14 foreachst in os do
15 Insert AST.copyNode(st);
16 remove(st);
17 Insert scope.join and scope.throwlfFailed;
18 Replace \(gs\) with _future.resultNow()_;
19 Remove \(cs\) and \(ds\) if they exist;
20 if!consistency(m, m')then
21 return False;
22 return True;
```  
**Algorithm 3**Transformation