/* * Created on Dec 5, 2004 * */ package fr.lifl.stc.stan.tools; import java.io.IOException; import org.apache.bcel.classfile.Attribute; import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.EscapeAttribute; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; /** * @author yann * */ public class Diff { private static JavaClass[] classes; private static String diff(short[] s1, short[] s2) { String res = ""; for(int i = 0; i < s2.length; i++) { int j = 0; while(j < s1.length) { if(s2[i] == s1[j]) break; j++; } if(j == s1.length) res += s2[i] + " "; } return res; } private static void compare(EscapeAttribute a1, EscapeAttribute a2) { if(a1 == null && a2 == null) { return; } else if (a1 == null) { short[] s = a2.getOffsets(); String res = ""; for(int i = 0; i < s.length; i++) { res += s[i] + " "; } System.out.println("+ " + res); } else if (a2 == null) { short[] s = a1.getOffsets(); String res = ""; for(int i = 0; i < s.length; i++) { res += s[i] + " "; } System.out.println("- " + res); } else { String splus = diff(a1.getOffsets(),a2.getOffsets()); String sminus = diff(a2.getOffsets(),a1.getOffsets()); if(!splus.equals("")) System.out.println("+ " + splus); if(!sminus.equals("")) System.out.println("- " + sminus); } } private static EscapeAttribute findEscapeAttr(Attribute[] a) { EscapeAttribute res = null; for(int i = 0; i < a.length; i++) { if(a[i] instanceof EscapeAttribute) { res = (EscapeAttribute)a[i]; break; } } return res; } private static void compare(JavaClass c1, JavaClass c2) { if(!c1.getClassName().equals(c2.getClassName())) { System.err.println("Trying to compare different classes"); System.exit(1); } Method[] m1 = c1.getMethods(); Method[] m2 = c2.getMethods(); for(int i = 0; i < m1.length; i++) { System.out.println("Method: " + m1[i]); if(m1[i].getCode() != null) { Attribute[] a1 = m1[i].getCode().getAttributes(); Attribute[] a2 = m2[i].getCode().getAttributes(); compare(findEscapeAttr(a1),findEscapeAttr(a2)); } } } public static void main(String[] args) { if(args.length != 2) { System.err.println("You must give exactly 2 arguments"); System.exit(1); } int i = 0; try { classes = new JavaClass[args.length]; while (i < args.length) { classes[i] = (new ClassParser(args[i])).parse(); i++; } compare(classes[0], classes[1]); } catch (IOException e) { System.err.println("E: File " + args[i] + " does not exist or you don't have permission on it."); System.exit(1); } } }