1 package no.uio.ifi.refaktor.aspects;
3 import java.util.HashMap;
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;
13 import org.eclipse.jdt.core.IMethod;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.ltk.core.refactoring.Refactoring;
17 privileged public aspect StatisticsAspect {
19 public class Statistics {
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;
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();
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();
59 private String timeSinceStart() {
60 long durationInMillis = System.currentTimeMillis() - startTime;
61 long secondsSinceAnalysisStart = durationInMillis/1000;
63 if (secondsSinceAnalysisStart > 0)
64 return secondsSinceAnalysisStart/60 + "m" + secondsSinceAnalysisStart % 60 + "s";
66 return durationInMillis + "ms";
69 public int getMethodCount() {
73 public int getTypeCount() {
77 public int getCompilationUnitCount() {
78 return compilationUnitCount;
81 public int getPackageCount() {
85 public String getSnapshotTimeSinceStart() {
86 return snapshotTimeSinceStart;
89 public Statistics generateSnapshot() {
90 return new Statistics(this);
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:";
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)");
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)";
125 private Statistics statistics;
127 public StatisticsAspect() {
128 this.statistics = new Statistics();
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(..));
140 after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) : methodAnalyze(analyzer) {
141 statistics.methodCount++;
142 debugPrintMethodAnalysisProgress(analyzer.method);
145 private void debugPrintMethodAnalysisProgress(IMethod method) {
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());
155 after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) returning(): methodAnalyze(analyzer) {
156 statistics.extractAndMoveMethodResultFoundCount++;
159 after(SearchBasedExtractAndMoveMethodAnalyzer analyzer) throwing(): methodAnalyze(analyzer) {
160 statistics.extractAndMoveMethodResultNotFoundCount++;
163 after(): typeAnalyze() {
164 statistics.typeCount++;
167 after(): compilationUnitAnalyze() {
168 statistics.compilationUnitCount++;
171 after(): packageAnalyze() {
172 statistics.packageCount++;
175 after(Refactoring refactoring) returning() : performRefactoring(refactoring) {
176 Integer count = statistics.successfullyPerformedRefactorings.get(refactoring.getName());
178 statistics.successfullyPerformedRefactorings.put(refactoring.getName(), 1);
180 statistics.successfullyPerformedRefactorings.put(refactoring.getName(), count + 1);
183 after(Refactoring refactoring) throwing() : performRefactoring(refactoring) {
184 Integer count = statistics.notPerformedRefactorings.get(refactoring.getName());
186 statistics.notPerformedRefactorings.put(refactoring.getName(), 1);
188 statistics.notPerformedRefactorings.put(refactoring.getName(), count + 1);
191 after() returning() : extractAndMoveExecuted() {
192 statistics.extractAndMoveMethodExecutedCount++;
195 after() throwing() : extractAndMoveExecuted() {
196 statistics.extractAndMoveMethodNotExecutedCount++;
200 this.statistics = new Statistics();
203 public String timeSinceStart() {
204 return statistics.timeSinceStart();
207 public int getMethodCount() {
208 return statistics.getMethodCount();
211 public int getTypeCount() {
212 return statistics.getTypeCount();
215 public int getCompilationUnitCount() {
216 return statistics.getCompilationUnitCount();
219 public int getPackageCount() {
220 return statistics.getPackageCount();
223 public Statistics getSnapshot() {
224 return statistics.generateSnapshot();