List of usage examples for org.objectweb.asm Opcodes CHECKCAST
int CHECKCAST
To view the source code for org.objectweb.asm Opcodes CHECKCAST.
Click Source Link
From source file:org.copperengine.core.instrument.TryCatchBlockHandler.java
License:Apache License
@SuppressWarnings("unchecked") public void instrument(ClassNode cn) { // if (1 == 1) return; for (MethodNode m : (List<MethodNode>) cn.methods) { if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) { continue; }// w ww. j a v a 2s. c om logger.info("Instrument " + cn.name + "." + m.name); HashSet<Label> labels = new HashSet<Label>(); for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) { if (labels.contains(catchNode.handler.getLabel())) { // some handlers share their handling code - check it out to prevent double instrumentation logger.info("skipping node"); continue; } labels.add(catchNode.handler.getLabel()); LabelNode labelNode = catchNode.handler; AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode ? labelNode.getNext() : labelNode; FrameNode frameNode = (FrameNode) lineNumberNode.getNext(); VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext(); AbstractInsnNode insertPoint = varInsnNode; if (catchNode.type == null) { // this is probably a finally block; if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) { insertPoint = insertPoint.getNext(); } } LabelNode labelNode4ifeg = new LabelNode(); InsnList newCode = new InsnList(); newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var)); newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME)); newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg)); newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var)); newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME)); newCode.add(new InsnNode(Opcodes.ATHROW)); newCode.add(labelNode4ifeg); m.instructions.insert(insertPoint, newCode); } } }
From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java
License:Open Source License
@Override public void visitTypeInsn(final int opcode, final String type) { assert type != null; final T t = getDu().getT(type); switch (opcode) { /********/* w w w. ja v a2 s . com*/ * CAST * ********/ case Opcodes.CHECKCAST: add(new CAST(this.ops.size(), opcode, this.line, T.REF, t)); break; /************** * INSTANCEOF * **************/ case Opcodes.INSTANCEOF: add(new INSTANCEOF(this.ops.size(), opcode, this.line, t)); break; /******* * NEW * *******/ case Opcodes.NEW: add(new NEW(this.ops.size(), opcode, this.line, t)); break; /************ * NEWARRAY * ************/ case Opcodes.ANEWARRAY: add(new NEWARRAY(this.ops.size(), opcode, this.line, getDu().getArrayT(t), 1)); break; default: log.warn(getM() + ": Unknown var insn opcode '" + opcode + "'!"); } }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractCreateDispatchCodeAdapter.java
License:Open Source License
protected InsnList getDispatchCode(MethodNode method, int joinPointId, int boundMethodId) { InsnList instructions = new InsnList(); // teams = TeamManager.getTeams(joinpointId) instructions.add(createLoadIntConstant(joinPointId)); instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ClassNames.TEAM_MANAGER_SLASH, ConstantMembers.getTeams.getName(), ConstantMembers.getTeams.getSignature(), false)); instructions.add(createInstructionsToCheackTeams(method)); // get the first team instructions.add(new InsnNode(Opcodes.DUP)); instructions.add(new InsnNode(Opcodes.ICONST_0)); instructions.add(new InsnNode(Opcodes.AALOAD)); instructions.add(new InsnNode(Opcodes.SWAP)); if (isStatic) { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); } else {//from w w w .ja v a2s . co m // put "this" on the stack and cast it to IBoundBase2 instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, ClassNames.I_BOUND_BASE_SLASH)); } instructions.add(new InsnNode(Opcodes.SWAP)); // start index instructions.add(new InsnNode(Opcodes.ICONST_0)); // TeamManager.getCallinIds(joinpointId) instructions.add(createLoadIntConstant(joinPointId)); instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ClassNames.TEAM_MANAGER_SLASH, ConstantMembers.getCallinIds.getName(), ConstantMembers.getCallinIds.getSignature(), false)); instructions.add(createLoadIntConstant(boundMethodId)); args = Type.getArgumentTypes(method.desc); // box the arguments instructions.add(getBoxedArguments(args)); instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, ClassNames.ITEAM_SLASH, ConstantMembers.callAllBindingsTeam.getName(), ConstantMembers.callAllBindingsTeam.getSignature(), true)); Type returnType = Type.getReturnType(method.desc); instructions.add(getUnboxingInstructionsForReturnValue(returnType)); return instructions; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java
License:Open Source License
/** * Returns the instructions, that are needed to convert * a return value of the type {@link Object} to the real type * @param returnType the real type// w w w .j a v a 2s . c o m * @return */ protected InsnList getUnboxingInstructionsForReturnValue(Type returnType) { InsnList instructions = new InsnList(); switch (returnType.getSort()) { case Type.VOID: instructions.add(new InsnNode(Opcodes.POP)); instructions.add(new InsnNode(Opcodes.RETURN)); break; case Type.ARRAY: // fallthrough case Type.OBJECT: instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, returnType.getInternalName())); instructions.add(new InsnNode(Opcodes.ARETURN)); break; default: String objectType = AsmTypeHelper.getObjectType(returnType); instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType)); instructions.add(AsmTypeHelper.getUnboxingInstructionForType(returnType, objectType)); instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN))); } return instructions; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateFieldAccessAdapter.java
License:Open Source License
@Override public boolean transform() { InsnList instructions = new InsnList(); // put accessId on the stack instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex + 1)); // read or write access LabelNode writeAccess = new LabelNode(); instructions.add(new JumpInsnNode(Opcodes.IFNE, writeAccess)); // read access if (field.isStatic()) { // get value of field instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, name, field.getName(), field.getSignature())); } else {/*from w w w. j a va 2s. c om*/ // put "this" on the stack instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); // get value of field instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, field.getName(), field.getSignature())); } //box value as "Object" Type type = Type.getType(field.getSignature()); instructions.add(AsmTypeHelper.getBoxingInstructionForType(type)); instructions.add(new InsnNode(Opcodes.ARETURN)); //write access instructions.add(writeAccess); //put "args" on the stack instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2)); //get the first element of "args" instructions.add(new InsnNode(Opcodes.ICONST_0)); instructions.add(new InsnNode(Opcodes.AALOAD)); //unbox it if (type.getSort() != Type.ARRAY && type.getSort() != Type.OBJECT) { String objectType = AsmTypeHelper.getObjectType(type); instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType)); instructions.add(AsmTypeHelper.getUnboxingInstructionForType(type, objectType)); } else { instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, type.getInternalName())); } if (field.isStatic()) { //save value in field instructions.add(new FieldInsnNode(Opcodes.PUTSTATIC, name, field.getName(), field.getSignature())); } else { //put "this" on the stack instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); instructions.add(new InsnNode(Opcodes.SWAP)); //save value in field instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, field.getName(), field.getSignature())); } //dummy return instructions.add(new InsnNode(Opcodes.ACONST_NULL)); instructions.add(new InsnNode(Opcodes.ARETURN)); //add the instructions to a new label in the existing switch MethodNode method = getMethod(access); addNewLabelToSwitch(method.instructions, instructions, accessId); return true; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateMethodAccessAdapter.java
License:Open Source License
@Override public boolean transform() { MethodNode methodNode = getMethod(method); InsnList instructions = new InsnList(); if (isConstructor) { // create empty object for constructor invocation: instructions.add(new TypeInsnNode(Opcodes.NEW, name)); instructions.add(new InsnNode(Opcodes.DUP)); } else if (!method.isStatic()) { //put "this" on the stack for a non-static method instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); }/*from ww w.ja va2 s . c o m*/ //Unbox arguments Type[] args = Type.getArgumentTypes(methodNode.desc); if (args.length > 0) { for (int i = 0; i < args.length; i++) { instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2)); instructions.add(createLoadIntConstant(i)); instructions.add(new InsnNode(Opcodes.AALOAD)); Type arg = args[i]; if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) { String objectType = AsmTypeHelper.getObjectType(arg); instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType)); instructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType)); } else { instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName())); } } } //call original method int opcode = Opcodes.INVOKEVIRTUAL; if (method.isStatic()) { opcode = Opcodes.INVOKESTATIC; } else if (isConstructor) { opcode = Opcodes.INVOKESPECIAL; } instructions.add(new MethodInsnNode(opcode, name, method.getName(), method.getSignature())); //box return value Type returnType = Type.getReturnType(methodNode.desc); if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY && returnType.getSort() != Type.VOID) { instructions.add(AsmTypeHelper.getBoxingInstructionForType(returnType)); instructions.add(new InsnNode(Opcodes.ARETURN)); } else if (returnType.getSort() == Type.VOID && !isConstructor) { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); instructions.add(new InsnNode(Opcodes.ARETURN)); } else { instructions.add(new InsnNode(Opcodes.ARETURN)); } //add the instructions to a new label in the existing switch MethodNode access = getMethod(this.access); addNewLabelToSwitch(access.instructions, instructions, accessId); return true; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.MoveCodeToCallOrigAdapter.java
License:Open Source License
public boolean transform() { MethodNode orgMethod = getMethod(method); if ((orgMethod.access & Opcodes.ACC_ABSTRACT) != 0) return false; MethodNode callOrig = getMethod(this.callOrig); Type returnType = Type.getReturnType(orgMethod.desc); InsnList newInstructions = new InsnList(); //Unboxing arguments Type[] args = Type.getArgumentTypes(orgMethod.desc); int boundMethodIdSlot = firstArgIndex; if (args.length > 0) { // move boundMethodId to a higher slot, to make lower slots available for original locals newInstructions.add(new IntInsnNode(Opcodes.ILOAD, boundMethodIdSlot)); boundMethodIdSlot = callOrig.maxLocals + 1; newInstructions.add(new IntInsnNode(Opcodes.ISTORE, boundMethodIdSlot)); newInstructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + argOffset + 1)); int slot = firstArgIndex + argOffset; for (int i = argOffset; i < args.length; i++) { if (i < args.length - 1) { newInstructions.add(new InsnNode(Opcodes.DUP)); }/*from w ww. j ava 2 s . com*/ newInstructions.add(createLoadIntConstant(i)); newInstructions.add(new InsnNode(Opcodes.AALOAD)); Type arg = args[i]; if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) { String objectType = AsmTypeHelper.getObjectType(arg); newInstructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType)); newInstructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType)); } else { newInstructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName())); } newInstructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ISTORE), slot)); slot += arg.getSize(); } } if (superIsWeavable) adjustSuperCalls(orgMethod.instructions, orgMethod.name, args, returnType, boundMethodIdSlot); // replace return of the original method with areturn and box the result value if needed replaceReturn(orgMethod.instructions, returnType); newInstructions.add(orgMethod.instructions); addNewLabelToSwitch(callOrig.instructions, newInstructions, boundMethodId); // a minimum stacksize of 3 is needed to box the arguments callOrig.maxStack = Math.max(Math.max(callOrig.maxStack, orgMethod.maxStack), 3); // we have to increment the max. stack size, because we have to put NULL on the stack if (returnType.getSort() == Type.VOID) { callOrig.maxStack += 1; } callOrig.maxLocals = Math.max(callOrig.maxLocals, orgMethod.maxLocals); return true; }
From source file:org.elasticsearch.plan.a.Caster.java
License:Apache License
void writeCast(final MethodVisitor visitor, final Cast cast) { final Type from = cast.from; final Type to = cast.to; if (from.equals(to)) { return;/*from w ww . ja v a2 s . c om*/ } if (from.metadata.numeric && to.metadata.numeric) { switch (from.metadata) { case BYTE: switch (to.metadata) { case SHORT: visitor.visitInsn(Opcodes.I2S); break; case CHAR: visitor.visitInsn(Opcodes.I2C); break; case LONG: visitor.visitInsn(Opcodes.I2L); break; case FLOAT: visitor.visitInsn(Opcodes.I2F); break; case DOUBLE: visitor.visitInsn(Opcodes.I2D); break; } break; case SHORT: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.I2B); break; case CHAR: visitor.visitInsn(Opcodes.I2C); break; case LONG: visitor.visitInsn(Opcodes.I2L); break; case FLOAT: visitor.visitInsn(Opcodes.I2F); break; case DOUBLE: visitor.visitInsn(Opcodes.I2D); break; } break; case CHAR: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.I2B); break; case SHORT: visitor.visitInsn(Opcodes.I2S); break; case LONG: visitor.visitInsn(Opcodes.I2L); break; case FLOAT: visitor.visitInsn(Opcodes.I2F); break; case DOUBLE: visitor.visitInsn(Opcodes.I2D); break; } break; case INT: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.I2B); break; case SHORT: visitor.visitInsn(Opcodes.I2S); break; case CHAR: visitor.visitInsn(Opcodes.I2C); break; case LONG: visitor.visitInsn(Opcodes.I2L); break; case FLOAT: visitor.visitInsn(Opcodes.I2F); break; case DOUBLE: visitor.visitInsn(Opcodes.I2D); break; } break; case LONG: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.L2I); visitor.visitInsn(Opcodes.I2B); break; case SHORT: visitor.visitInsn(Opcodes.L2I); visitor.visitInsn(Opcodes.I2S); break; case CHAR: visitor.visitInsn(Opcodes.L2I); visitor.visitInsn(Opcodes.I2C); break; case INT: visitor.visitInsn(Opcodes.L2I); break; case FLOAT: visitor.visitInsn(Opcodes.L2F); break; case DOUBLE: visitor.visitInsn(Opcodes.L2D); break; } break; case FLOAT: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.F2I); visitor.visitInsn(Opcodes.I2B); break; case SHORT: visitor.visitInsn(Opcodes.F2I); visitor.visitInsn(Opcodes.I2S); break; case CHAR: visitor.visitInsn(Opcodes.F2I); visitor.visitInsn(Opcodes.I2C); break; case INT: visitor.visitInsn(Opcodes.F2I); break; case LONG: visitor.visitInsn(Opcodes.F2L); break; case DOUBLE: visitor.visitInsn(Opcodes.F2D); break; } break; case DOUBLE: switch (to.metadata) { case BYTE: visitor.visitInsn(Opcodes.D2I); visitor.visitInsn(Opcodes.I2B); break; case SHORT: visitor.visitInsn(Opcodes.D2I); visitor.visitInsn(Opcodes.I2S); break; case CHAR: visitor.visitInsn(Opcodes.D2I); visitor.visitInsn(Opcodes.I2C); break; case INT: visitor.visitInsn(Opcodes.D2I); break; case LONG: visitor.visitInsn(Opcodes.D2L); break; case FLOAT: visitor.visitInsn(Opcodes.D2F); break; } break; } } else { try { from.clazz.asSubclass(to.clazz); } catch (ClassCastException exception) { visitor.visitTypeInsn(Opcodes.CHECKCAST, to.internal); } } }
From source file:org.elasticsearch.plan.a.Caster.java
License:Apache License
void writeTransform(final MethodVisitor visitor, final Transform transform) { final Class clazz = transform.method.owner.clazz; final java.lang.reflect.Method method = transform.method.method; final String name = method.getName(); final String internal = transform.method.owner.internal; final String descriptor = transform.method.descriptor; final Type upcast = transform.upcast; final Type downcast = transform.downcast; if (upcast != null) { visitor.visitTypeInsn(Opcodes.CHECKCAST, upcast.internal); }//from ww w.j a v a 2s . c om if (java.lang.reflect.Modifier.isStatic(method.getModifiers())) { visitor.visitMethodInsn(Opcodes.INVOKESTATIC, internal, name, descriptor, false); } else if (java.lang.reflect.Modifier.isInterface(clazz.getModifiers())) { visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, internal, name, descriptor, true); } else { visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internal, name, descriptor, false); } if (downcast != null) { visitor.visitTypeInsn(Opcodes.CHECKCAST, downcast.internal); } }
From source file:org.evosuite.graphs.cfg.BytecodeInstruction.java
License:Open Source License
/** * <p>/*from w w w .ja va2 s . c o m*/ * getASMNodeString * </p> * * @return a {@link java.lang.String} object. */ public String getASMNodeString() { String type = getType(); String opcode = getInstructionType(); String stack = ""; if (frame == null) stack = "null"; else for (int i = 0; i < frame.getStackSize(); i++) { stack += frame.getStack(i) + ","; } if (asmNode instanceof LabelNode) { return "LABEL " + ((LabelNode) asmNode).getLabel().toString(); } else if (asmNode instanceof FieldInsnNode) return "Field" + " " + ((FieldInsnNode) asmNode).owner + "." + ((FieldInsnNode) asmNode).name + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof FrameNode) return "Frame" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof IincInsnNode) return "IINC " + ((IincInsnNode) asmNode).var + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof InsnNode) return "" + opcode; else if (asmNode instanceof IntInsnNode) return "INT " + ((IntInsnNode) asmNode).operand + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof MethodInsnNode) return opcode + " " + ((MethodInsnNode) asmNode).owner + "." + ((MethodInsnNode) asmNode).name + ((MethodInsnNode) asmNode).desc; else if (asmNode instanceof JumpInsnNode) return "JUMP " + ((JumpInsnNode) asmNode).label.getLabel() + " Type=" + type + ", Opcode=" + opcode + ", Stack: " + stack + " - Line: " + lineNumber; else if (asmNode instanceof LdcInsnNode) return "LDC " + ((LdcInsnNode) asmNode).cst + " Type=" + type; // + // ", Opcode="; // + opcode; // cst starts with mutationid if // this is location of mutation else if (asmNode instanceof LineNumberNode) return "LINE " + " " + ((LineNumberNode) asmNode).line; else if (asmNode instanceof LookupSwitchInsnNode) return "LookupSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof MultiANewArrayInsnNode) return "MULTIANEWARRAY " + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof TableSwitchInsnNode) return "TableSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode; else if (asmNode instanceof TypeInsnNode) { switch (asmNode.getOpcode()) { case Opcodes.NEW: return "NEW " + ((TypeInsnNode) asmNode).desc; case Opcodes.ANEWARRAY: return "ANEWARRAY " + ((TypeInsnNode) asmNode).desc; case Opcodes.CHECKCAST: return "CHECKCAST " + ((TypeInsnNode) asmNode).desc; case Opcodes.INSTANCEOF: return "INSTANCEOF " + ((TypeInsnNode) asmNode).desc; default: return "Unknown node" + " Type=" + type + ", Opcode=" + opcode; } } // return "TYPE " + " " + node.getOpcode() + " Type=" + type // + ", Opcode=" + opcode; else if (asmNode instanceof VarInsnNode) return opcode + " " + ((VarInsnNode) asmNode).var; else return "Unknown node" + " Type=" + type + ", Opcode=" + opcode; }