Skip to content

Commit b30b520

Browse files
committed
refactored some nasty koan running code, deferred compilation until
running a suite
1 parent e433e5a commit b30b520

30 files changed

Lines changed: 374 additions & 507 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.class
22
bin/*
3+
.metadata
34
*/bin/*
45
koans/data
56
koans/data*

‎koans-lib/src/com/sandwich/koan/KoanMethod.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public static KoanMethod getInstance(KoanElementAttributes koanAttributes){
2727
}
2828
}
2929

30+
public static KoanMethod getInstance(Method method){
31+
return new KoanMethod(null, method, true);
32+
}
33+
3034
public static KoanMethod getInstance(String lesson, Method method){
3135
return new KoanMethod(lesson, method, true);
3236
}

‎koans-lib/src/com/sandwich/koan/KoanSuiteResult.java‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

‎koans-lib/src/com/sandwich/koan/cmdline/CommandLineArgumentBuilder.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ void applyAssumedStartupBehaviors() {
8989
ArgumentType.BACKUP.run(null);
9090
ConsoleUtils.clearConsole();
9191
}
92-
if(isEmpty() || !containsKey(ArgumentType.RUN_KOANS) && containsKey(ArgumentType.CLASS_ARG)){
92+
if(isEmpty() || !containsKey(ArgumentType.RUN_KOANS) && (
93+
containsKey(ArgumentType.CLASS_ARG) ||
94+
containsKey(ArgumentType.DEBUG))){
9395
if(KoanConstants.DEBUG){
9496
System.out.println("Planting default run target.");
9597
for(Entry<ArgumentType, CommandLineArgument> argEntry : entrySet()){

‎koans-lib/src/com/sandwich/koan/constant/ArgumentType.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.sandwich.koan.cmdline.behavior.Help;
1818
import com.sandwich.koan.cmdline.behavior.MethodArg;
1919
import com.sandwich.koan.cmdline.behavior.Reset;
20-
import com.sandwich.koan.cmdline.behavior.Test;
2120
import com.sandwich.koan.runner.RunKoans;
2221
import com.sandwich.util.Strings;
2322

@@ -28,7 +27,7 @@ public enum ArgumentType implements ArgumentBehavior {
2827
RESET( Reset.class),
2928
BACKUP( Backup.class),
3029
DEBUG( Debug.class),
31-
TEST( Test.class),
30+
//TEST( Test.class),
3231
// important class MUST come before method - due to how Enum implements comparable and order
3332
// dependent logic later @ see ArgumentTypeTest.testClassPrecedesMethod
3433
CLASS_ARG( ClassArg.class),

‎koans-lib/src/com/sandwich/koan/path/PathToEnlightenment.java‎

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sandwich.koan.path;
22

33
import java.io.FileNotFoundException;
4-
import java.lang.reflect.Method;
54
import java.util.Iterator;
65
import java.util.Map;
76
import java.util.Map.Entry;
@@ -11,7 +10,8 @@
1110
import com.sandwich.koan.path.xmltransformation.KoanElementAttributes;
1211
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer;
1312
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;
1515

1616
public abstract class PathToEnlightenment {
1717

@@ -67,26 +67,34 @@ public static class Path implements Iterable<Entry<String, Map<String, Map<Strin
6767
private Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage;
6868
public Path(){}
6969
public int getTotalNumberOfKoans() {
70-
int total = 0;
70+
Counter total = new Counter();
7171
Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> koanMethodsBySuiteByPackageIter =
7272
getKoanMethodsBySuiteByPackage();
73-
DynamicClassLoader loader = new DynamicClassLoader();
7473
while(koanMethodsBySuiteByPackageIter.hasNext()){
7574
Entry<String, Map<String, Map<String, KoanElementAttributes>>> e = koanMethodsBySuiteByPackageIter.next();
7675
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();
8390
}
8491
}
85-
return total;
8692
}
93+
8794
public Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> iterator() {
8895
return getKoanMethodsBySuiteByPackage();
8996
}
97+
9098
public Path stubKoanMethodsBySuiteByClass(Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage){
9199
// unlike other collections in this app, this actually needs to remain mutable after the reference is
92100
// 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>>
148156
return getKoanMethodsBySuiteByPackage().toString();
149157
}
150158
}
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-
174159
}

‎koans-lib/src/com/sandwich/koan/path/xmltransformation/XmlToPathTransformerImpl.java‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
5-
import java.lang.reflect.Method;
6-
import java.util.ArrayList;
75
import java.util.LinkedHashMap;
8-
import java.util.List;
96
import java.util.Map;
107

118
import javax.xml.parsers.DocumentBuilder;
@@ -17,10 +14,7 @@
1714
import org.w3c.dom.Node;
1815
import org.w3c.dom.NodeList;
1916

20-
import com.sandwich.koan.Koan;
21-
import com.sandwich.koan.KoanMethod;
2217
import com.sandwich.koan.path.PathToEnlightenment.Path;
23-
import com.sandwich.util.io.DynamicClassLoader;
2418

2519
public class XmlToPathTransformerImpl implements XmlToPathTransformer {
2620

@@ -92,17 +86,6 @@ Map<String, Map<String, KoanElementAttributes>> getKoanElementAttributesByMethod
9286
return koansByMethodNameByClass;
9387
}
9488

95-
// List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
96-
// Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(
97-
// koanSuiteClass.getName(), nodes);
98-
// List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass,
99-
// rawKoanLessonByMethodName);
100-
// KoanComparator koanComparator = new KoanComparator(
101-
// rawKoanLessonByMethodName.keySet());
102-
// Collections.sort(koanMethods, koanComparator);
103-
// return koanMethods;
104-
// }
105-
10689
Map<String, KoanElementAttributes> extractKoansAndRawLessons(
10790
String className, NodeList nodes) {
10891
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
@@ -129,29 +112,12 @@ Map<String, KoanElementAttributes> extractKoansAndRawLessons(
129112
return rawKoanAttributesByMethodName;
130113
}
131114

132-
static class DuplicateKoanException extends RuntimeException {
133-
private static final long serialVersionUID = 3846796580641690961L;
134-
public DuplicateKoanException(String className, String name){
135-
super("Duplicate koans in the same suite: "+className+" with the name "+name);
136-
}
137-
}
138-
139-
public List<KoanMethod> getKoanMethods(String koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
140-
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
141-
DynamicClassLoader loader = new DynamicClassLoader();
142-
for(final Method koan : loader.loadClass(koanSuiteClass).getMethods()){
143-
final Koan annotation = koan.getAnnotation(Koan.class);
144-
if(annotation != null){
145-
if(methodName == null || methodName.equalsIgnoreCase(koan.getName())){
146-
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
147-
koanMethodAttributes = koanMethodAttributes == null ?
148-
new KoanElementAttributes(null, koan.getName(), null, koanSuiteClass) : koanMethodAttributes;
149-
koanMethods.add(KoanMethod.getInstance(koanMethodAttributes));
150-
}
151-
}
152-
}
153-
return koanMethods;
115+
static class DuplicateKoanException extends RuntimeException {
116+
private static final long serialVersionUID = 3846796580641690961L;
117+
public DuplicateKoanException(String className, String name){
118+
super("Duplicate koans in the same suite: "+className+" with the name "+name);
154119
}
120+
}
155121

156122
}
157123

‎koans-lib/src/com/sandwich/koan/runner/AppLauncher.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import com.sandwich.koan.cmdline.CommandLineArgumentRunner;
56
import com.sandwich.koan.cmdline.CommandLineArgument;
67
import com.sandwich.koan.cmdline.CommandLineArgumentBuilder;
78
import com.sandwich.koan.constant.ArgumentType;
@@ -14,7 +15,7 @@ public class AppLauncher {
1415

1516
public static void main(final String... args) throws Throwable {
1617
Map<ArgumentType, CommandLineArgument> argsMap = new CommandLineArgumentBuilder(args);
17-
new KoanSuiteRunner(argsMap).run();
18+
new CommandLineArgumentRunner(argsMap).run();
1819
if(KoanConstants.DEBUG){
1920
StringBuilder argsBuilder = new StringBuilder();
2021
int argNumber = 0;

0 commit comments

Comments
 (0)