1 15 16 package javassist; 17 18 import javassist.bytecode.*; 19 import javassist.compiler.Javac; 20 import javassist.compiler.CompileError; 21 import javassist.CtMethod.ConstParameter; 22 23 34 public class CtNewConstructor { 35 39 public static final int PASS_NONE = 0; 41 46 public static final int PASS_ARRAY = 1; 48 53 public static final int PASS_PARAMS = 2; 54 55 63 public static CtConstructor make(String src, CtClass declaring) 64 throws CannotCompileException 65 { 66 Javac compiler = new Javac(declaring); 67 try { 68 CtMember obj = compiler.compile(src); 69 if (obj instanceof CtConstructor) 70 return (CtConstructor)obj; 71 } 72 catch (CompileError e) { 73 throw new CannotCompileException(e); 74 } 75 76 throw new CannotCompileException("not a constructor"); 77 } 78 79 91 public static CtConstructor make(CtClass[] parameters, 92 CtClass[] exceptions, 93 String body, CtClass declaring) 94 throws CannotCompileException 95 { 96 try { 97 CtConstructor cc = new CtConstructor(parameters, declaring); 98 cc.setExceptionTypes(exceptions); 99 cc.setBody(body); 100 return cc; 101 } 102 catch (NotFoundException e) { 103 throw new CannotCompileException(e); 104 } 105 } 106 107 118 public static CtConstructor copy(CtConstructor c, CtClass declaring, 119 ClassMap map) throws CannotCompileException { 120 return new CtConstructor(c, declaring, map); 121 } 122 123 129 public static CtConstructor defaultConstructor(CtClass declaring) 130 throws CannotCompileException 131 { 132 CtConstructor cons = new CtConstructor((CtClass[])null, declaring); 133 134 ConstPool cp = declaring.getClassFile2().getConstPool(); 135 Bytecode code = new Bytecode(cp, 1, 1); 136 code.addAload(0); 137 try { 138 code.addInvokespecial(declaring.getSuperclass(), 139 "<init>", "()V"); 140 } 141 catch (NotFoundException e) { 142 throw new CannotCompileException(e); 143 } 144 145 code.add(Bytecode.RETURN); 146 147 cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); 148 return cons; 149 } 150 151 169 public static CtConstructor skeleton(CtClass[] parameters, 170 CtClass[] exceptions, CtClass declaring) 171 throws CannotCompileException 172 { 173 return make(parameters, exceptions, PASS_NONE, 174 null, null, declaring); 175 } 176 177 188 public static CtConstructor make(CtClass[] parameters, 189 CtClass[] exceptions, CtClass declaring) 190 throws CannotCompileException 191 { 192 return make(parameters, exceptions, PASS_PARAMS, 193 null, null, declaring); 194 } 195 196 300 public static CtConstructor make(CtClass[] parameters, 301 CtClass[] exceptions, int howto, 302 CtMethod body, ConstParameter cparam, 303 CtClass declaring) 304 throws CannotCompileException 305 { 306 return CtNewWrappedConstructor.wrapped(parameters, exceptions, 307 howto, body, cparam, declaring); 308 } 309 } 310 | Popular Tags |