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 Instanceof extends Expr { 27 30 protected Instanceof(int pos, CodeIterator i, CtClass declaring, 31 MethodInfo m) { 32 super(pos, i, declaring, m); 33 } 34 35 39 public CtBehavior where() { return super.where(); } 40 41 47 public int getLineNumber() { 48 return super.getLineNumber(); 49 } 50 51 57 public String getFileName() { 58 return super.getFileName(); 59 } 60 61 66 public CtClass getType() throws NotFoundException { 67 ConstPool cp = getConstPool(); 68 int pos = currentPos; 69 int index = iterator.u16bitAt(pos + 1); 70 String name = cp.getClassInfo(index); 71 return Descriptor.toCtClass(name, thisClass.getClassPool()); 72 } 73 74 80 public CtClass[] mayThrow() { 81 return super.mayThrow(); 82 } 83 84 92 public void replace(String statement) throws CannotCompileException { 93 ConstPool constPool = getConstPool(); 94 int pos = currentPos; 95 int index = iterator.u16bitAt(pos + 1); 96 97 Javac jc = new Javac(thisClass); 98 ClassPool cp = thisClass.getClassPool(); 99 CodeAttribute ca = iterator.get(); 100 101 try { 102 CtClass[] params 103 = new CtClass[] { cp.get(javaLangObject) }; 104 CtClass retType = CtClass.booleanType; 105 106 int paramVar = ca.getMaxLocals(); 107 jc.recordParams(javaLangObject, params, true, paramVar, 108 withinStatic()); 109 int retVar = jc.recordReturnType(retType, true); 110 jc.recordProceed(new ProceedForInstanceof(index)); 111 112 jc.recordType(getType()); 114 115 117 checkResultValue(retType, statement); 118 119 Bytecode bytecode = jc.getBytecode(); 120 storeStack(params, true, paramVar, bytecode); 121 jc.recordLocalVariables(ca, pos); 122 123 bytecode.addConstZero(retType); 124 bytecode.addStore(retVar, retType); 126 jc.compileStmnt(statement); 127 bytecode.addLoad(retVar, retType); 128 129 replace0(pos, bytecode, 3); 130 } 131 catch (CompileError e) { throw new CannotCompileException(e); } 132 catch (NotFoundException e) { throw new CannotCompileException(e); } 133 catch (BadBytecode e) { 134 throw new CannotCompileException("broken method"); 135 } 136 } 137 138 140 static class ProceedForInstanceof implements ProceedHandler { 141 int index; 142 143 ProceedForInstanceof(int i) { 144 index = i; 145 } 146 147 public void doit(JvstCodeGen gen, Bytecode bytecode, ASTList args) 148 throws CompileError 149 { 150 if (gen.getMethodArgsLength(args) != 1) 151 throw new CompileError(Javac.proceedName 152 + "() cannot take more than one parameter " 153 + "for instanceof"); 154 155 gen.atMethodArgs(args, new int[1], new int[1], new String [1]); 156 bytecode.addOpcode(Opcode.INSTANCEOF); 157 bytecode.addIndex(index); 158 gen.setType(CtClass.booleanType); 159 } 160 161 public void setReturnType(JvstTypeChecker c, ASTList args) 162 throws CompileError 163 { 164 c.atMethodArgs(args, new int[1], new int[1], new String [1]); 165 c.setType(CtClass.booleanType); 166 } 167 } 168 } 169 | Popular Tags |