Skip to content

Commit fc627e6

Browse files
committed
cleanup - making incremental compilation work with anonymous inner classes, class arg, and method arg
1 parent e743017 commit fc627e6

18 files changed

Lines changed: 350 additions & 197 deletions

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.sandwich.koan.constant.ArgumentType;
1313
import com.sandwich.koan.constant.KoanConstants;
14+
import com.sandwich.util.io.DynamicClassLoader;
1415
import com.sandwich.util.io.FileUtils;
1516

1617
public class CommandLineArgumentBuilder extends LinkedHashMap<ArgumentType, CommandLineArgument> {
@@ -69,13 +70,14 @@ private static void guessAtMethodAndClass(
6970
if(potentialClassOrMethod != null){
7071
if(!hasClass){
7172
commandLineArguments.put(ArgumentType.CLASS_ARG,
72-
new CommandLineArgument(ArgumentType.CLASS_ARG, Class.forName(potentialClassOrMethod).getName()));
73+
new CommandLineArgument(ArgumentType.CLASS_ARG,
74+
new DynamicClassLoader().loadClass(potentialClassOrMethod).getName()));
7375
}else if(!hasMethod){
7476
commandLineArguments.put(ArgumentType.METHOD_ARG,
7577
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));
7678
}
7779
}
78-
}catch(ClassNotFoundException cnfe2){
80+
}catch(Exception cnfe2){
7981
if(!hasMethod){
8082
commandLineArguments.put(ArgumentType.METHOD_ARG,
8183
new CommandLineArgument(ArgumentType.METHOD_ARG, potentialClassOrMethod));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class ClassArg extends AbstractArgumentBehavior {
66

77
public void run(String koanSuiteClassName) {
88
if(koanSuiteClassName != null && koanSuiteClassName.trim().length() != 0){
9-
PathToEnlightenment.stagePathToEnlightenment(koanSuiteClassName);
9+
PathToEnlightenment.filterBySuite(koanSuiteClassName);
1010
}
1111
}
1212

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class MethodArg extends AbstractArgumentBehavior {
66

77
public void run(String koanName) {
88
if(koanName != null && koanName.trim().length() != 0){
9-
PathToEnlightenment.removeAllKoanMethodsExcept(koanName);
9+
PathToEnlightenment.filterByKoan(koanName);
1010
}
1111
}
1212

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

Lines changed: 14 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,155 +2,48 @@
22

33
import java.lang.reflect.Method;
44
import java.util.ArrayList;
5-
import java.util.Arrays;
6-
import java.util.Collections;
75
import java.util.HashMap;
86
import java.util.Iterator;
97
import java.util.LinkedHashMap;
108
import java.util.List;
119
import java.util.Map;
1210
import java.util.Map.Entry;
13-
import java.util.logging.Logger;
1411

1512
import com.sandwich.koan.KoanMethod;
1613
import com.sandwich.koan.constant.KoanConstants;
1714
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer;
18-
import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer.KoanElementAttributes;
1915

2016
public abstract class PathToEnlightenment {
2117

2218
static Path theWay;
19+
static String suiteName;
20+
static String koanMethod;
2321

24-
static Path createPath(){
25-
try{
26-
return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION).transform();
27-
}catch(Throwable t){
28-
throw new RuntimeException(t);
29-
}
30-
}
31-
32-
public static Path getPathToEnlightment(){
33-
if(theWay == null){
34-
theWay = createPath();
35-
}
36-
return theWay;
37-
}
22+
private PathToEnlightenment(){} // non instantiable
3823

39-
public static void removeAllKoanMethodsExcept(String koanName) {
40-
Path koans = getPathToEnlightment();
41-
// if more than 1 pkg, or more than 1 suite - something is likely broken before this point
42-
Map<Object, List<KoanMethod>> lessonsBySuiteMap = koans.iterator().next().getValue();
43-
koanName = koanName.trim();
44-
for(Entry<Object, List<KoanMethod>> methodsBySuite : lessonsBySuiteMap.entrySet()){
45-
KoanMethod keeper = null;
46-
for(KoanMethod method : methodsBySuite.getValue()){
47-
if(koanName.equals(method.getMethod().getName())){
48-
keeper = method;
49-
break;
50-
}
51-
}
52-
if(keeper == null){
53-
// default is to assume method if not a class or recognized arg
54-
// no sense throwing exception, might be user error
55-
System.err.println(koanName+" was not a recognized parameter.");
56-
System.exit(-3);
57-
}
58-
methodsBySuite.setValue(Arrays.asList(keeper));
59-
}
60-
// defer the warning until after potential user error is in console and app is exited
61-
if(koans.size() != 1 || lessonsBySuiteMap.size() != 1){
62-
Logger.getAnonymousLogger().warning("not just one koansuite remains, " +
63-
"check koan suite name argument - not likely that filtering by method will work.");
64-
}
65-
}
66-
67-
static void stagePathToEnlightenment(String pkg, String koanSuite){
68-
try{
69-
Class<?> koanClass;
70-
if(koanSuite.contains(KoanConstants.PERIOD)){
71-
koanClass = Class.forName(koanSuite);
72-
}else{
73-
koanClass = Class.forName(
74-
new StringBuilder(pkg).append(KoanConstants.PERIOD).append(koanSuite).toString());
75-
}
76-
stagePathToEnlightenment(koanClass);
77-
}catch(Throwable t){
78-
throw new RuntimeException(t);
79-
}
80-
}
81-
82-
public static void stagePathToEnlightenment(String koanSuite){
24+
static Path createPath(){
8325
try{
84-
Class<?> koanClass;
85-
if(koanSuite.contains(KoanConstants.PERIOD)){
86-
koanClass = Class.forName(koanSuite);
87-
}else{
88-
throw new RuntimeException("need package to instantiate the class");
89-
}
90-
stagePathToEnlightenment(koanClass);
26+
return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION, suiteName, koanMethod)
27+
.transform();
9128
}catch(Throwable t){
9229
throw new RuntimeException(t);
9330
}
9431
}
9532

96-
static void stagePathToEnlightenment(Class<?> koanSuite) {
97-
try {
98-
stagePathToEnlightenment(koanSuite.newInstance());
99-
} catch (Exception e) {
100-
throw new RuntimeException(e);
101-
}
33+
public static void filterBySuite(String suite){
34+
suiteName = suite;
10235
}
10336

104-
static void stagePathToEnlightenment(Object koanSuite) {
105-
if(koanSuite instanceof Class<?>){
106-
stagePathToEnlightenment((Class<?>)koanSuite);
107-
}
108-
Map<String, Map<Object, List<KoanMethod>>> koans = new HashMap<String, Map<Object,List<KoanMethod>>>();
109-
Map<Object, List<KoanMethod>> suiteAndMethods = new HashMap<Object, List<KoanMethod>>();
110-
List<KoanMethod> methods = XmlToPathTransformer.getKoanMethods(koanSuite.getClass(),
111-
createStubbedLessonMapForSuite(koanSuite.getClass()));
112-
suiteAndMethods.put(koanSuite, methods);
113-
koans.put(koanSuite.toString(), suiteAndMethods);
114-
PathToEnlightenment.theWay = new Path(koans);
37+
public static void filterByKoan(String koan){
38+
koanMethod = koan;
11539
}
11640

117-
private static Map<String, KoanElementAttributes> createStubbedLessonMapForSuite(
118-
Class<? extends Object> clazz) {
119-
Map<String, KoanElementAttributes> partialLessonByKoanMap = new HashMap<String, KoanElementAttributes>();
120-
Map<String, String> lessonsByMethodName = Collections.emptyMap();
121-
try{
122-
Path path = PathToEnlightenment.createPath();
123-
Map<String, Map<Object, List<KoanMethod>>> koanMethodsBySuiteByPackage = path.koanMethodsBySuiteByPackage;
124-
Map<Object, List<KoanMethod>> methodsBySuite = Collections.emptyMap();
125-
for(Entry<String, Map<Object, List<KoanMethod>>> e : koanMethodsBySuiteByPackage.entrySet()){
126-
for(Entry<Object, List<KoanMethod>> e1 : e.getValue().entrySet()){
127-
if(clazz.isInstance(e1.getKey())){
128-
methodsBySuite = e.getValue();
129-
break;
130-
}
131-
}
132-
}
133-
for(Entry<Object, List<KoanMethod>> e : methodsBySuite.entrySet()){
134-
if(clazz.isInstance(e.getKey())){
135-
lessonsByMethodName = new HashMap<String, String>();
136-
for(KoanMethod method : e.getValue()){
137-
lessonsByMethodName.put(method.getMethod().getName(), method.getLesson());
138-
}
139-
break;
140-
}
141-
}
142-
}catch(Throwable x){
143-
Logger.getAnonymousLogger().severe(x.getMessage());
144-
}
145-
for(Method m : clazz.getMethods()){
146-
String methodName = m.getName();
147-
partialLessonByKoanMap.put(methodName, new KoanElementAttributes(
148-
lessonsByMethodName.get(methodName), methodName, "true"));
41+
public static Path getPathToEnlightment(){
42+
if(theWay == null){
43+
theWay = createPath();
14944
}
150-
return partialLessonByKoanMap;
45+
return theWay;
15146
}
152-
153-
private PathToEnlightenment(){} // non instantiable
15447

15548
public static class FileFormatException extends RuntimeException{
15649
private static final long serialVersionUID = -1343169944770684376L;

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,24 @@
2525
import com.sandwich.koan.KoanMethod;
2626
import com.sandwich.koan.path.PathToEnlightenment.Path;
2727
import com.sandwich.util.KoanComparator;
28+
import com.sandwich.util.io.DynamicClassLoader;
2829

2930
public class XmlToPathTransformer {
3031

3132
private final File xmlFile;
33+
private final String suiteName;
34+
private final String methodName;
3235

33-
public XmlToPathTransformer(String xmlFileLocation) throws FileNotFoundException {
36+
public XmlToPathTransformer(){
37+
xmlFile = null;
38+
suiteName = null;
39+
methodName = null;
40+
}
41+
42+
public XmlToPathTransformer(String xmlFileLocation, String suiteName, String methodName) throws FileNotFoundException {
3443
this.xmlFile = new File(xmlFileLocation);
44+
this.suiteName = suiteName;
45+
this.methodName = methodName;
3546
if(!xmlFile.exists()){
3647
throw new FileNotFoundException(xmlFile.getAbsolutePath()
3748
+ " was not found. it may have been deleted, renamed.");
@@ -56,29 +67,33 @@ public Path transform() throws ParserConfigurationException, SAXException,
5667
return new Path(koans);
5768
}
5869

59-
static Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException {
70+
Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException {
6071
Map<Object, List<KoanMethod>> suitesAndKoans = new LinkedHashMap<Object, List<KoanMethod>>();
6172
for(int i = 0; i < childNodes.getLength(); i++){
6273
Node node = childNodes.item(i);
6374
if("suite".equalsIgnoreCase(node.getNodeName())){
64-
Class<?> koanSuiteClass = Class.forName(pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue());
65-
suitesAndKoans.put(
75+
String className = pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue();
76+
if(suiteName == null || suiteName.equalsIgnoreCase(className)){
77+
DynamicClassLoader dynamicClassLoader = new DynamicClassLoader();
78+
Class<?> koanSuiteClass = dynamicClassLoader.loadClass(className);
79+
suitesAndKoans.put(
6680
koanSuiteClass.newInstance(), Collections.unmodifiableList(
6781
createKoanMethods(koanSuiteClass, node.getChildNodes())));
82+
}
6883
}
6984
}
7085
return suitesAndKoans;
7186
}
7287

73-
static List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
88+
List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
7489
Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
7590
List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName);
7691
KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet());
7792
Collections.sort(koanMethods, koanComparator);
7893
return koanMethods;
7994
}
8095

81-
static Map<String, KoanElementAttributes> extractKoansAndRawLessons(
96+
Map<String, KoanElementAttributes> extractKoansAndRawLessons(
8297
String className, NodeList nodes) {
8398
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
8499
for(int i = 0; i < nodes.getLength(); i++){
@@ -109,17 +124,19 @@ public DuplicateKoanException(String className, String name){
109124
}
110125
}
111126

112-
public static List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
127+
public List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
113128
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
114129
for(final Method koan : koanSuiteClass.getMethods()){
115130
final Koan annotation = koan.getAnnotation(Koan.class);
116131
if(annotation != null){
117-
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
118-
koanMethodAttributes = koanMethodAttributes == null ? KoanElementAttributes.NON_EXISTENT : koanMethodAttributes;
119-
String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException;
120-
boolean displayIncompleteKoanException = incompleteKoanException == null ||
121-
"true".equalsIgnoreCase(incompleteKoanException.trim());
122-
koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException));
132+
if(methodName == null || methodName.equalsIgnoreCase(koan.getName())){
133+
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
134+
koanMethodAttributes = koanMethodAttributes == null ? KoanElementAttributes.NON_EXISTENT : koanMethodAttributes;
135+
String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException;
136+
boolean displayIncompleteKoanException = incompleteKoanException == null ||
137+
"true".equalsIgnoreCase(incompleteKoanException.trim());
138+
koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException));
139+
}
123140
}
124141
}
125142
return koanMethods;

‎koans-lib/src/com/sandwich/util/Assert.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sandwich.util;
22

33
import com.sandwich.koan.KoanIncompleteException;
4+
import com.sandwich.koan.constant.KoanConstants;
45

56
public class Assert {
67

@@ -65,7 +66,7 @@ public static void fail(Object o0, Object o1) throws KoanIncompleteException {
6566
}
6667

6768
public static void fail(String msg, Object o0, Object o1){
68-
fail(msg+(msg.length() == 0 ? "" : " ")+EXPECTED+o0+BUT_WAS+o1+END);
69+
fail(msg+(msg.length() == 0 ? "" : KoanConstants.EOL)+EXPECTED+o0+BUT_WAS+o1+END);
6970
}
7071

7172
public static void fail(String msg){

0 commit comments

Comments
 (0)