package javassist.bytecode; //import javassist.bytecode.*; import javassist.*; import java.io.*; import java.util.*; public class AttributeInfoTest { //JSI_1,3 public String test1()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); ClassFile cf1 = new ClassFile(new DataInputStream(fin1)); ConstPool cp1 = cf1.getConstPool(); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp1, attrname, code); return ai.constPool.getClassName(); } /* //JSI_2,3 //NOT USED SINCE THIS TEST CAN BE SUBSTITUED WITH OTHER TESTS public int test2()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); int attrname = 1; int attrname1 = 2; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp, attrname1, code); return ai.name; } //PCI //PCI_1, JSI_3 public int test3()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); ClassFile cf1 = new ClassFile(new DataInputStream(fin1)); ConstPool cp1 = cf1.getConstPool(); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp1, attrname, code); AttributeInfo ai2 = new AttributeInfo(cp, 2, code); LinkedList aiLists = new LinkedList(); aiLists.add(ai); aiLists.add(ai1); return ai2.getLength(aiLists); } //JSI_2,3 PCI_2,3 public int test4()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); ClassFile cf1 = new ClassFile(new DataInputStream(fin1)); ConstPool cp1 = cf1.getConstPool(); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp1, attrname, code); AttributeInfo ai2 = new AttributeInfo(cp, 2, code); LinkedList aiLists = new LinkedList(); aiLists.add(ai); aiLists.add(ai1); DataOutputStream fout = new DataOutputStream(new FileOutputStream("fout.txt")); ai2.writeAll(aiLists, fout); return ai2.name; } //PCI_2,3,4,5,6 public int test5()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); ClassFile cf1 = new ClassFile(new DataInputStream(fin1)); ConstPool cp1 = cf1.getConstPool(); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp1, attrname, code); AttributeInfo ai2 = new AttributeInfo(cp, 2, code); LinkedList aiLists = new LinkedList(); aiLists.add(ai); aiLists.add(ai1); ai2.renameClass(aiLists, "old", "new"); return ai2.name; } //JSI_2,3 PCI_7,8,9 public int test6()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point2.class")); ClassFile cf = new ClassFile(new DataInputStream(fin)); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); ClassFile cf1 = new ClassFile(new DataInputStream(fin1)); ConstPool cp1 = cf1.getConstPool(); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = new AttributeInfo(cp1, attrname, code); AttributeInfo ai2 = new AttributeInfo(cp, 2, code); LinkedList aiLists = new LinkedList(); aiLists.add(ai); aiLists.add(ai1); HashMap hm = new HashMap(); hm.put("old", "new"); ai2.renameClass(aiLists, hm); return ai2.name; } //PNC //JSI_3, PNC_2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 public String test7()throws Exception{ byte[] b1 = new byte[2]; b1[0] = 1; b1[1] = 1; ByteArrayInputStream bais = new ByteArrayInputStream(b1); DataInputStream dis2 = new DataInputStream(bais); BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream ("c:\\mujava\\fout.txt")); DataOutputStream dos = new DataOutputStream(fout); dos.write(b1); dos.writeInt(0); dos.flush(); BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\classes\\javassist\\CtClass.class")); DataInputStream dis = new DataInputStream(fin); ClassFile cf = new ClassFile(dis); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\fout.txt")); DataInputStream dis1 = new DataInputStream(fin1); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = (AnnotationDefaultAttribute)ai.read(cp, dis1); return ((AnnotationDefaultAttribute)ai1).tag; } //JSI_3, PNC_18,26,27 public String test8()throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); DataInputStream dis = new DataInputStream(fin); ClassFile cf = new ClassFile(dis); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\classes\\javassist\\CtClass.class")); DataInputStream dis1 = new DataInputStream(fin1); ClassFile cf1 = new ClassFile(dis1); ConstPool cp1 = cf1.getConstPool(); HashMap hm = new HashMap(); hm.put("old", "new"); int attrname = 15; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = (LocalVariableAttribute)ai.copy(cp1, hm); return ((LocalVariableAttribute)ai1).tag; } /* public static void main(String[] args) throws Exception{ BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); DataInputStream dis = new DataInputStream(fin); ClassFile cf = new ClassFile(dis); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\fout.txt")); DataInputStream dis1 = new DataInputStream(fin1); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); */ //byte to int /* short value = 01; byte[] b = new byte[2]; for (int i = 0; i < 1; i++) { int offset = (b.length - 2 - i) * 8; b[i] = (byte) ((value >>> offset) & 0xFF); } short value1 = 40; for (int i = 1; i < 2; i++) { int offset = (b.length - 1 - i) * 8; b[i] = (byte) ((value >>> offset) & 0xFF); }*/ // System.out.println( "dd: " + ((b[0] & 0xFF) << 8)+ (b[1] & 0xFF)); /* byte[] b1 = new byte[2]; b1[0] = 1; b1[1] = 1; for(int i = 0;i < b1.length;i++){ System.out.println("b1" + i + ": " + (char)b1[i]); } ByteArrayInputStream bais = new ByteArrayInputStream(b1); DataInputStream dis2 = new DataInputStream(bais); while(bais.available() > 0) System.out.println("b's next " + bais.read()); BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream ("c:\\mujava\\fout.txt")); DataOutputStream dos = new DataOutputStream(fout); dos.write(b1); dos.writeInt(0); dos.flush(); // int name = dis1.readUnsignedShort(); // int name = dis2.readUnsignedShort(); // System.out.println(name); // System.out.println(cp.items.size()); // System.out.println(cp.numOfItems); /*for(int i = 0; i < cp.items.size();i++){ Object o = cp.items.elementAt(i); if(o != null) System.out.println(o.toString()); }*/ //Utf8Info u = new Utf8Info("z", 1); /* System.out.println(cp.addUtf8Info("z")); System.out.println(cp.addUtf8Info("equals")); System.out.println(cp.items.size()); System.out.println(cp.numOfItems); /* for(int i = 0; i < cp.items.size();i++) System.out.println(cp.items.elementAt(i));*/ /* String nameStr = cp.getUtf8Info(name); System.out.println(nameStr); */ //BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); /* BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\classes\\javassist\\CtClass.class")); DataInputStream dis = new DataInputStream(fin); ClassFile cf = new ClassFile(dis); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\fout.txt")); DataInputStream dis1 = new DataInputStream(fin1); int attrname = 1; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); //cp.addUtf8Info("z"); // cp.addUtf8Info("equals"); //AttributeInfo ai1 = (AnnotationDefaultAttribute)ai.read(cp, dis1); AttributeInfo ai1 = ai.read(cp, dis1); System.out.println(((AnnotationDefaultAttribute)ai1).tag); //System.out.println(cp.items.size()); // int name = dis1.readUnsignedShort(); //System.out.println(name); // String nameStr = cp.getUtf8Info(name); // System.out.println(nameStr); //return ((AnnotationDefaultAttribute)ai1).tag; BufferedInputStream fin = new BufferedInputStream(new FileInputStream("c:\\mujava\\testset\\javassist\\bytecode\\Point3.class")); DataInputStream dis = new DataInputStream(fin); ClassFile cf = new ClassFile(dis); ConstPool cp = cf.getConstPool(); BufferedInputStream fin1 = new BufferedInputStream(new FileInputStream("c:\\mujava\\classes\\javassist\\CtClass.class")); DataInputStream dis1 = new DataInputStream(fin1); ClassFile cf1 = new ClassFile(dis1); ConstPool cp1 = cf1.getConstPool(); HashMap hm = new HashMap(); hm.put("old", "new"); int attrname = 15; byte[] code = "".getBytes(); AttributeInfo ai = new AttributeInfo(cp, attrname, code); AttributeInfo ai1 = (LocalVariableAttribute)ai.copy(cp1, hm); }*/ }