List of usage examples for org.objectweb.asm Opcodes GETFIELD
int GETFIELD
To view the source code for org.objectweb.asm Opcodes GETFIELD.
Click Source Link
From source file:org.jacoco.core.internal.analysis.filter.KotlinCoroutineFilterTest.java
License:Open Source License
@Test public void should_filter_suspending_lambdas_generated_by_Kotlin_1_3_30() { 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);//from w ww .ja v a 2 s . c o m 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.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V", false); range1.toInclusive = m.instructions.getLast(); } // 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.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V", false); } 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 a va 2s. c o 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>// w w w .ja v 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.KotlinLateinitFilterTest.java
License:Open Source License
@Test public void testLateinitBranchIsFiltered() { final Label l1 = new Label(); final Label l2 = new Label(); m.visitLabel(l1);//from www . java 2 s. com m.visitVarInsn(Opcodes.ALOAD, 0); m.visitFieldInsn(Opcodes.GETFIELD, "com/better/alarm/background/VibrationService", "wakeLock", "Landroid/os/PowerManager$WakeLock;"); m.visitInsn(Opcodes.DUP); m.visitJumpInsn(Opcodes.IFNONNULL, l2); final AbstractInsnNode expectedFrom = m.instructions.getLast(); m.visitLdcInsn("wakelock"); m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "throwUninitializedPropertyAccessException", "(Ljava/lang/String;)V", false); final AbstractInsnNode expectedTo = m.instructions.getLast(); m.visitLabel(l2); m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "android/os/PowerManager$WakeLock", "acquire", "", false); filter.filter(m, context, output); assertIgnored(new Range(expectedFrom, expectedTo)); }
From source file:org.jacoco.core.internal.analysis.filter.SynchronizedFilterTest.java
License:Open Source License
@Test public void javac() { final Label start = new Label(); final Label end = new Label(); final Label handler = new Label(); final Label handlerEnd = new Label(); m.visitTryCatchBlock(start, end, handler, null); m.visitTryCatchBlock(handler, handlerEnd, handler, null); m.visitVarInsn(Opcodes.ALOAD, 0);// w w w . j av a2s .c o m m.visitFieldInsn(Opcodes.GETFIELD, "Fun", "lock", "Ljava/lang/Object;"); m.visitInsn(Opcodes.DUP); m.visitVarInsn(Opcodes.ASTORE, 1); m.visitInsn(Opcodes.MONITORENTER); m.visitLabel(start); m.visitInsn(Opcodes.NOP); m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.MONITOREXIT); m.visitLabel(end); final Label exit = new Label(); m.visitJumpInsn(Opcodes.GOTO, exit); m.visitLabel(handler); m.visitVarInsn(Opcodes.ASTORE, 2); m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.MONITOREXIT); m.visitLabel(handlerEnd); m.visitVarInsn(Opcodes.ALOAD, 2); m.visitInsn(Opcodes.ATHROW); m.visitLabel(exit); m.visitInsn(Opcodes.RETURN); filter.filter(m, context, output); assertIgnored(new Range((LabelNode) handler.info, ((LabelNode) exit.info).getPrevious())); }
From source file:org.jacoco.core.internal.analysis.filter.SynchronizedFilterTest.java
License:Open Source License
@Test public void ecj() { final Label start = new Label(); final Label end = new Label(); final Label handler = new Label(); final Label handlerEnd = new Label(); m.visitTryCatchBlock(start, end, handler, null); m.visitTryCatchBlock(handler, handlerEnd, handler, null); m.visitVarInsn(Opcodes.ALOAD, 0);//from w w w . j av a 2s .c om m.visitFieldInsn(Opcodes.GETFIELD, "Target", "lock", "Ljava/lang/Object;"); m.visitInsn(Opcodes.DUP); m.visitVarInsn(Opcodes.ASTORE, 1); m.visitInsn(Opcodes.MONITORENTER); m.visitLabel(start); m.visitVarInsn(Opcodes.ALOAD, 0); m.visitInsn(Opcodes.NOP); m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.MONITOREXIT); m.visitLabel(end); final Label exit = new Label(); m.visitJumpInsn(Opcodes.GOTO, exit); m.visitLabel(handler); m.visitVarInsn(Opcodes.ALOAD, 1); m.visitInsn(Opcodes.MONITOREXIT); m.visitLabel(handlerEnd); m.visitInsn(Opcodes.ATHROW); m.visitLabel(exit); m.visitInsn(Opcodes.RETURN); filter.filter(m, context, output); assertIgnored(new Range((LabelNode) handler.info, ((LabelNode) exit.info).getPrevious())); }
From source file:org.jacoco.core.internal.instr.DuplicateFrameEliminatorTest.java
License:Open Source License
@Test public void testFieldInsn() { testInstructionBetweenFrames(new FieldInsnNode(Opcodes.GETFIELD, "Foo", "f", "I")); }
From source file:org.jacoco.core.internal.instr.FrameTracker.java
License:Open Source License
@Override public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) { final Type t = Type.getType(desc); switch (opcode) { case Opcodes.PUTSTATIC: pop(t);/*from ww w . ja v a 2s .c om*/ break; case Opcodes.PUTFIELD: pop(t); pop(1); break; case Opcodes.GETSTATIC: push(t); break; case Opcodes.GETFIELD: pop(1); push(t); break; default: throw new IllegalArgumentException(); } mv.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.jacoco.core.runtime.ExecutionDataAccessTest.java
License:Open Source License
@Test public void testGenerateAccessCall() throws Exception { final boolean[] data = store.get(Long.valueOf(1234), "Sample", 5).getData(); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]); mv.visitCode();/*from www .j a va 2 s. co m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;"); ExecutionDataAccess.generateAccessCall(1234, "Sample", 5, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(6, 1); mv.visitEnd(); writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class) .newInstance(access); assertSame(data, callable.call()); }
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateAccessCall() throws Exception { final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes(); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]); mv.visitCode();//www .j a v a 2 s. c o m mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;"); RuntimeData.generateAccessCall(1234, "Sample", 5, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(6, 1); mv.visitEnd(); writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class).newInstance(data); assertSame(probes, callable.call()); }