1 15 16 package javassist; 17 18 import javassist.bytecode.*; 19 import javassist.compiler.Javac; 20 import javassist.compiler.CompileError; 21 22 35 public final class CtConstructor extends CtBehavior { 36 protected CtConstructor(MethodInfo minfo, CtClass declaring) { 37 super(declaring, minfo); 38 next = null; 39 } 40 41 56 public CtConstructor(CtClass[] parameters, CtClass declaring) { 57 this((MethodInfo)null, declaring); 58 ConstPool cp = declaring.getClassFile2().getConstPool(); 59 String desc = Descriptor.ofConstructor(parameters); 60 methodInfo = new MethodInfo(cp, "<init>", desc); 61 setModifiers(Modifier.PUBLIC); 62 } 63 64 97 public CtConstructor(CtConstructor src, CtClass declaring, ClassMap map) 98 throws CannotCompileException 99 { 100 this((MethodInfo)null, declaring); 101 MethodInfo srcInfo = src.methodInfo; 102 CtClass srcClass = src.getDeclaringClass(); 103 ConstPool cp = declaring.getClassFile2().getConstPool(); 104 if (map == null) 105 map = new ClassMap(); 106 107 map.put(srcClass.getName(), declaring.getName()); 108 try { 109 boolean patch = false; 110 CtClass srcSuper = srcClass.getSuperclass(); 111 String destSuperName = declaring.getSuperclass().getName(); 112 if (srcSuper != null) { 113 String srcSuperName = srcSuper.getName(); 114 if (!srcSuperName.equals(destSuperName)) 115 if (srcSuperName.equals(CtClass.javaLangObject)) 116 patch = true; 117 else 118 map.put(srcSuperName, destSuperName); 119 } 120 121 methodInfo = new MethodInfo(cp, srcInfo.getName(), srcInfo, map); 122 if (patch) 123 methodInfo.setSuperclass(destSuperName); 124 } 125 catch (NotFoundException e) { 126 throw new CannotCompileException(e); 127 } 128 catch (BadBytecode e) { 129 throw new CannotCompileException(e); 130 } 131 } 132 133 136 public boolean isConstructor() { 137 return methodInfo.isConstructor(); 138 } 139 140 143 public boolean isClassInitializer() { 144 return methodInfo.isStaticInitializer(); 145 } 146 147 153 public String getName() { 154 if (methodInfo.isStaticInitializer()) 155 return MethodInfo.nameClinit; 156 else 157 return declaringClass.getSimpleName(); 158 } 159 160 163 public boolean isEmpty() { 164 CodeAttribute ca = getMethodInfo2().getCodeAttribute(); 165 if (ca == null) 166 return false; 169 ConstPool cp = ca.getConstPool(); 170 CodeIterator it = ca.iterator(); 171 try { 172 int pos, desc; 173 return it.byteAt(it.next()) == Opcode.ALOAD_0 174 && it.byteAt(pos = it.next()) == Opcode.INVOKESPECIAL 175 && (desc = cp.isConstructor(CtClass.javaLangObject, 176 it.u16bitAt(pos + 1))) != 0 177 && cp.getUtf8Info(desc).equals("()V") 178 && it.byteAt(it.next()) == Opcode.RETURN 179 && !it.hasNext(); 180 } 181 catch (BadBytecode e) {} 182 return false; 183 } 184 185 194 public void setBody(String src) throws CannotCompileException { 195 if (src == null) 196 if (isClassInitializer()) 197 src = ";"; 198 else 199 src = "super();"; 200 201 super.setBody(src); 202 } 203 204 216 public void setBody(CtConstructor src, ClassMap map) 217 throws CannotCompileException 218 { 219 setBody0(src.declaringClass, src.methodInfo, 220 declaringClass, methodInfo, map); 221 } 222 223 231 public void insertBeforeBody(String src) throws CannotCompileException { 232 declaringClass.checkModify(); 233 if (isClassInitializer()) 234 throw new CannotCompileException("class initializer"); 235 236 CodeAttribute ca = methodInfo.getCodeAttribute(); 237 CodeIterator iterator = ca.iterator(); 238 Bytecode b = new Bytecode(methodInfo.getConstPool(), 239 ca.getMaxStack(), ca.getMaxLocals()); 240 b.setStackDepth(ca.getMaxStack()); 241 Javac jv = new Javac(b, declaringClass); 242 try { 243 jv.recordParams(getParameterTypes(), false); 244 jv.compileStmnt(src); 245 ca.setMaxStack(b.getMaxStack()); 246 ca.setMaxLocals(b.getMaxLocals()); 247 iterator.skipConstructor(); 248 int pos = iterator.insertEx(b.get()); 249 iterator.insert(b.getExceptionTable(), pos); 250 } 251 catch (NotFoundException e) { 252 throw new CannotCompileException(e); 253 } 254 catch (CompileError e) { 255 throw new CannotCompileException(e); 256 } 257 catch (BadBytecode e) { 258 throw new CannotCompileException(e); 259 } 260 } 261 262 265 int getStartPosOfBody(CodeAttribute ca) throws CannotCompileException { 266 CodeIterator ci = ca.iterator(); 267 try { 268 ci.skipConstructor(); 269 return ci.next(); 270 } 271 catch (BadBytecode e) { 272 throw new CannotCompileException(e); 273 } 274 } 275 } 276 | Popular Tags |