22
33import java .io .FileNotFoundException ;
44import java .lang .reflect .Method ;
5- import java .util .ArrayList ;
6- import java .util .HashMap ;
75import java .util .Iterator ;
8- import java .util .LinkedHashMap ;
9- import java .util .List ;
106import java .util .Map ;
117import java .util .Map .Entry ;
128
139import com .sandwich .koan .Koan ;
14- import com .sandwich .koan .KoanMethod ;
1510import com .sandwich .koan .constant .KoanConstants ;
11+ import com .sandwich .koan .path .xmltransformation .KoanElementAttributes ;
1612import com .sandwich .koan .path .xmltransformation .XmlToPathTransformer ;
1713import com .sandwich .koan .path .xmltransformation .XmlToPathTransformerImpl ;
14+ import com .sandwich .util .io .DynamicClassLoader ;
1815
1916public 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}
0 commit comments