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.jacoco.core.instr.ResizeInstructionsTest.java
License:Open Source License
/** * Adds code that requires/*from www . j a v a 2 s. com*/ * {@link ClassWriter#getCommonSuperClass(String, String)}. * * <pre> * Object o = this; * while (true) { * o = (Integer) null; * } * </pre> */ private static void addCauseOfGetCommonSuperClass(final MethodVisitor mv) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ASTORE, 1); Label label = new Label(); mv.visitLabel(label); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer"); mv.visitVarInsn(Opcodes.ASTORE, 1); mv.visitJumpInsn(Opcodes.GOTO, label); }
From source file:org.jacoco.core.internal.analysis.filter.AbstractMatcherTest.java
License:Open Source License
@Test public void nextIsType() { m.visitInsn(Opcodes.NOP);/* w w w . jav a2 s . com*/ m.visitTypeInsn(Opcodes.NEW, "descriptor"); // should set cursor to null when opcode mismatch matcher.cursor = m.instructions.getFirst(); matcher.nextIsType(Opcodes.CHECKCAST, "descriptor"); assertNull(matcher.cursor); // should set cursor to null when descriptor mismatch matcher.cursor = m.instructions.getFirst(); matcher.nextIsType(Opcodes.NEW, "another_descriptor"); assertNull(matcher.cursor); // should set cursor to next instruction when match matcher.cursor = m.instructions.getFirst(); matcher.nextIsType(Opcodes.NEW, "descriptor"); assertSame(m.instructions.getLast(), matcher.cursor); // should not do anything when cursor is null matcher.cursor = null; matcher.nextIsType(Opcodes.NEW, "descriptor"); }
From source file:org.jacoco.core.internal.analysis.filter.KotlinCoroutineFilterTest.java
License:Open Source License
/** * <pre>/*from ww w.ja v a2 s.co m*/ * runBlocking { * val x = 42 * nop(x) * suspendingFunction() * nop(x) * } * </pre> */ @Test public void should_filter_suspending_lambdas() { final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "invokeSuspend", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null); context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); m.visitLabel(new Label()); final Range range1 = new Range(); range1.fromInclusive = m.instructions.getLast(); m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt", "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false); m.visitVarInsn(Opcodes.ASTORE, 4); m.visitVarInsn(Opcodes.ALOAD, 0); // line of "runBlocking" m.visitFieldInsn(Opcodes.GETFIELD, "Target", "label", "I"); final Label dflt = new Label(); final Label state0 = new Label(); final Label state1 = new Label(); m.visitTableSwitchInsn(0, 1, dflt, state0, state1); m.visitLabel(state0); { m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.DUP); m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure"); Label label = new Label(); m.visitJumpInsn(Opcodes.IFEQ, label); m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure"); m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable"); m.visitInsn(Opcodes.ATHROW); m.visitInsn(Opcodes.POP); range1.toInclusive = m.instructions.getLast(); m.visitLabel(label); } // line before "suspendingFunction" m.visitInsn(Opcodes.NOP); // line of "suspendingFunction" m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false); m.visitInsn(Opcodes.DUP); final Range range2 = new Range(); range2.fromInclusive = m.instructions.getLast(); m.visitVarInsn(Opcodes.ALOAD, 4); final Label continuationLabelAfterLoadedResult = new Label(); m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult); // line of "runBlocking" m.visitVarInsn(Opcodes.ALOAD, 4); m.visitInsn(Opcodes.ARETURN); m.visitLabel(state1); m.visitVarInsn(Opcodes.ALOAD, 0); m.visitFieldInsn(Opcodes.GETFIELD, "Target", "I$0", "I"); m.visitVarInsn(Opcodes.ISTORE, 3); { m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.DUP); m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure"); final Label label = new Label(); m.visitJumpInsn(Opcodes.IFEQ, label); m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure"); m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable"); m.visitInsn(Opcodes.ATHROW); m.visitInsn(Opcodes.POP); m.visitLabel(label); } m.visitVarInsn(Opcodes.ALOAD, 1); range2.toInclusive = m.instructions.getLast(); m.visitLabel(continuationLabelAfterLoadedResult); // line after "suspendingFunction" m.visitInsn(Opcodes.NOP); m.visitInsn(Opcodes.ARETURN); m.visitLabel(dflt); final Range range0 = new Range(); range0.fromInclusive = m.instructions.getLast(); m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException"); m.visitInsn(Opcodes.DUP); m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine"); m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V", false); m.visitInsn(Opcodes.ATHROW); range0.toInclusive = m.instructions.getLast(); filter.filter(m, context, output); assertIgnored(range0, range1, range2); }
From source file:org.jacoco.core.internal.analysis.filter.KotlinCoroutineFilterTest.java
License:Open Source License
/** * <pre>//from w w w . j av a 2 s .c o m * suspend fun example() { * suspendingFunction() * nop() * } * </pre> */ @Test public void should_filter_suspending_functions() { final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_STATIC, "example", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", null, null); context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC); final int continuationArgumentIndex = 0; final int continuationIndex = 2; m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex); final Range range1 = new Range(); range1.fromInclusive = m.instructions.getLast(); m.visitTypeInsn(Opcodes.INSTANCEOF, "ExampleKt$example$1"); final Label createStateInstance = new Label(); m.visitJumpInsn(Opcodes.IFEQ, createStateInstance); m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex); m.visitTypeInsn(Opcodes.CHECKCAST, "ExampleKt$example$1"); m.visitVarInsn(Opcodes.ASTORE, continuationIndex); m.visitVarInsn(Opcodes.ALOAD, continuationIndex); m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I"); m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE)); m.visitInsn(Opcodes.IAND); m.visitJumpInsn(Opcodes.IFEQ, createStateInstance); m.visitVarInsn(Opcodes.ALOAD, continuationIndex); m.visitInsn(Opcodes.DUP); m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I"); m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE)); m.visitInsn(Opcodes.ISUB); m.visitFieldInsn(Opcodes.PUTFIELD, "ExampleKt$example$1", "label", "I"); final Label afterCoroutineStateCreated = new Label(); m.visitJumpInsn(Opcodes.GOTO, afterCoroutineStateCreated); m.visitLabel(createStateInstance); m.visitTypeInsn(Opcodes.NEW, "ExampleKt$example$1"); m.visitInsn(Opcodes.DUP); m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex); m.visitMethodInsn(Opcodes.INVOKESPECIAL, "ExampleKt$example$1", "<init>", "(Lkotlin/coroutines/Continuation;)V", false); m.visitVarInsn(Opcodes.ASTORE, continuationIndex); m.visitLabel(afterCoroutineStateCreated); m.visitVarInsn(Opcodes.ALOAD, continuationIndex); m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "result", "Ljava/lang/Object;"); m.visitVarInsn(Opcodes.ASTORE, 1); m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt", "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false); // line of "fun" m.visitVarInsn(Opcodes.ASTORE, 3); m.visitVarInsn(Opcodes.ALOAD, continuationIndex); m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I"); final Label dflt = new Label(); final Label state0 = new Label(); final Label state1 = new Label(); m.visitTableSwitchInsn(0, 1, dflt, state0, state1); m.visitLabel(state0); { m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.DUP); m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure"); Label label = new Label(); m.visitJumpInsn(Opcodes.IFEQ, label); m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure"); m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable"); m.visitInsn(Opcodes.ATHROW); m.visitInsn(Opcodes.POP); range1.toInclusive = m.instructions.getLast(); m.visitLabel(label); } // line of "suspendingFunction" m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false); m.visitInsn(Opcodes.DUP); final Range range2 = new Range(); range2.fromInclusive = m.instructions.getLast(); m.visitVarInsn(Opcodes.ALOAD, 3); final Label continuationLabelAfterLoadedResult = new Label(); m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult); // line of "fun" m.visitVarInsn(Opcodes.ALOAD, 3); m.visitInsn(Opcodes.ARETURN); m.visitLabel(state1); { m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.DUP); m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure"); final Label label = new Label(); m.visitJumpInsn(Opcodes.IFEQ, label); m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure"); m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable"); m.visitInsn(Opcodes.ATHROW); m.visitInsn(Opcodes.POP); m.visitLabel(label); } m.visitVarInsn(Opcodes.ALOAD, 1); range2.toInclusive = m.instructions.getLast(); m.visitLabel(continuationLabelAfterLoadedResult); // line after "suspendingFunction" m.visitInsn(Opcodes.NOP); m.visitInsn(Opcodes.ARETURN); m.visitLabel(dflt); final Range range0 = new Range(); range0.fromInclusive = m.instructions.getLast(); m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException"); m.visitInsn(Opcodes.DUP); m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine"); m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V", false); m.visitInsn(Opcodes.ATHROW); range0.toInclusive = m.instructions.getLast(); filter.filter(m, context, output); assertIgnored(range0, range1, range2); }
From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenFilterTest.java
License:Open Source License
@Test public void should_not_filter_explicit_else() { final Label label = new Label(); m.visitInsn(Opcodes.NOP);/* w ww .j a va 2 s . c o m*/ m.visitJumpInsn(Opcodes.IFEQ, label); m.visitInsn(Opcodes.NOP); m.visitLabel(label); m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException"); m.visitInsn(Opcodes.DUP); m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/NoWhenBranchMatchedException", "<init>", "()V", false); m.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Throwable"); m.visitInsn(Opcodes.ATHROW); filter.filter(m, context, output); assertIgnored(); assertNoReplacedBranches(); }
From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategy.java
License:Open Source License
public int storeInstance(final MethodVisitor mv, final boolean clinit, final int variable) { final Handle bootstrapMethod = new Handle(Opcodes.H_INVOKESTATIC, className, InstrSupport.INITMETHOD_NAME, B_DESC, isInterface);/* w ww. ja v a 2s .c o m*/ // As a workaround for https://bugs.openjdk.java.net/browse/JDK-8216970 // constant should have type Object mv.visitLdcInsn(new ConstantDynamic(InstrSupport.DATAFIELD_NAME, "Ljava/lang/Object;", bootstrapMethod)); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Z"); mv.visitVarInsn(Opcodes.ASTORE, variable); return 1; }
From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategyTest.java
License:Open Source License
@Test public void should_store_instance_using_condy_and_checkcast() { final MethodNode m = new MethodNode(); final int maxStack = strategy.storeInstance(m, false, 1); assertEquals(1, maxStack);/*from w w w . j a v a2 s. c o m*/ final ConstantDynamic constantDynamic = (ConstantDynamic) ((LdcInsnNode) m.instructions.get(0)).cst; assertEquals("$jacocoData", constantDynamic.getName()); assertEquals("Ljava/lang/Object;", constantDynamic.getDescriptor()); final Handle bootstrapMethod = constantDynamic.getBootstrapMethod(); assertEquals(Opcodes.H_INVOKESTATIC, bootstrapMethod.getTag()); assertEquals("ClassName", bootstrapMethod.getOwner()); assertEquals("$jacocoInit", bootstrapMethod.getName()); assertEquals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", bootstrapMethod.getDesc()); assertTrue(bootstrapMethod.isInterface()); final TypeInsnNode castInstruction = (TypeInsnNode) m.instructions.get(1); assertEquals(Opcodes.CHECKCAST, castInstruction.getOpcode()); assertEquals("[Z", castInstruction.desc); final VarInsnNode storeInstruction = (VarInsnNode) m.instructions.get(2); assertEquals(Opcodes.ASTORE, storeInstruction.getOpcode()); assertEquals(1, storeInstruction.var); assertEquals(3, m.instructions.size()); }
From source file:org.jacoco.core.internal.instr.FrameTracker.java
License:Open Source License
@Override public void visitTypeInsn(final int opcode, final String type) { switch (opcode) { case Opcodes.NEW: final Label label = new Label(); mv.visitLabel(label);//from w w w . j a va2 s . co m push(label); break; case Opcodes.ANEWARRAY: pop(1); push('[' + Type.getObjectType(type).getDescriptor()); break; case Opcodes.CHECKCAST: pop(1); push(type); break; case Opcodes.INSTANCEOF: pop(1); push(Opcodes.INTEGER); break; default: throw new IllegalArgumentException(); } mv.visitTypeInsn(opcode, type); }
From source file:org.jacoco.core.runtime.AbstractRuntimeData.java
License:Open Source License
/** * Generates the code that calls a {@link ControlFlowRuntimeData} instance through the * JRE API method {@link Object#equals(Object)}. The code pops a * {@link Object} instance from the stack and pushes the probe array of type * <code>boolean[]</code> on the operand stack. The generated code requires * a stack size of 6.//from w w w . j a va2 s .c o m * * @param classid * class identifier * @param classname * VM class name * @param probecount * probe count for this class * @param mv * visitor to emit generated code */ public static void generateAccessCall(final long classid, final String classname, final int probecount, final MethodVisitor mv) { // stack[0]: Ljava/lang/Object; generateArgumentArray(classid, classname, probecount, mv); // stack[1]: [Ljava/lang/Object; // stack[0]: Ljava/lang/Object; mv.visitInsn(Opcodes.DUP_X1); // stack[2]: [Ljava/lang/Object; // stack[1]: Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false); mv.visitInsn(Opcodes.POP); // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); // stack[0]: [Z mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); }
From source file:org.jacoco.core.runtime.ExecutionDataAccess.java
License:Open Source License
/** * Generates the code that calls the runtime data access through the JRE API * method {@link Object#equals(Object)}. The code pops a {@link Object} * instance from the stack and pushes the probe array of type * <code>boolean[]</code> on the operand stack. The generated code requires * a stack size of 6.//from w w w. j a v a 2 s. c om * * @param classid * @param classname * @param probecount * @param mv */ public static void generateAccessCall(final long classid, final String classname, final int probecount, final MethodVisitor mv) { // stack[0]: Ljava/lang/Object; generateArgumentArray(classid, classname, probecount, mv); // stack[1]: [Ljava/lang/Object; // stack[0]: Ljava/lang/Object; mv.visitInsn(Opcodes.DUP_X1); // stack[2]: [Ljava/lang/Object; // stack[1]: Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z"); mv.visitInsn(Opcodes.POP); // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); // stack[0]: [Z mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); }