1 package sample.vector; 2 3 import java.io.IOException ; 4 import javassist.*; 5 import javassist.preproc.Assistant; 6 7 43 public class VectorAssistant implements Assistant { 44 public final String packageName = "sample.vector."; 45 46 52 public CtClass[] assist(ClassPool pool, String vec, String [] args) 53 throws CannotCompileException 54 { 55 if (args.length != 1) 56 throw new CannotCompileException( 57 "VectorAssistant receives a single argument."); 58 59 try { 60 CtClass subclass; 61 CtClass elementType = pool.get(args[0]); 62 if (elementType.isPrimitive()) 63 subclass = makeSubclass2(pool, elementType); 64 else 65 subclass = makeSubclass(pool, elementType); 66 67 CtClass[] results = { subclass, pool.get(vec) }; 68 return results; 69 } 70 catch (NotFoundException e) { 71 throw new CannotCompileException(e); 72 } 73 catch (IOException e) { 74 throw new CannotCompileException(e); 75 } 76 } 77 78 84 public CtClass makeSubclass(ClassPool pool, CtClass type) 85 throws CannotCompileException, NotFoundException, IOException 86 { 87 CtClass vec = pool.makeClass(makeClassName(type)); 88 vec.setSuperclass(pool.get("java.util.Vector")); 89 90 CtClass c = pool.get("sample.vector.Sample"); 91 CtMethod addmethod = c.getDeclaredMethod("add"); 92 CtMethod atmethod = c.getDeclaredMethod("at"); 93 94 ClassMap map = new ClassMap(); 95 map.put("sample.vector.X", type.getName()); 96 97 vec.addMethod(CtNewMethod.copy(addmethod, "add", vec, map)); 98 vec.addMethod(CtNewMethod.copy(atmethod, "at", vec, map)); 99 vec.writeFile(); 100 return vec; 101 } 102 103 109 public CtClass makeSubclass2(ClassPool pool, CtClass type) 110 throws CannotCompileException, NotFoundException, IOException 111 { 112 CtClass vec = pool.makeClass(makeClassName(type)); 113 vec.setSuperclass(pool.get("java.util.Vector")); 114 115 CtClass c = pool.get("sample.vector.Sample2"); 116 CtMethod addmethod = c.getDeclaredMethod("add"); 117 CtMethod atmethod = c.getDeclaredMethod("at"); 118 119 CtClass[] args1 = { type }; 120 CtClass[] args2 = { CtClass.intType }; 121 CtMethod m 122 = CtNewMethod.wrapped(CtClass.voidType, "add", args1, 123 null, addmethod, null, vec); 124 vec.addMethod(m); 125 m = CtNewMethod.wrapped(type, "at", args2, 126 null, atmethod, null, vec); 127 vec.addMethod(m); 128 vec.writeFile(); 129 return vec; 130 } 131 132 private String makeClassName(CtClass type) { 133 return packageName + type.getSimpleName() + "Vector"; 134 } 135 } 136 | Popular Tags |