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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations