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