Search in sources :

Example 86 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeForInLoop.

protected void writeForInLoop(ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    // Declare the loop counter.
    BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false);
    // Then get the iterator and generate the loop control
    MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression());
    iterator.visit(controller.getAcg());
    operandStack.doGroovyCast(ClassHelper.Iterator_TYPE);
    final int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();
    mv.visitLabel(continueLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorHasNext(mv);
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    mv.visitJumpInsn(IFEQ, breakLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorNext(mv);
    operandStack.push(ClassHelper.OBJECT_TYPE);
    operandStack.storeVar(variable);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
    compileStack.removeVar(iteratorIdx);
    compileStack.pop();
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Label(org.objectweb.asm.Label) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 87 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeSynchronized.

public void writeSynchronized(SynchronizedStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitSynchronizedStatement");
    writeStatementLabel(statement);
    final MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    statement.getExpression().visit(controller.getAcg());
    controller.getOperandStack().box();
    final int index = compileStack.defineTemporaryVariable("synchronized", ClassHelper.OBJECT_TYPE, true);
    final Label synchronizedStart = new Label();
    final Label synchronizedEnd = new Label();
    final Label catchAll = new Label();
    mv.visitVarInsn(ALOAD, index);
    mv.visitInsn(MONITORENTER);
    mv.visitLabel(synchronizedStart);
    // place holder for "empty" synchronized blocks, for example
    // if there is only a break/continue.
    mv.visitInsn(NOP);
    Runnable finallyPart = new Runnable() {

        public void run() {
            mv.visitVarInsn(ALOAD, index);
            mv.visitInsn(MONITOREXIT);
        }
    };
    BlockRecorder fb = new BlockRecorder(finallyPart);
    fb.startRange(synchronizedStart);
    compileStack.pushBlockRecorder(fb);
    statement.getCode().visit(controller.getAcg());
    fb.closeRange(catchAll);
    compileStack.writeExceptionTable(fb, catchAll, null);
    //pop fb
    compileStack.pop();
    finallyPart.run();
    mv.visitJumpInsn(GOTO, synchronizedEnd);
    mv.visitLabel(catchAll);
    finallyPart.run();
    mv.visitInsn(ATHROW);
    mv.visitLabel(synchronizedEnd);
    compileStack.removeVar(index);
}
Also used : BlockRecorder(org.codehaus.groovy.classgen.asm.CompileStack.BlockRecorder) Label(org.objectweb.asm.Label) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 88 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project groovy by apache.

the class StatementWriter method writeThrow.

public void writeThrow(ThrowStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitThrowStatement");
    writeStatementLabel(statement);
    MethodVisitor mv = controller.getMethodVisitor();
    statement.getExpression().visit(controller.getAcg());
    // we should infer the type of the exception from the expression
    mv.visitTypeInsn(CHECKCAST, "java/lang/Throwable");
    mv.visitInsn(ATHROW);
    controller.getOperandStack().remove(1);
}
Also used : MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 89 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project byte-buddy by raphw.

the class TypeProxyCreationTest method testForConstructorConstruction.

@Test
public void testForConstructorConstruction() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    StackManipulation stackManipulation = new TypeProxy.ForSuperMethodByConstructor(foo, implementationTarget, Collections.singletonList((TypeDescription) new TypeDescription.ForLoadedType(Void.class)), true, false);
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(foo);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(3));
    verify(implementationContext).register(any(AuxiliaryType.class));
    verifyNoMoreInteractions(implementationContext);
    verify(methodVisitor).visitTypeInsn(Opcodes.NEW, Type.getInternalName(Foo.class));
    verify(methodVisitor, times(2)).visitInsn(Opcodes.DUP);
    verify(methodVisitor).visitInsn(Opcodes.ACONST_NULL);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, foo.getInternalName(), MethodDescription.CONSTRUCTOR_INTERNAL_NAME, foo.getDeclaredMethods().filter(isConstructor()).getOnly().getDescriptor(), false);
    verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, foo.getInternalName(), TypeProxy.INSTANCE_FIELD, Type.getDescriptor(Void.class));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 90 with MethodVisitor

use of org.objectweb.asm.MethodVisitor in project byte-buddy by raphw.

the class TypeProxyCreationTest method testForDefaultMethodConstruction.

@Test
public void testForDefaultMethodConstruction() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    StackManipulation stackManipulation = new TypeProxy.ForDefaultMethod(foo, implementationTarget, false);
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(foo);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(2));
    verify(implementationContext).register(any(AuxiliaryType.class));
    verifyNoMoreInteractions(implementationContext);
    verify(methodVisitor).visitTypeInsn(Opcodes.NEW, Type.getInternalName(Foo.class));
    verify(methodVisitor, times(2)).visitInsn(Opcodes.DUP);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, foo.getInternalName(), MethodDescription.CONSTRUCTOR_INTERNAL_NAME, foo.getDeclaredMethods().filter(isConstructor()).getOnly().getDescriptor(), false);
    verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, foo.getInternalName(), TypeProxy.INSTANCE_FIELD, Type.getDescriptor(Void.class));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Aggregations

MethodVisitor (org.objectweb.asm.MethodVisitor)630 Label (org.objectweb.asm.Label)186 ClassWriter (org.objectweb.asm.ClassWriter)116 Type (org.objectweb.asm.Type)59 ClassNode (org.codehaus.groovy.ast.ClassNode)57 FieldVisitor (org.objectweb.asm.FieldVisitor)56 ClassVisitor (org.objectweb.asm.ClassVisitor)47 ClassReader (org.objectweb.asm.ClassReader)43 ArrayList (java.util.ArrayList)32 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)30 Test (org.junit.Test)29 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)29 List (java.util.List)23 LinkedList (java.util.LinkedList)22 Parameter (org.codehaus.groovy.ast.Parameter)22 InterfaceHelperClassNode (org.codehaus.groovy.ast.InterfaceHelperClassNode)18 AsmClassGenerator (org.codehaus.groovy.classgen.AsmClassGenerator)18 BytecodeExpression (org.codehaus.groovy.classgen.BytecodeExpression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)17 Method (java.lang.reflect.Method)16