@@ -36,79 +36,76 @@ public void forLoopToFunctional(list[loc] locs, set[str] checkedExceptions) {
36
36
}
37
37
38
38
// Losing formatting after a method is refactored.
39
- public CompilationUnit refactorEnhancedForStatements (CompilationUnit unit ) {
39
+ public tuple [CompilationUnit unit , int occurrences ] refactorEnhancedForStatements (CompilationUnit unit ) {
40
+ int occurrences = 0 ;
40
41
alreadyComputedClassFields = false ;
41
42
CompilationUnit refactoredUnit = visit (unit ) {
42
43
case (MethodDeclaration ) `<MethodModifier * mds > <MethodHeader methodHeader > <MethodBody mBody > ` : {
43
44
MethodBody refactoredMethodBody = visit (mBody ) {
44
- case MethodBody methodBody : insert refactorEnhancedForStatementsInMethodBody (unit , methodHeader , methodBody );
45
+ case MethodBody methodBody : {
46
+ refactored = refactorEnhancedForStatementsInMethodBody (unit , methodHeader , methodBody );
47
+ occurrences += refactored .occurrences ;
48
+ insert refactored .body ;
49
+ }
45
50
};
46
51
47
52
insert ((MethodDeclaration ) `<MethodModifier * mds > <MethodHeader methodHeader > <MethodBody refactoredMethodBody > ` );
48
53
}
49
54
};
50
55
51
- return refactoredUnit ;
56
+ return < refactoredUnit , occurrences > ;
52
57
}
53
58
54
59
// TODO What happens when two for statements are refactored inside the same method?
55
- public MethodBody refactorEnhancedForStatementsInMethodBody (CompilationUnit unit , MethodHeader methodHeader , MethodBody methodBody ) {
60
+ public tuple [ MethodBody body , int occurrences ] refactorEnhancedForStatementsInMethodBody (CompilationUnit unit , MethodHeader methodHeader , MethodBody methodBody ) {
56
61
set [MethodVar ] availableVars = {};
57
62
alreadyComputedCurrentMethodAvailableVars = false ;
63
+ occurrences = 0 ;
64
+
58
65
MethodBody refactoredMethodBody = methodBody ;
59
66
60
67
top -down visit (methodBody ) {
61
- case EnhancedForStatement forStmt : {
62
- if (PRINT_DEBUG ) {
63
- println ("for" );
64
- println (forStmt );
65
- println ();
66
- }
67
-
68
- if (!alreadyComputedClassFields ) {
69
- currentClassFields = findClassFields (unit );
70
- alreadyComputedClassFields = true ;
71
- }
72
-
73
- if (!alreadyComputedCurrentMethodAvailableVars ) {
74
- methodVars = findLocalVariables (methodHeader , methodBody );
75
- availableVars = retainLocalVariablesIfDuplicates (currentClassFields , methodVars );
76
- alreadyComputedAvailableVars = true ;
77
- }
68
+ case EnhancedForStatement enhancedForStmt :
69
+ visit (enhancedForStmt ) {
78
70
79
- top -down visit (forStmt ) {
80
- case EnhancedForStatement enhancedForStmt : {
81
- visit (enhancedForStmt ) {
82
- case (EnhancedForStatement ) `for ( <VariableModifier * _> <UnannType _> <VariableDeclaratorId iteratedVarName > : <Expression collectionId > ) <Statement loopBody > ` : {
83
-
84
- if (isLoopRefactorable (availableVars , collectionId , loopBody )) {
85
-
86
- try {
87
- refactoredMethodBody = refactorEnhancedToFunctional (availableVars , enhancedForStmt , methodBody , iteratedVarName , collectionId );
88
- refactoredCount += 1 ;
71
+ case (EnhancedForStatement ) `for ( <VariableModifier * _> <UnannType _> <VariableDeclaratorId iteratedVarName > : <Expression collectionId > ) <Statement loopBody > ` : {
72
+
73
+ if (!alreadyComputedClassFields ) {
74
+ currentClassFields = findClassFields (unit );
75
+ alreadyComputedClassFields = true ;
76
+ }
77
+
78
+ if (!alreadyComputedCurrentMethodAvailableVars ) {
79
+ methodVars = findLocalVariables (methodHeader , methodBody );
80
+ availableVars = retainLocalVariablesIfDuplicates (currentClassFields , methodVars );
81
+ alreadyComputedAvailableVars = true ;
82
+ }
89
83
90
- if (PRINT_DEBUG ) {
91
- println ("refactored: " + toString (refactoredCount ));
92
- println (enhancedForStmt );
93
- println ("---" );
94
- println (refactoredMethodBody );
95
- println ();
96
- }
97
- } catch :
98
- continue ;
99
-
84
+ if (isLoopRefactorable (availableVars , collectionId , loopBody )) {
85
+
86
+ try {
87
+ refactoredMethodBody = refactorEnhancedToFunctional (availableVars , enhancedForStmt , methodBody , iteratedVarName , collectionId );
88
+ occurrences += 1 ;
89
+
90
+ if (PRINT_DEBUG ) {
91
+ println ("refactored: " + toString (occurrences ));
92
+ println (enhancedForStmt );
93
+ println ("---" );
94
+ println (refactoredMethodBody );
95
+ println ();
100
96
}
101
- }
97
+ } catch :
98
+ continue ;
102
99
}
103
100
}
101
+
104
102
}
105
- }
106
103
107
104
case (EnhancedForStatementNoShortIf ) `for ( <VariableModifier * _> <UnannType _> <VariableDeclaratorId _> : <Expression _> ) <StatementNoShortIf stmt > ` :
108
105
println ("TODO" );
109
106
}
110
107
111
- return refactoredMethodBody ;
108
+ return < refactoredMethodBody , occurrences > ;
112
109
}
113
110
114
111
private bool isLoopRefactorable (set [MethodVar ] availableVariables , Expression collectionId , Statement loopBody ) {
0 commit comments