]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/aspects/StatisticsAspect.aj
StatisticsAspect: adding advices for analysis results
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / aspects / StatisticsAspect.aj
1 package no.uio.ifi.refaktor.aspects;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import no.uio.ifi.refaktor.analyze.analyzers.CompilationUnitWideExtractAndMoveMethodAnalyzer;
7 import no.uio.ifi.refaktor.analyze.analyzers.SearchBasedExtractAndMoveMethodAnalyzer;
8 import no.uio.ifi.refaktor.analyze.analyzers.TypeWideExtractAndMoveMethodAnalyzer;
9 import no.uio.ifi.refaktor.change.executors.ExtractAndMoveMethodExecutor;
10 import no.uio.ifi.refaktor.change.performers.RefactoringPerformer;
11 import no.uio.ifi.refaktor.utils.RefaktorDebug;
12
13 import org.eclipse.jdt.core.IMethod;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.ltk.core.refactoring.Refactoring;
16
17 privileged public aspect StatisticsAspect {
18
19         public class Statistics {
20
21                 private long startTime;
22                 private int methodCount;
23                 private int typeCount;
24                 private int compilationUnitCount;
25                 private int packageCount;
26                 private int extractAndMoveMethodExecutedCount;
27                 private int extractAndMoveMethodNotExecutedCount;
28                 private int extractAndMoveMethodResultFoundCount;
29                 private int extractAndMoveMethodResultNotFoundCount;
30                 private final Map<String, Integer> successfullyPerformedRefactorings;
31                 private final Map<String, Integer> notPerformedRefactorings;
32                 private final String snapshotTimeSinceStart;
33
34                 private Statistics() {
35                         startTime = System.currentTimeMillis();
36                         methodCount = typeCount = compilationUnitCount = packageCount = 0;
37                         extractAndMoveMethodExecutedCount = extractAndMoveMethodNotExecutedCount = 0;
38                         extractAndMoveMethodResultFoundCount = extractAndMoveMethodResultNotFoundCount = 0;
39                         successfullyPerformedRefactorings = new HashMap<String, Integer>();
40                         notPerformedRefactorings = new HashMap<String, Integer>();
41                         snapshotTimeSinceStart = timeSinceStart();
42                 }
43
44                 private Statistics(Statistics statistics) {
45                         this.startTime = statistics.startTime;
46                         this.methodCount = statistics.methodCount;
47                         this.typeCount = statistics.typeCount;
48                         this.compilationUnitCount = statistics.compilationUnitCount;
49                         this.packageCount = statistics.packageCount;
50                         this.extractAndMoveMethodExecutedCount = statistics.extractAndMoveMethodExecutedCount;
51                         this.extractAndMoveMethodNotExecutedCount = statistics.extractAndMoveMethodNotExecutedCount;
52                         this.extractAndMoveMethodResultFoundCount = statistics.extractAndMoveMethodResultFoundCount;
53                         this.extractAndMoveMethodResultNotFoundCount = statistics.extractAndMoveMethodResultNotFoundCount;
54                         this.successfullyPerformedRefactorings = new HashMap<String, Integer>(statistics.successfullyPerformedRefactorings);
55                         this.notPerformedRefactorings = new HashMap<String, Integer>(statistics.notPerformedRefactorings);
56                         this.snapshotTimeSinceStart = timeSinceStart();
57                 }
58
59                 private String timeSinceStart() {
60                         long durationInMillis = System.currentTimeMillis() - startTime;
61                         long secondsSinceAnalysisStart = durationInMillis/1000;
62
63                         if (secondsSinceAnalysisStart > 0)
64                                 return secondsSinceAnalysisStart/60 + "m" + secondsSinceAnalysisStart % 60 + "s";
65                         else
66                                 return durationInMillis + "ms";
67                 }
68
69                 public int getMethodCount() {
70                         return methodCount;
71                 }
72
73                 public int getTypeCount() {
74                         return typeCount;
75                 }
76
77                 public int getCompilationUnitCount() {
78                         return compilationUnitCount;
79                 }
80
81                 public int getPackageCount() {
82                         return packageCount;
83                 }
84
85                 public String getSnapshotTimeSinceStart() {
86                         return snapshotTimeSinceStart;
87                 }
88
89                 public Statistics generateSnapshot() {
90                         return new Statistics(this);
91                 }
92
93                 @Override
94                 public String toString() {
95                         String message = "Statistics:\n-----------\nTime used: " + snapshotTimeSinceStart
96                                         + (packageCount > 0 ? "\n\nNumber of packages analyzed: " + packageCount : "") 
97                                         + (compilationUnitCount > 0 ? "\nNumber of compilation units analyzed: " + compilationUnitCount : "")
98                                         + (typeCount > 0 ? "\nNumber of types analyzed: " + typeCount : "")
99                                         + "\nNumber of methods analyzed: " + methodCount 
100                                         + "\n(" + extractAndMoveMethodResultFoundCount 
101                                         + " methods are candidates for the Extract and Move Method refactoring, while " 
102                                         + extractAndMoveMethodResultNotFoundCount + " are not)"
103                                         + "\n\nPrimitive refactorings performed:";
104
105                         for (String refactoringName: successfullyPerformedRefactorings.keySet()) {
106                                 Integer successCount = successfullyPerformedRefactorings.get(refactoringName);
107                                 Integer notPerformedCount = notPerformedRefactorings.get(refactoringName);
108                                 message += "\n" + refactoringName + ": " + successCount + " performed" 
109                                                 + (notPerformedCount != null ? ", " + notPerformedCount + " not performed" : "")
110                                                 + (" (" + (successCount + (notPerformedCount != null ? notPerformedCount : 0)) + " in total)");
111                         }
112
113                         if (extractAndMoveMethodExecutedCount > 0 || extractAndMoveMethodNotExecutedCount > 0) {
114                                 message += "\n\nExtract and Move Method refactorings executed:"
115                                                 + (extractAndMoveMethodExecutedCount > 0 ? "\nFully executed: " + extractAndMoveMethodExecutedCount : "")
116                                                 + (extractAndMoveMethodNotExecutedCount > 0 ? "\nNot fully executed: " + extractAndMoveMethodNotExecutedCount : "")
117                                                 + "\n(" + (extractAndMoveMethodExecutedCount + extractAndMoveMethodNotExecutedCount) + " in total)";
118                         }
119
120                         return message;
121                 }
122
123         }
124
125         private Statistics statistics;
126
127         public StatisticsAspect() {
128                 this.statistics = new Statistics();
129         }
130
131         pointcut methodAnalyze(SearchBasedExtractAndMoveMethodAnalyzer analyzer) : 
132                 call(* SearchBasedExtractAndMoveMethodAnalyzer.analyze()) && target(analyzer);
133         pointcut typeAnalyze() : call(* TypeWideExtractAndMoveMethodAnalyzer.analyze());
134         pointcut compilationUnitAnalyze() : call(* CompilationUnitWideExtractAndMoveMethodAnalyzer.analyze());
135         pointcut packageAnalyze() : call(* CompilationUnitWideExtractAndMoveMethodAnalyzer.analyze());
136         pointcut performRefactoring(Refactoring refactoring) : 
137                 call(* RefactoringPerformer+.performRefactoring(Refactoring)) && args(refactoring);
138         pointcut extractAndMoveExecuted() : call(* ExtractAndMoveMethodExecutor.execute(..));
139
140         after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) : methodAnalyze(analyzer) {
141                 statistics.methodCount++;
142                 debugPrintMethodAnalysisProgress(analyzer.method);
143         }
144
145         private void debugPrintMethodAnalysisProgress(IMethod method) {
146                 try {
147                         RefaktorDebug.println("#" + statistics.methodCount + " (" + timeSinceStart() + "): " 
148                                         + method.getDeclaringType().getElementName() + "." + method.getElementName() + " (" + "offset: "
149                                         + method.getSourceRange().getOffset() + ", length: " + method.getSourceRange().getLength() + ")");
150                 } catch (JavaModelException e) {
151                         RefaktorDebug.println("No info about " + method.getElementName());
152                 }
153         }
154
155         after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) returning(): methodAnalyze(analyzer) {
156                 statistics.extractAndMoveMethodResultFoundCount++;
157         }
158
159         after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) throwing(): methodAnalyze(analyzer) {
160                 statistics.extractAndMoveMethodResultNotFoundCount++;
161         }
162
163         after(): typeAnalyze() {
164                 statistics.typeCount++;
165         }
166
167         after(): compilationUnitAnalyze() {
168                 statistics.compilationUnitCount++;
169         }
170
171         after(): packageAnalyze() {
172                 statistics.packageCount++;
173         }
174
175         after(Refactoring refactoring) returning() : performRefactoring(refactoring) {
176                 Integer count = statistics.successfullyPerformedRefactorings.get(refactoring.getName());
177                 if (count == null)
178                         statistics.successfullyPerformedRefactorings.put(refactoring.getName(), 1);
179                 else
180                         statistics.successfullyPerformedRefactorings.put(refactoring.getName(), count + 1);
181         }
182
183         after(Refactoring refactoring) throwing() : performRefactoring(refactoring) {
184                 Integer count = statistics.notPerformedRefactorings.get(refactoring.getName());
185                 if (count == null)
186                         statistics.notPerformedRefactorings.put(refactoring.getName(), 1);
187                 else
188                         statistics.notPerformedRefactorings.put(refactoring.getName(), count + 1);
189         }
190
191         after() returning() : extractAndMoveExecuted() {
192                 statistics.extractAndMoveMethodExecutedCount++;
193         }
194
195         after() throwing() : extractAndMoveExecuted() {
196                 statistics.extractAndMoveMethodNotExecutedCount++;
197         }
198
199         public void init() {
200                 this.statistics = new Statistics();
201         }
202
203         public String timeSinceStart() {
204                 return statistics.timeSinceStart();
205         }
206
207         public int getMethodCount() {
208                 return statistics.getMethodCount();
209         }
210
211         public int getTypeCount() {
212                 return statistics.getTypeCount();
213         }
214
215         public int getCompilationUnitCount() {
216                 return statistics.getCompilationUnitCount();
217         }
218
219         public int getPackageCount() {
220                 return statistics.getPackageCount();
221         }
222
223         public Statistics getSnapshot() {
224                 return statistics.generateSnapshot();
225         }
226
227 }