package javassist.expr; import javassist.bytecode.*; import javassist.*; public class HandlerTest{ //IHI1 /* public int test1(){ ConstPool cp = new ConstPool("assignment10.Point2.class"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); byte[] code = "dd".getBytes(); CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Handler hdl = new Handler(et, 0, ca.iterator(), null, null); return hdl.iterator.getCodeLength(); } //IHI2 public int test2()throws Exception{ ConstPool cp = new ConstPool("assignment10.Point2"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); byte[] code = "dd".getBytes(); Bytecode bc = new Bytecode(cp, 1, 0); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); Handler hdl = new Handler(et, 0, ca.iterator(), CtClass.intType, null); hdl.replace0(0, bc, 2); return hdl.maxStack; } //IHI3 public int test3()throws Exception{ ConstPool cp = new ConstPool("assignment10.Point2"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); byte[] code = "dd".getBytes(); Bytecode bc = new Bytecode(cp, 1, 1); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); Handler hdl = new Handler(et, 0, ca.iterator(), CtClass.intType, null); hdl.replace0(0, bc, 2); return hdl.maxLocals; } //IHI4 public boolean test4()throws Exception{ ConstPool cp = new ConstPool("assignment10.Point2"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); byte[] code = "dd".getBytes(); Bytecode bc = new Bytecode(cp, 1, 1); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); Handler hdl = new Handler(et, 0, ca.iterator(), CtClass.intType, null); hdl.replace0(0, bc, 2); return hdl.edited; } //IHI5 public String test5()throws Exception{ ConstPool cp = new ConstPool("assignment10.Point2"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); byte[] code = "dd".getBytes(); Bytecode bc = new Bytecode(cp, 1, 1); //bc.addIconst(3); // bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); // ClassPool pool = ClassPool.getDefault(); //CtClass cc = pool.get("assignment10.Point2"); Handler hdl = new Handler(et, 0, ca.iterator(), CtClass.intType, null); //hdl.replace0(0, bc, 2); return hdl.thisClass.getName(); } //EOA1 public boolean test6()throws Exception{ ConstPool cp = new ConstPool("assignment10.Point2"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); byte[] code = "dd".getBytes(); Bytecode bc = new Bytecode(cp, 1, 1); CodeAttribute ca = bc.toCodeAttribute(); Handler hdl = new Handler(et, 0, ca.iterator(), CtClass.intType, null); et.remove(0); return hdl.isFinally(); } public int test7()throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 0); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); hdl.insertBefore("System.out.println($1);"); return hdl.maxStack; } public int test8()throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 0); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); hdl.insertBefore("System.out.println($1);"); return hdl.maxLocals; } public int test9()throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 1); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); hdl.insertBefore("System.out.println($1);"); return hdl.maxLocals; } public boolean test10()throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); ExceptionTable et1 = new ExceptionTable(cp); et1.add(0,1,2,0,0); et1.add(1,1,2,0,2); //byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 0); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); Handler hdl1 = new Handler(et1, 1, ca.iterator(), cc, mi); et1.remove(1); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); //hdl.insertBefore("System.out.println($1);"); //CodeAttribute ca1 = hdl.iterator.get(); return hdl.isFinally(); }*/ public boolean test11()throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,0,2,0,0); et.add(1,1,2,0,2); ExceptionTable et1 = new ExceptionTable(cp); et1.add(0,1,2,0,0); et1.add(1,1,2,0,2); //byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 0); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); Handler hdl1 = new Handler(et1, 0, ca.iterator(), cc, mi); //et1.remove(1); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); //hdl.insertBefore("System.out.println($1);"); //CodeAttribute ca1 = hdl.iterator.get(); return hdl.isFinally(); } /* public static void main(String[] args) throws Exception{ ConstPool cp = new ConstPool("javassist.ClassPoolTailTest"); ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("c:\\mujava\\testset"); CtClass cc = pool.get("javassist.ClassPoolTailTest"); ExceptionTable et = new ExceptionTable(cp); et.add(0,1,2,0,0); et.add(1,1,2,0,2); byte[] code = "dd".getBytes(); // CodeAttribute ca = new CodeAttribute(cp, 5, 3, code, et); Bytecode bc = new Bytecode(cp, 1, 0); System.out.println(bc.length()); bc.addIndex(2); System.out.println(bc.length()); bc.addIconst(3); bc.addReturn(CtClass.intType); CodeAttribute ca = bc.toCodeAttribute(); MethodInfo mi = new MethodInfo(cp, "a", "b"); Handler hdl = new Handler(et, 1, ca.iterator(), cc, mi); //System.out.println(et.catchType(1)); //hdl.replace0(0, bc, 2); //ExprEditor ee = new ExprEditor(); //hdl.runEditor(ee, ca.iterator()); //et.remove(0); System.out.println(hdl.iterator.getCodeLength()); hdl.insertBefore("System.out.println($1);"); System.out.println(hdl.iterator.getCodeLength()); } */ }