Search in sources :

Example 1 with AsmVisitorWrapper

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper in project byte-buddy by raphw.

the class AbstractDynamicTypeBuilderTest method testWriterHint.

@Test
@SuppressWarnings("unchecked")
public void testWriterHint() throws Exception {
    AsmVisitorWrapper asmVisitorWrapper = mock(AsmVisitorWrapper.class);
    when(asmVisitorWrapper.wrap(any(TypeDescription.class), any(ClassVisitor.class), any(Implementation.Context.class), any(TypePool.class), any(FieldList.class), any(MethodList.class), anyInt(), anyInt())).then(new Answer<ClassVisitor>() {

        @Override
        public ClassVisitor answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new ClassVisitor(Opcodes.ASM5, (ClassVisitor) invocationOnMock.getArguments()[1]) {

                @Override
                public void visitEnd() {
                    MethodVisitor mv = visitMethod(Opcodes.ACC_PUBLIC, FOO, "()Ljava/lang/String;", null, null);
                    mv.visitCode();
                    mv.visitLdcInsn(FOO);
                    mv.visitInsn(Opcodes.ARETURN);
                    mv.visitMaxs(-1, -1);
                    mv.visitEnd();
                }
            };
        }
    });
    when(asmVisitorWrapper.mergeWriter(0)).thenReturn(ClassWriter.COMPUTE_MAXS);
    Class<?> type = createPlain().visit(asmVisitorWrapper).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
    verify(asmVisitorWrapper).mergeWriter(0);
    verify(asmVisitorWrapper, atMost(1)).mergeReader(0);
    verify(asmVisitorWrapper).wrap(any(TypeDescription.class), any(ClassVisitor.class), any(Implementation.Context.class), any(TypePool.class), any(FieldList.class), any(MethodList.class), anyInt(), anyInt());
    verifyNoMoreInteractions(asmVisitorWrapper);
}
Also used : MethodList(net.bytebuddy.description.method.MethodList) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldList(net.bytebuddy.description.field.FieldList) MethodVisitor(org.objectweb.asm.MethodVisitor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AsmVisitorWrapper(net.bytebuddy.asm.AsmVisitorWrapper) TypeDescription(net.bytebuddy.description.type.TypeDescription) TypePool(net.bytebuddy.pool.TypePool) Test(org.junit.Test)

Example 2 with AsmVisitorWrapper

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper in project byte-buddy by raphw.

the class AbstractDynamicTypeBuilderForInliningTest method testReaderHint.

@Test
@SuppressWarnings("unchecked")
public void testReaderHint() throws Exception {
    AsmVisitorWrapper asmVisitorWrapper = mock(AsmVisitorWrapper.class);
    when(asmVisitorWrapper.wrap(any(TypeDescription.class), any(ClassVisitor.class), any(Implementation.Context.class), any(TypePool.class), any(FieldList.class), any(MethodList.class), anyInt(), anyInt())).then(new Answer<ClassVisitor>() {

        @Override
        public ClassVisitor answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new ClassVisitor(Opcodes.ASM5, (ClassVisitor) invocationOnMock.getArguments()[1]) {

                @Override
                public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                    return new LocalVariablesSorter(access, desc, super.visitMethod(access, name, desc, signature, exceptions));
                }
            };
        }
    });
    when(asmVisitorWrapper.mergeWriter(0)).thenReturn(ClassWriter.COMPUTE_MAXS);
    when(asmVisitorWrapper.mergeReader(0)).thenReturn(ClassReader.EXPAND_FRAMES);
    Class<?> type = create(StackMapFrames.class).visit(asmVisitorWrapper).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
    verify(asmVisitorWrapper).mergeWriter(0);
    verify(asmVisitorWrapper).mergeReader(0);
    verify(asmVisitorWrapper).wrap(any(TypeDescription.class), any(ClassVisitor.class), any(Implementation.Context.class), any(TypePool.class), any(FieldList.class), any(MethodList.class), anyInt(), anyInt());
    verifyNoMoreInteractions(asmVisitorWrapper);
}
Also used : MethodList(net.bytebuddy.description.method.MethodList) FieldList(net.bytebuddy.description.field.FieldList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AsmVisitorWrapper(net.bytebuddy.asm.AsmVisitorWrapper) TypeDescription(net.bytebuddy.description.type.TypeDescription) LocalVariablesSorter(org.objectweb.asm.commons.LocalVariablesSorter) TypePool(net.bytebuddy.pool.TypePool) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Aggregations

AsmVisitorWrapper (net.bytebuddy.asm.AsmVisitorWrapper)2 FieldList (net.bytebuddy.description.field.FieldList)2 MethodList (net.bytebuddy.description.method.MethodList)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 TypePool (net.bytebuddy.pool.TypePool)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 LocalVariablesSorter (org.objectweb.asm.commons.LocalVariablesSorter)1