Skip to content

Commit e433e5a

Browse files
committed
defer compilation until running the koan
1 parent d121452 commit e433e5a

21 files changed

Lines changed: 366 additions & 254 deletions

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@
22

33
import java.lang.reflect.Method;
44

5+
import com.sandwich.koan.path.xmltransformation.KoanElementAttributes;
56
import com.sandwich.koan.path.xmltransformation.XmlVariableInjector;
7+
import com.sandwich.util.io.DynamicClassLoader;
68

79
public class KoanMethod {
810

911
private final transient Method method;
1012
private final String lesson;
1113
private final boolean displayIncompleteException;
14+
private static final DynamicClassLoader classLoader = new DynamicClassLoader();
1215

13-
public KoanMethod(String lesson, Method method){
14-
this(lesson,method,true);
16+
private KoanMethod(KoanElementAttributes koanAttributes) throws SecurityException, NoSuchMethodException{
17+
this( koanAttributes.lesson,
18+
classLoader.loadClass(koanAttributes.className).getMethod(koanAttributes.name),
19+
!"false".equalsIgnoreCase(koanAttributes.displayIncompleteKoanException));
1520
}
1621

17-
public KoanMethod(String lesson, Method method, boolean displayIncompleteException){
22+
public static KoanMethod getInstance(KoanElementAttributes koanAttributes){
23+
try {
24+
return new KoanMethod(koanAttributes);
25+
} catch (Exception e) {
26+
throw new RuntimeException(e);
27+
}
28+
}
29+
30+
public static KoanMethod getInstance(String lesson, Method method){
31+
return new KoanMethod(lesson, method, true);
32+
}
33+
34+
private KoanMethod(String lesson, Method method, boolean displayIncompleteException){
1835
if(method == null){
1936
throw new IllegalArgumentException("method may not be null");
2037
}

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.sandwich.koan.cmdline;
22

3-
import static com.sandwich.koan.constant.KoanConstants.DATA_FOLDER;
4-
import static com.sandwich.koan.constant.KoanConstants.FILESYSTEM_SEPARATOR;
5-
import static com.sandwich.koan.constant.KoanConstants.PROJ_MAIN_FOLDER;
6-
7-
import java.io.File;
83
import java.util.LinkedHashMap;
94
import java.util.Map;
105
import java.util.Map.Entry;
116

127
import com.sandwich.koan.constant.ArgumentType;
138
import com.sandwich.koan.constant.KoanConstants;
9+
import com.sandwich.koan.util.ApplicationUtils;
10+
import com.sandwich.util.ConsoleUtils;
1411
import com.sandwich.util.io.DynamicClassLoader;
15-
import com.sandwich.util.io.FileUtils;
1612

1713
public class CommandLineArgumentBuilder extends LinkedHashMap<ArgumentType, CommandLineArgument> {
1814

@@ -87,20 +83,11 @@ private static void guessAtMethodAndClass(
8783
}
8884
}
8985
}
90-
91-
static private boolean isFirstTimeAppHasBeenRun() {
92-
File dataDirectory = new File(FileUtils.makeAbsoluteRelativeTo(
93-
new StringBuilder(PROJ_MAIN_FOLDER).append(FILESYSTEM_SEPARATOR).append(DATA_FOLDER).toString()));
94-
return !dataDirectory.exists();
95-
}
9686

9787
void applyAssumedStartupBehaviors() {
98-
if(isFirstTimeAppHasBeenRun()){
88+
if(ApplicationUtils.isFirstTimeAppHasBeenRun()){
9989
ArgumentType.BACKUP.run(null);
100-
// really hackish - but this will clear console for first run, not necessary after
101-
for(int i = 0; i < 80; i++){
102-
System.out.println();
103-
}
90+
ConsoleUtils.clearConsole();
10491
}
10592
if(isEmpty() || !containsKey(ArgumentType.RUN_KOANS) && containsKey(ArgumentType.CLASS_ARG)){
10693
if(KoanConstants.DEBUG){

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

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
import java.io.FileNotFoundException;
44
import java.lang.reflect.Method;
5-
import java.util.ArrayList;
6-
import java.util.HashMap;
75
import java.util.Iterator;
8-
import java.util.LinkedHashMap;
9-
import java.util.List;
106
import java.util.Map;
117
import java.util.Map.Entry;
128

139
import com.sandwich.koan.Koan;
14-
import com.sandwich.koan.KoanMethod;
1510
import com.sandwich.koan.constant.KoanConstants;
11+
import com.sandwich.koan.path.xmltransformation.KoanElementAttributes;
1612
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer;
1713
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformerImpl;
14+
import com.sandwich.util.io.DynamicClassLoader;
1815

1916
public abstract class PathToEnlightenment {
2017

@@ -66,53 +63,59 @@ public FileFormatException(String message){
6663
}
6764
}
6865

69-
public static class Path implements Iterable<Entry<String, Map<Object, List<KoanMethod>>>>{
70-
final Map<String, Map<Object, List<KoanMethod>>> koanMethodsBySuiteByPackage;
71-
public Path(Map<String, Map<Object, List<KoanMethod>>> koanMethodsBySuiteByPackage){
72-
this.koanMethodsBySuiteByPackage = koanMethodsBySuiteByPackage;
73-
}
66+
public static class Path implements Iterable<Entry<String, Map<String, Map<String, KoanElementAttributes>>>>{
67+
private Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage;
68+
public Path(){}
7469
public int getTotalNumberOfKoans() {
7570
int total = 0;
76-
for(Entry<String, Map<Object, List<KoanMethod>>> e0 : koanMethodsBySuiteByPackage.entrySet()){
77-
for(Entry<Object, List<KoanMethod>> e1 : e0.getValue().entrySet()){
78-
total += e1.getValue().size();
71+
Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> koanMethodsBySuiteByPackageIter =
72+
getKoanMethodsBySuiteByPackage();
73+
DynamicClassLoader loader = new DynamicClassLoader();
74+
while(koanMethodsBySuiteByPackageIter.hasNext()){
75+
Entry<String, Map<String, Map<String, KoanElementAttributes>>> e = koanMethodsBySuiteByPackageIter.next();
76+
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+
}
7983
}
8084
}
8185
return total;
8286
}
83-
public Iterator<Entry<String, Map<Object, List<KoanMethod>>>> iterator() {
84-
return koanMethodsBySuiteByPackage.entrySet().iterator();
87+
public Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> iterator() {
88+
return getKoanMethodsBySuiteByPackage();
8589
}
86-
public int size() {
87-
return koanMethodsBySuiteByPackage.size();
90+
public Path stubKoanMethodsBySuiteByClass(Map<String, Map<String, Map<String, KoanElementAttributes>>> koanMethodsBySuiteByPackage){
91+
// unlike other collections in this app, this actually needs to remain mutable after the reference is
92+
// stored and utilized internally. this is so the DynamicClassLoader can swap out references to
93+
// any dynamic classes
94+
this.koanMethodsBySuiteByPackage = koanMethodsBySuiteByPackage;
95+
return this;
8896
}
89-
public int size(String pkg){
90-
Map<?,?> suiteAndKoans = koanMethodsBySuiteByPackage.get(pkg);
91-
if(suiteAndKoans == null){
92-
return -1;
93-
}
94-
return suiteAndKoans.size();
97+
protected Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> getKoanMethodsBySuiteByPackage() {
98+
return koanMethodsBySuiteByPackage.entrySet().iterator();
9599
}
96100
@Override public boolean equals(Object o){
97101
if(o == this){
98102
return true;
99103
}
100104
if(o instanceof Path){
101-
if(koanMethodsBySuiteByPackage == ((Path)o).koanMethodsBySuiteByPackage){
105+
if(getKoanMethodsBySuiteByPackage() == ((Path)o).getKoanMethodsBySuiteByPackage()){
102106
return true;
103107
}
104-
if(koanMethodsBySuiteByPackage == null || ((Path)o).koanMethodsBySuiteByPackage == null
105-
|| koanMethodsBySuiteByPackage.size() != ((Path)o).koanMethodsBySuiteByPackage.size()
106-
|| koanMethodsBySuiteByPackage.getClass() != ((Path)o).koanMethodsBySuiteByPackage.getClass()){
108+
if(getKoanMethodsBySuiteByPackage() == null || ((Path)o).getKoanMethodsBySuiteByPackage() == null
109+
|| getKoanMethodsBySuiteByPackage().getClass() != ((Path)o).getKoanMethodsBySuiteByPackage().getClass()){
107110
return false;
108111
}
109-
Iterator<Entry<String,Map<Object, List<KoanMethod>>>> i1 =
110-
koanMethodsBySuiteByPackage.entrySet().iterator();
111-
Iterator<Entry<String,Map<Object, List<KoanMethod>>>> i2 =
112-
((Path)o).koanMethodsBySuiteByPackage.entrySet().iterator();
112+
Iterator<Entry<String,Map<String, Map<String, KoanElementAttributes>>>> i1 =
113+
getKoanMethodsBySuiteByPackage();
114+
Iterator<Entry<String, Map<String, Map<String, KoanElementAttributes>>>> i2 =
115+
((Path)o).getKoanMethodsBySuiteByPackage();
113116
while(i1.hasNext()){
114-
Map<Object, List<KoanMethod>> m1 = i1.next().getValue();
115-
Map<Object, List<KoanMethod>> m2 = i2.next().getValue();
117+
Map<String, Map<String, KoanElementAttributes>> m1 = i1.next().getValue();
118+
Map<String, Map<String, KoanElementAttributes>> m2 = i2.next().getValue();
116119
if(m1 == m2){
117120
continue;
118121
}
@@ -122,11 +125,11 @@ public int size(String pkg){
122125
|| m1.getClass() != m2.getClass()){
123126
return false;
124127
}
125-
Iterator<Entry<Object, List<KoanMethod>>> ii1 = m1.entrySet().iterator();
126-
Iterator<Entry<Object, List<KoanMethod>>> ii2 = m2.entrySet().iterator();
128+
Iterator<Entry<String, Map<String, KoanElementAttributes>>> ii1 = m1.entrySet().iterator();
129+
Iterator<Entry<String, Map<String, KoanElementAttributes>>> ii2 = m2.entrySet().iterator();
127130
while(ii1.hasNext()){
128-
Entry<Object, List<KoanMethod>> e1 = ii1.next();
129-
Entry<Object, List<KoanMethod>> e2 = ii2.next();
131+
Entry<String, Map<String, KoanElementAttributes>> e1 = ii1.next();
132+
Entry<String, Map<String, KoanElementAttributes>> e2 = ii2.next();
130133
if(!e1.getKey().getClass().equals(e2.getKey().getClass())){
131134
return false;
132135
}
@@ -139,55 +142,33 @@ public int size(String pkg){
139142
return true;
140143
}
141144
@Override public int hashCode(){
142-
return koanMethodsBySuiteByPackage.hashCode();
145+
return getKoanMethodsBySuiteByPackage().hashCode();
143146
}
144147
@Override public String toString(){
145-
return koanMethodsBySuiteByPackage.toString();
148+
return getKoanMethodsBySuiteByPackage().toString();
146149
}
147150
}
148151

149152
public static void replace(Class<?> cls) {
150-
for(Entry<String,Map<Object, List<KoanMethod>>> e : getPathToEnlightment().koanMethodsBySuiteByPackage.entrySet()){
151-
Map<Object, List<KoanMethod>> methodsBySuite = e.getValue();
152-
Map<Object, List<KoanMethod>> replacementValue = new LinkedHashMap<Object, List<KoanMethod>>();
153-
for(Entry<Object, List<KoanMethod>> e1 : methodsBySuite.entrySet()){
154-
Object suite = e1.getKey();
155-
if(suite.getClass().getName().equals(cls.getName())){
156-
try {
157-
replacementValue.put(cls.newInstance(), copyMethods(cls, e1.getValue()));
158-
} catch (Exception e2) {
159-
e2.printStackTrace();
160-
}
161-
}else{
162-
replacementValue.put(suite, e1.getValue());
163-
}
164-
}
165-
e.setValue(replacementValue);
166-
}
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);
167171
}
168172

169-
private static List<KoanMethod> copyMethods(Class<?> cls, List<KoanMethod> methodsBySuite) {
170-
List<KoanMethod> newMethods = new ArrayList<KoanMethod>();
171-
try {
172-
Map<String, Method> methodByName = new HashMap<String, Method>();
173-
for(Method method : cls.getDeclaredMethods()){
174-
methodByName.put(method.getName(), method);
175-
KoanMethod equivalentKoanMethod = null;
176-
for(KoanMethod koanMethod : methodsBySuite){
177-
if(koanMethod.getMethod().getName().equals(method.getName())){
178-
equivalentKoanMethod = koanMethod;
179-
break;
180-
}
181-
}
182-
if(equivalentKoanMethod != null && method.getAnnotation(Koan.class) != null){
183-
newMethods.add(equivalentKoanMethod.clone(method));
184-
}else if(method.getAnnotation(Koan.class) != null){
185-
newMethods.add(new KoanMethod("", method));
186-
}
187-
}
188-
} catch (Exception e2) {
189-
throw new RuntimeException(e2);
190-
}
191-
return newMethods;
192-
}
173+
193174
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.sandwich.koan.path.xmltransformation;
2+
3+
4+
public class KoanElementAttributes{
5+
6+
public String lesson, name, displayIncompleteKoanException, className;
7+
8+
public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException, String className){
9+
this.lesson = lesson;
10+
this.name = name;
11+
this.displayIncompleteKoanException = displayIncompleteKoanException;
12+
this.className = className;
13+
}
14+
15+
@Override
16+
public int hashCode() {
17+
final int prime = 31;
18+
int result = 1;
19+
result = prime * result
20+
+ ((className == null) ? 0 : className.hashCode());
21+
result = prime
22+
* result
23+
+ ((displayIncompleteKoanException == null) ? 0
24+
: displayIncompleteKoanException.hashCode());
25+
result = prime * result + ((lesson == null) ? 0 : lesson.hashCode());
26+
result = prime * result + ((name == null) ? 0 : name.hashCode());
27+
return result;
28+
}
29+
30+
@Override
31+
public boolean equals(Object obj) {
32+
if (this == obj)
33+
return true;
34+
if (obj == null)
35+
return false;
36+
if (getClass() != obj.getClass())
37+
return false;
38+
KoanElementAttributes other = (KoanElementAttributes) obj;
39+
if (className == null) {
40+
if (other.className != null)
41+
return false;
42+
} else if (!className.equals(other.className))
43+
return false;
44+
if (displayIncompleteKoanException == null) {
45+
if (other.displayIncompleteKoanException != null)
46+
return false;
47+
} else if (!displayIncompleteKoanException
48+
.equals(other.displayIncompleteKoanException))
49+
return false;
50+
if (lesson == null) {
51+
if (other.lesson != null)
52+
return false;
53+
} else if (!lesson.equals(other.lesson))
54+
return false;
55+
if (name == null) {
56+
if (other.name != null)
57+
return false;
58+
} else if (!name.equals(other.name))
59+
return false;
60+
return true;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "KoanElementAttributes [lesson=" + lesson + ", name=" + name
66+
+ ", displayIncompleteKoanException="
67+
+ displayIncompleteKoanException + ", className=" + className
68+
+ "]";
69+
}
70+
71+
}

0 commit comments

Comments
 (0)