|
1 | 1 | package com.sandwich.koan.path; |
2 | 2 |
|
3 | 3 | import java.io.FileNotFoundException; |
4 | | -import java.lang.reflect.Method; |
5 | 4 | import java.util.Iterator; |
6 | 5 | import java.util.Map; |
7 | 6 | import java.util.Map.Entry; |
|
11 | 10 | import com.sandwich.koan.path.xmltransformation.KoanElementAttributes; |
12 | 11 | import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer; |
13 | 12 | import com.sandwich.koan.path.xmltransformation.XmlToPathTransformerImpl; |
14 | | -import com.sandwich.util.io.DynamicClassLoader; |
| 13 | +import com.sandwich.util.Counter; |
| 14 | +import com.sandwich.util.io.FileUtils; |
15 | 15 |
|
16 | 16 | public abstract class PathToEnlightenment { |
17 | 17 |
|
@@ -67,26 +67,34 @@ public static class Path implements Iterable<Entry<String, Map<String, Map<Strin |
67 | 67 | private Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage; |
68 | 68 | public Path(){} |
69 | 69 | public int getTotalNumberOfKoans() { |
70 | | - int total = 0; |
| 70 | + Counter total = new Counter(); |
71 | 71 | Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> koanMethodsBySuiteByPackageIter = |
72 | 72 | getKoanMethodsBySuiteByPackage(); |
73 | | - DynamicClassLoader loader = new DynamicClassLoader(); |
74 | 73 | while(koanMethodsBySuiteByPackageIter.hasNext()){ |
75 | 74 | Entry<String, Map<String, Map<String, KoanElementAttributes>>> e = koanMethodsBySuiteByPackageIter.next(); |
76 | 75 | for(Entry<String, Map<String, KoanElementAttributes>> e1 : e.getValue().entrySet()){ |
77 | | - Class<?> c = loader.loadClass(e1.getKey()); |
78 | | - for(Method m : c.getMethods()){ |
79 | | - if(m.getAnnotation(Koan.class) != null){ |
80 | | - total++; |
81 | | - } |
82 | | - } |
| 76 | + countKoanAnnotationsInJavaFileGivenClassName(e1.getKey(), total); |
| 77 | + } |
| 78 | + } |
| 79 | + return (int)total.getCount(); |
| 80 | + } |
| 81 | + |
| 82 | + private void countKoanAnnotationsInJavaFileGivenClassName(String className, Counter total) { |
| 83 | + String[] lines = FileUtils.getContentsOfOriginalJavaFile(className).split(KoanConstants.EOLS); |
| 84 | + String koanClassSimpleNameWithAnnotationPrefix = '@'+Koan.class.getSimpleName(); |
| 85 | + for(String line : lines){ |
| 86 | + String trimmedLine = line.trim(); |
| 87 | + if(trimmedLine.contains(koanClassSimpleNameWithAnnotationPrefix) |
| 88 | + && !trimmedLine.startsWith("//") && !trimmedLine.startsWith("*") && !trimmedLine.startsWith("/*")){ |
| 89 | + total.count(); |
83 | 90 | } |
84 | 91 | } |
85 | | - return total; |
86 | 92 | } |
| 93 | + |
87 | 94 | public Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> iterator() { |
88 | 95 | return getKoanMethodsBySuiteByPackage(); |
89 | 96 | } |
| 97 | + |
90 | 98 | public Path stubKoanMethodsBySuiteByClass(Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage){ |
91 | 99 | // unlike other collections in this app, this actually needs to remain mutable after the reference is |
92 | 100 | // stored and utilized internally. this is so the DynamicClassLoader can swap out references to |
@@ -148,27 +156,4 @@ protected Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>> |
148 | 156 | return getKoanMethodsBySuiteByPackage().toString(); |
149 | 157 | } |
150 | 158 | } |
151 | | - |
152 | | - public static void replace(Class<?> cls) { |
153 | | -// for(Entry<String, Map<Object, List<KoanMethod>>> e : getPathToEnlightment().koanMethodsBySuiteByPackage.entrySet()){ |
154 | | -// Map<Object, List<KoanMethod>> methodsBySuite = e.getValue(); |
155 | | -// Map<Object, List<KoanMethod>> replacementValue = new LinkedHashMap<Object, List<KoanMethod>>(); |
156 | | -// for(Entry<Object, List<KoanMethod>> e1 : methodsBySuite.entrySet()){ |
157 | | -// Object suite = e1.getKey(); |
158 | | -// if(suite.getClass().getName().equals(cls.getName())){ |
159 | | -// try { |
160 | | -// replacementValue.put(cls.newInstance(), copyMethods(cls, e1.getValue())); |
161 | | -// } catch (Exception e2) { |
162 | | -// e2.printStackTrace(); |
163 | | -// } |
164 | | -// }else{ |
165 | | -// replacementValue.put(suite, e1.getValue()); |
166 | | -// } |
167 | | -// } |
168 | | -// e.setValue(replacementValue); |
169 | | -// } |
170 | | - System.out.println("UNIQ-replace hit: "+cls); |
171 | | - } |
172 | | - |
173 | | - |
174 | 159 | } |
0 commit comments