/** * Return whether or not the given instruction can throw exceptions. * * @param handle * the instruction * @return true if the instruction can throw an exception, false otherwise */ private boolean isPEI(InstructionHandle handle) { Instruction ins = handle.getInstruction(); if (!(ins instanceof ExceptionThrower)) return false; if (ins instanceof NEW) return false; // if (ins instanceof ATHROW) return false; if (ins instanceof GETSTATIC) return false; if (ins instanceof PUTSTATIC) return false; if (ins instanceof ReturnInstruction) return false; if (ins instanceof INSTANCEOF) return false; if (ins instanceof MONITOREXIT) return false; if (ins instanceof LDC) return false; return true; }
InstructionHandle insertTypecheckCode(MethodGen m, InstructionList il, InstructionHandle pos, int spawnId, int exceptionPos) { ArrayList<CodeExceptionGen> catches = mtab.getCatchTypes(m, spawnId); InstructionHandle[] jumpTargets = new InstructionHandle[catches.size() + 1]; BranchHandle[] jumps = new BranchHandle[catches.size()]; for (int i = 0; i < catches.size(); i++) { CodeExceptionGen e = catches.get(i); ObjectType type = e.getCatchType(); InstructionHandle catchTarget = e.getHandlerPC(); jumpTargets[i] = il.insert(pos, new ALOAD(exceptionPos)); il.insert(pos, new INSTANCEOF(cpg.addClass(type))); il.insert(pos, new BIPUSH((byte) 1)); jumps[i] = il.insert(pos, new IF_ICMPNE(null)); il.insert(pos, new ALOAD(exceptionPos)); il.insert(pos, ins_f.createCheckCast(type)); il.insert(pos, new GOTO(catchTarget)); } InstructionHandle t = il.insert(pos, new ALOAD(exceptionPos)); il.insert(pos, new ATHROW()); jumpTargets[catches.size()] = t; for (int i = 0; i < catches.size(); i++) { jumps[i].setTarget(jumpTargets[i + 1]); } return pos; }
public void visitINSTANCEOF(INSTANCEOF i) { Type t = i.getType(poolGen); log.log(" instr(instanceof)=" + t, Project.MSG_DEBUG); String type = t.toString(); design.checkClass(type); }
/** * Inject common exception catch blocks */ public void injectCommonExceptionCatchBlock(InstructionList il, MethodGen method, int variableIndex) { il.append(new INSTANCEOF(constantsPool.addClass(new ObjectType("java.lang.RuntimeException")))); BranchInstruction b1 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null); il.append(b1); il.append(InstructionFactory.createLoad(Type.OBJECT, variableIndex)); il.append(factory.createCheckCast(new ObjectType("java.lang.RuntimeException"))); il.append(InstructionConstants.ATHROW); InstructionHandle ih1 = il.append(InstructionFactory.createLoad(Type.OBJECT, variableIndex)); il.append(new INSTANCEOF(constantsPool.addClass(new ObjectType("java.lang.Error")))); BranchInstruction b2 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null); il.append(b2); il.append(InstructionFactory.createLoad(Type.OBJECT, variableIndex)); il.append(factory.createCheckCast(new ObjectType("java.lang.Error"))); il.append(InstructionConstants.ATHROW); InstructionHandle ih2 = il.append(InstructionFactory.createLoad(Type.OBJECT, variableIndex)); il.append(factory.createInvoke("java.lang.Throwable", "printStackTrace", Type.VOID, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(factory.createNew("org.codehaus.jremoting.client.InvocationException")); il.append(InstructionConstants.DUP); il.append(factory.createNew("java.lang.StringBuffer")); il.append(InstructionConstants.DUP); il.append(new PUSH(constantsPool, "Should never get here: ")); il.append(factory.createInvoke("java.lang.StringBuffer", "<init>", Type.VOID, new Type[]{Type.STRING}, Constants.INVOKESPECIAL)); il.append(InstructionFactory.createLoad(Type.OBJECT, variableIndex)); il.append(factory.createInvoke("java.lang.Throwable", "getMessage", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(factory.createInvoke("java.lang.StringBuffer", "append", Type.STRINGBUFFER, new Type[]{Type.STRING}, Constants.INVOKEVIRTUAL)); il.append(factory.createInvoke("java.lang.StringBuffer", "toString", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(factory.createInvoke("org.codehaus.jremoting.client.InvocationException", "<init>", Type.VOID, new Type[]{Type.STRING}, Constants.INVOKESPECIAL)); il.append(InstructionConstants.ATHROW); b1.setTarget(ih1); b2.setTarget(ih2); }
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */ public void visitINSTANCEOF(INSTANCEOF o){ indexValid(o, o.getIndex()); Constant c = cpg.getConstant(o.getIndex()); if (! (c instanceof ConstantClass)){ constraintViolated(o, "Expecting a CONSTANT_Class operand, but found a '"+c+"'."); } }
public boolean prescreen(ClassContext classContext, Method method) { BitSet bytecodeSet = classContext.getBytecodeSet(method); return bytecodeSet != null && (bytecodeSet.get(Constants.CHECKCAST) || bytecodeSet.get(Constants.INSTANCEOF)); }
public void visitINSTANCEOF( INSTANCEOF i ) { Type type = i.getType(_cp); _out.println("il.append(new INSTANCEOF(_cp.addClass(" + BCELifier.printType(type) + ")));"); }