package tools.ic;

import java.util.*;
import java.lang.reflect.*;
import java.net.URLClassLoader;
import java.io.*;

public class InterfaceComparer {
	private static void compareClasses(Class cleanroomClass, Class studentClass) {

		boolean equals = false;
		ArrayList<Method> studentMethods = new ArrayList<Method>(Arrays.asList(studentClass.getDeclaredMethods()));
		// checkMethods
		for(Method cleanroomMethod : cleanroomClass.getDeclaredMethods()){
			for(Method studentMethod : studentMethods) {
				if(cleanroomMethod.equals(studentMethod)) {
					equals = true;
					studentMethods.remove(studentMethod);
					break;
				}

				if(equals){
					equals = false;
				}else{
					System.err.println("WARNING - Method " +cleanroomMethod.getName() + " does not exists in student code or does not match with student counterpart");
				}
			}	
		}		
	}

	private static String getSimpleFileName(String path){
		int idx = path.lastIndexOf("/");
		return idx >= 0 ? path.substring(idx + 1) : path;
	}


	public static void main(String args[]){
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		String pathToCleanroom = "./cleanroom";
		File f = new File(pathToCleanroom);

		for(File path : f.listFiles()) {
			if (path.isFile()) {
				String pathString = path.toString();
				if(pathString.endsWith(".class")){
					// get simple Name
					String fileName = getSimpleFileName(pathString);
					// strip fileextension
					fileName = fileName.substring(0, fileName.lastIndexOf('.'));	
					Class cleanroomClass = null;
					Class studentClass = null;

					try{
						cleanroomClass = cl.loadClass(fileName);
						studentClass = cl.loadClass(fileName);

					} catch (ClassNotFoundException cnfe) {
						throw new Error("WARNING class [" + cnfe.getMessage()+"] not found");
					}
					compareClasses(cleanroomClass,studentClass);
				}
			}
		}
	}
}

@@ -92,16 +92,76 @@
 Loader;%0A
+import java.net.URL;%0Aimport java.net.MalformedURLException;%0A
 import j
@@ -1103,138 +1103,407 @@
 %7B%0A%09%09
-ClassLoader cl = ClassLoader.getSystemClassLoader();%0A%09%09String pathToCleanroom = %22./cleanroom%22;%0A%09%09File f = new File(pathToCleanroom
+String pathToCleanroom = %22./cleanroom%22;%0A%09%09URL%5B%5D cleanroomSearchUrls = null;%0A%09%09try %7B%0A%09%09%09cleanroomSearchUrls = new URL%5B%5D%7B new URL(pathToCleanroom) %7D;%0A%09%09%0A%09%09%7D catch (MalformedURLException mue) %7B%0A%09%09%09throw new Error(%22WARNING - %22 + mue.getMessage());%0A%09%09%7D%0A%0A%09%09File f = new File(pathToCleanroom);%0A%09%09ClassLoader cl = ClassLoader.getSystemClassLoader();%0A%09%09ClassLoader ul = new URLClassLoader(cleanroomSearchUrls
 );%0A%0A
@@ -1820,24 +1820,27 @@
 %09%0A%09%09%09%09%09Class
+%3C?%3E
  cleanroomCl
@@ -1857,24 +1857,27 @@
 ;%0A%09%09%09%09%09Class
+%3C?%3E
  studentClas
@@ -1912,33 +1912,33 @@
 leanroomClass = 
-c
+u
 l.loadClass(file

load clearoom classes, not student, #12