package agaricus.MethodComparator;

import org.apache.commons.io.FilenameUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.io.IOException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

//import org.objectweb.asm.*;

public class MethodComparator
{
    /**
     * Read classes in a jar file into ASM ClassNodes
     * @param jarFilename Name of jar to read
     * @param packagePrefix Prefix to match for classes to read (all others ignored)
     * @return Map of class name String to ClassNode
     */
    public static Map<String,ClassNode> getClassNodes(String jarFilename, String packagePrefix) throws IOException
    {
        JarFile jarFile = new JarFile(jarFilename);
        Enumeration<JarEntry> entries = jarFile.entries();

        Map<String,ClassNode> classes = new HashMap<String,ClassNode>();

        while(entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String path = entry.getName();

            if (!path.startsWith(packagePrefix) || !path.endsWith(".class")) {
                continue;
            }

            String className = FilenameUtils.getBaseName(path);
            System.out.println(className);

            ClassReader classReader = new ClassReader(jarFile.getInputStream(entry));
            ClassNode classNode = new ClassNode();

            classReader.accept(classNode, 0);

            classes.put(className, classNode);
        }

        return classes;
    }
    public static void main(String args[]) throws IOException
    {
        Map<String,ClassNode> cs1 = getClassNodes("/tmp/minecraft-server-1.4.5.jar", "net/minecraft/server/");
        Map<String,ClassNode> cs2 = getClassNodes("/tmp/craftbukkit-1.4.5-R0.3-2536.jar", "net/minecraft/server/v1_4_5/");

        System.out.println(cs1.size() + " / " + cs2.size());

        Set<String> s1 = cs1.keySet();
        Set<String> s2 = cs2.keySet();

        s2.removeAll(s1);

        System.out.println("missing "+s2);

        /*
        for (MethodNode methodNode: (List<MethodNode>)c1.methods) {
            System.out.println("\t "+methodNode.name+" "+methodNode.desc);
        }
        */
    }
}

@@ -1663,75 +1663,526 @@
   %7D%0A
-    public static void main(String args%5B%5D) throws IOException%0A    %7B
+%0A    /**%0A     * Compute asymmetric set difference%0A     * @param s1 minuend%0A     * @param s2 subtrahend%0A     * @param %3CT%3E%0A     * @return s1 - s2%0A     */%0A    public static %3CT%3E Set%3CT%3E setDifference(Set%3CT%3E s1, Set%3CT%3E s2)%0A    %7B%0A        Set%3CT%3E diff = new HashSet%3CT%3E(s1);%0A        diff.removeAll(s2);%0A        return diff;%0A    %7D%0A%0A    public static void main(String args%5B%5D) throws IOException%0A    %7B%0A        String filename1 = %22/tmp/minecraft-server-1.4.5.jar%22;%0A        String filename2 = %22/tmp/craftbukkit-1.4.5-R0.3-2536.jar%22;%0A
 %0A   
@@ -2232,41 +2232,17 @@
 des(
-%22/tmp/minecraft-server-1.4.5.jar%22
+filename1
 , %22n
@@ -2319,46 +2319,17 @@
 des(
-%22/tmp/craftbukkit-1.4.5-R0.3-2536.jar%22
+filename2
 , %22n
@@ -2446,94 +2446,302 @@
 ng%3E 
-s1 = cs1.keySet();%0A        Set%3CString%3E s2 = cs2.keySet();%0A%0A        s2.removeAll(s1
+missing = setDifference(cs1.keySet(), cs2.keySet());%0A        if (!missing.isEmpty()) %7B%0A            System.out.println(%22Missing classes! %22 + filename1 + %22 - %22 + filename2 + %22 = %22 + missing);%0A            return;%0A        %7D%0A%0A        Set%3CString%3E surplus = setDifference(cs2.keySet(), cs1.keySet()
 );%0A
-%0A
     
@@ -2768,24 +2768,64 @@
 ln(%22
-missing %22+s2
+Classes added (%22 + surplus.size() + %22): %22 + surplus
 );%0A%0A
+%0A
     

Check for missing classes, in jar1 but not jar2