package tools.ic;

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

public class InterfaceComparer {
	private static boolean error = false;
	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()){
			System.out.println("[cleanroom]" + cleanroomMethod);
			for(Method studentMethod : studentMethods) {
				System.out.println("[student]" + studentMethod);
				if(cleanroomMethod.toString().equals(studentMethod.toString())) {
					equals = true;
					studentMethods.remove(studentMethod);
					break;
				}
			}	

			if(equals){
				equals = false;
			}else{
				error = true;
				System.err.println("WARNING - Method " +cleanroomMethod + " 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[]){
		String pathToCleanroom = "/cleanroom/";
		File f = new File("./"+pathToCleanroom);
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		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);

						// clean 
						Runtime rt = Runtime.getRuntime();
						String cmd = "make clean-cleanroom";
						Process pr = rt.exec(cmd);
						pr.waitFor();

						studentClass = cl.loadClass(fileName);

					} catch (ClassNotFoundException cnfe) {
						throw new Error("Error - class [" + cnfe.getMessage()+"] not found");
					} catch(IOException ioe) {
						throw new Error("Error - cleaning cleanroom failed");
					} catch(InterruptedException ie) {
						throw new Error("Error - cleaning cleanroom failed");
					}
					compareClasses(cleanroomClass,studentClass);
				}
			}
	
		}

		if(error){
			throw new Error();
		}
	}
}

@@ -1303,16 +1303,17 @@
 room = %22
+.
 /cleanro
@@ -1342,13 +1342,8 @@
 ile(
-%22./%22+
 path
@@ -1411,16 +1411,411 @@
 ader();%0A
+%09%09String cwd = System.getProperty(%22user.dir%22);%0A%09%09File cleanroomJAR = new File(cwd+%22/cleanroom/%22);%0A%09%09ClassLoader cleanroomLoader = null;%0A%09%09ClassLoader studentLoader = null;%0A%09%09try%7B%0A%09%09%09cleanroomLoader = new URLClassLoader(new URL%5B%5D%7BcleanroomJAR.toURI().toURL()%7D);%0A%09%09%09studentLoader = new URLClassLoader(new URL%5B%5D%7Bnew File(cwd).toURI().toURL()%7D);%0A%09%09%7Dcatch(MalformedURLException mfue)%7B%0A%09%09%09// TODO%0A%09%09%7D%0A
 %09%09for(Fi
@@ -2218,32 +2218,45 @@
 anroomClass = cl
+eanroomLoader
 .loadClass(fileN
@@ -2265,517 +2265,181 @@
 e);%0A
-%0A
 %09%09%09%09%09%09
-// clean %0A%09%09%09%09%09%09Runtime rt = Runtime.getRuntime();%0A%09%09%09%09%09%09String cmd = %22make clean-cleanroom%22;%0A%09%09%09%09%09%09Process pr = rt.exec(cmd);%0A%09%09%09%09%09%09pr.waitFor();%0A%0A%09%09%09%09%09%09studentClass = cl.loadClass(fileName);%0A%0A%09%09%09%09%09%7D catch (ClassNotFoundException cnfe) %7B%0A%09%09%09%09%09%09throw new Error(%22Error - class %5B%22 + cnfe.getMessage()+%22%5D not found%22);%0A%09%09%09%09%09%7D catch(IOException ioe) %7B%0A%09%09%09%09%09%09throw new Error(%22Error - cleaning cleanroom failed%22);%0A%09%09%09%09%09%7D catch(InterruptedException ie) %7B%0A%09%09%09%09%09%09throw new Error(%22Error - cleaning cleanroom faile
+studentClass = studentLoader.loadClass(fileName);%0A%0A%09%09%09%09%09%7D catch (ClassNotFoundException cnfe) %7B%0A%09%09%09%09%09%09throw new Error(%22Error - class %5B%22 + cnfe.getMessage()+%22%5D not foun
 d%22);

load two classes with same name without using subprocess, #12