1 15 16 package javassist; 17 18 import javassist.bytecode.*; 19 20 29 public final class CtMethod extends CtBehavior { 30 protected String cachedStringRep; 31 32 CtMethod(MethodInfo minfo, CtClass declaring) { 33 super(declaring, minfo); 34 next = null; 35 cachedStringRep = null; 36 } 37 38 49 public CtMethod(CtClass returnType, String mname, 50 CtClass[] parameters, CtClass declaring) { 51 this(null, declaring); 52 ConstPool cp = declaring.getClassFile2().getConstPool(); 53 String desc = Descriptor.ofMethod(returnType, parameters); 54 methodInfo = new MethodInfo(cp, mname, desc); 55 setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); 56 } 57 58 105 public CtMethod(CtMethod src, CtClass declaring, ClassMap map) 106 throws CannotCompileException 107 { 108 this(null, declaring); 109 MethodInfo srcInfo = src.methodInfo; 110 CtClass srcClass = src.getDeclaringClass(); 111 ConstPool cp = declaring.getClassFile2().getConstPool(); 112 if (map == null) 113 map = new ClassMap(); 114 115 map.put(srcClass.getName(), declaring.getName()); 116 try { 117 CtClass srcSuper = srcClass.getSuperclass(); 118 if (srcSuper != null) { 119 String srcSuperName = srcSuper.getName(); 120 if (!srcSuperName.equals(CtClass.javaLangObject)) 121 map.put(srcSuperName, 122 declaring.getSuperclass().getName()); 123 } 124 125 methodInfo = new MethodInfo(cp, srcInfo.getName(), srcInfo, map); 126 } 127 catch (NotFoundException e) { 128 throw new CannotCompileException(e); 129 } 130 catch (BadBytecode e) { 131 throw new CannotCompileException(e); 132 } 133 } 134 135 140 public int hashCode() { 141 return getStringRep().hashCode(); 142 } 143 144 146 final String getStringRep() { 147 if (cachedStringRep == null) 148 cachedStringRep = methodInfo.getName() 149 + Descriptor.getParamDescriptor(methodInfo.getDescriptor()); 150 151 return cachedStringRep; 152 } 153 154 158 public boolean equals(Object obj) { 159 return obj != null && obj instanceof CtMethod 160 && ((CtMethod)obj).getStringRep().equals(getStringRep()); 161 } 162 163 166 public String getName() { 167 return methodInfo.getName(); 168 } 169 170 173 public void setName(String newname) { 174 declaringClass.checkModify(); 175 methodInfo.setName(newname); 176 } 177 178 181 public CtClass getReturnType() throws NotFoundException { 182 return getReturnType0(); 183 } 184 185 189 public boolean isEmpty() { 190 CodeAttribute ca = getMethodInfo2().getCodeAttribute(); 191 if (ca == null) return (getModifiers() & Modifier.ABSTRACT) != 0; 193 194 CodeIterator it = ca.iterator(); 195 try { 196 return it.hasNext() && it.byteAt(it.next()) == Opcode.RETURN 197 && !it.hasNext(); 198 } 199 catch (BadBytecode e) {} 200 return false; 201 } 202 203 217 public void setBody(CtMethod src, ClassMap map) 218 throws CannotCompileException 219 { 220 setBody0(src.declaringClass, src.methodInfo, 221 declaringClass, methodInfo, map); 222 } 223 224 235 public void setWrappedBody(CtMethod mbody, ConstParameter constParam) 236 throws CannotCompileException 237 { 238 declaringClass.checkModify(); 239 240 CtClass clazz = getDeclaringClass(); 241 CtClass[] params; 242 CtClass retType; 243 try { 244 params = getParameterTypes(); 245 retType = getReturnType(); 246 } 247 catch (NotFoundException e) { 248 throw new CannotCompileException(e); 249 } 250 251 Bytecode code = CtNewWrappedMethod.makeBody(clazz, 252 clazz.getClassFile2(), 253 mbody, 254 params, retType, 255 constParam); 256 CodeAttribute cattr = code.toCodeAttribute(); 257 methodInfo.setCodeAttribute(cattr); 258 methodInfo.setAccessFlags(methodInfo.getAccessFlags() 259 & ~AccessFlag.ABSTRACT); 260 } 261 262 264 273 public static class ConstParameter { 274 279 public static ConstParameter integer(int i) { 280 return new IntConstParameter(i); 281 } 282 283 288 public static ConstParameter integer(long i) { 289 return new LongConstParameter(i); 290 } 291 292 297 public static ConstParameter string(String s) { 298 return new StringConstParameter(s); 299 } 300 301 ConstParameter() {} 302 303 306 int compile(Bytecode code) throws CannotCompileException { 307 return 0; 308 } 309 310 String descriptor() { 311 return defaultDescriptor(); 312 } 313 314 317 static String defaultDescriptor() { 318 return "([Ljava/lang/Object;)Ljava/lang/Object;"; 319 } 320 321 326 String constDescriptor() { 327 return defaultConstDescriptor(); 328 } 329 330 333 static String defaultConstDescriptor() { 334 return "([Ljava/lang/Object;)V"; 335 } 336 } 337 338 static class IntConstParameter extends ConstParameter { 339 int param; 340 341 IntConstParameter(int i) { 342 param = i; 343 } 344 345 int compile(Bytecode code) throws CannotCompileException { 346 code.addIconst(param); 347 return 1; 348 } 349 350 String descriptor() { 351 return "([Ljava/lang/Object;I)Ljava/lang/Object;"; 352 } 353 354 String constDescriptor() { 355 return "([Ljava/lang/Object;I)V"; 356 } 357 } 358 359 static class LongConstParameter extends ConstParameter { 360 long param; 361 362 LongConstParameter(long l) { 363 param = l; 364 } 365 366 int compile(Bytecode code) throws CannotCompileException { 367 code.addLconst(param); 368 return 2; 369 } 370 371 String descriptor() { 372 return "([Ljava/lang/Object;J)Ljava/lang/Object;"; 373 } 374 375 String constDescriptor() { 376 return "([Ljava/lang/Object;J)V"; 377 } 378 } 379 380 static class StringConstParameter extends ConstParameter { 381 String param; 382 383 StringConstParameter(String s) { 384 param = s; 385 } 386 387 int compile(Bytecode code) throws CannotCompileException { 388 code.addLdc(param); 389 return 1; 390 } 391 392 String descriptor() { 393 return "([Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;"; 394 } 395 396 String constDescriptor() { 397 return "([Ljava/lang/Object;Ljava/lang/String;)V"; 398 } 399 } 400 } 401 | Popular Tags |