1 15 16 package javassist.expr; 17 18 import javassist.*; 19 import javassist.bytecode.*; 20 import javassist.compiler.*; 21 import javassist.compiler.ast.ASTList; 22 23 26 public class Cast extends Expr { 27 30 protected Cast(int pos, CodeIterator i, CtClass declaring, MethodInfo m) { 31 super(pos, i, declaring, m); 32 } 33 34 38 public CtBehavior where() { return super.where(); } 39 40 46 public int getLineNumber() { 47 return super.getLineNumber(); 48 } 49 50 55 public String getFileName() { 56 return super.getFileName(); 57 } 58 59 63 public CtClass getType() throws NotFoundException { 64 ConstPool cp = getConstPool(); 65 int pos = currentPos; 66 int index = iterator.u16bitAt(pos + 1); 67 String name = cp.getClassInfo(index); 68 return Descriptor.toCtClass(name, thisClass.getClassPool()); 69 } 70 71 77 public CtClass[] mayThrow() { 78 return super.mayThrow(); 79 } 80 81 89 public void replace(String statement) throws CannotCompileException { 90 ConstPool constPool = getConstPool(); 91 int pos = currentPos; 92 int index = iterator.u16bitAt(pos + 1); 93 94 Javac jc = new Javac(thisClass); 95 ClassPool cp = thisClass.getClassPool(); 96 CodeAttribute ca = iterator.get(); 97 98 try { 99 CtClass[] params 100 = new CtClass[] { cp.get(javaLangObject) }; 101 CtClass retType = getType(); 102 103 int paramVar = ca.getMaxLocals(); 104 jc.recordParams(javaLangObject, params, true, paramVar, 105 withinStatic()); 106 int retVar = jc.recordReturnType(retType, true); 107 jc.recordProceed(new ProceedForCast(index, retType)); 108 109 111 checkResultValue(retType, statement); 112 113 Bytecode bytecode = jc.getBytecode(); 114 storeStack(params, true, paramVar, bytecode); 115 jc.recordLocalVariables(ca, pos); 116 117 bytecode.addConstZero(retType); 118 bytecode.addStore(retVar, retType); 120 jc.compileStmnt(statement); 121 bytecode.addLoad(retVar, retType); 122 123 replace0(pos, bytecode, 3); 124 } 125 catch (CompileError e) { throw new CannotCompileException(e); } 126 catch (NotFoundException e) { throw new CannotCompileException(e); } 127 catch (BadBytecode e) { 128 throw new CannotCompileException("broken method"); 129 } 130 } 131 132 134 static class ProceedForCast implements ProceedHandler { 135 int index; 136 CtClass retType; 137 138 ProceedForCast(int i, CtClass t) { 139 index = i; 140 retType = t; 141 } 142 143 public void doit(JvstCodeGen gen, Bytecode bytecode, ASTList args) 144 throws CompileError 145 { 146 if (gen.getMethodArgsLength(args) != 1) 147 throw new CompileError(Javac.proceedName 148 + "() cannot take more than one parameter " 149 + "for cast"); 150 151 gen.atMethodArgs(args, new int[1], new int[1], new String [1]); 152 bytecode.addOpcode(Opcode.CHECKCAST); 153 bytecode.addIndex(index); 154 gen.setType(retType); 155 } 156 157 public void setReturnType(JvstTypeChecker c, ASTList args) 158 throws CompileError 159 { 160 c.atMethodArgs(args, new int[1], new int[1], new String [1]); 161 c.setType(retType); 162 } 163 } 164 } 165 | Popular Tags |