Search in sources :

Example 6 with StackManipulation

use of net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.

the class ReferenceTypeAwareAssignerTest method testPrimitiveAssignabilityWhenEqual.

@Test
public void testPrimitiveAssignabilityWhenEqual() throws Exception {
    // Note: cannot mock equals
    TypeDescription.Generic primitiveType = new TypeDescription.Generic.OfNonGenericType.ForLoadedType(int.class);
    StackManipulation stackManipulation = ReferenceTypeAwareAssigner.INSTANCE.assign(primitiveType, primitiveType, Assigner.Typing.DYNAMIC);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
    verifyZeroInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 7 with StackManipulation

use of net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.

the class ReferenceTypeAwareAssignerTest method testTargetToSourceAssignableRuntimeType.

@Test
public void testTargetToSourceAssignableRuntimeType() throws Exception {
    defineAssignability(false, false);
    when(rawTarget.getInternalName()).thenReturn(FOO);
    StackManipulation stackManipulation = ReferenceTypeAwareAssigner.INSTANCE.assign(source, target, Assigner.Typing.DYNAMIC);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
    verify(methodVisitor).visitTypeInsn(Opcodes.CHECKCAST, FOO);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 8 with StackManipulation

use of net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.

the class AbstractArrayFactoryTest method testCreationUsing.

protected void testCreationUsing(Class<?> componentType, int storageOpcode) throws Exception {
    defineComponentType(componentType);
    CollectionFactory arrayFactory = ArrayFactory.forType(this.componentType);
    StackManipulation arrayStackManipulation = arrayFactory.withValues(Collections.singletonList(stackManipulation));
    assertThat(arrayStackManipulation.isValid(), is(true));
    verify(stackManipulation, atLeast(1)).isValid();
    StackManipulation.Size size = arrayStackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(3 + StackSize.of(componentType).toIncreasingSize().getSizeImpact()));
    verify(methodVisitor).visitInsn(Opcodes.ICONST_1);
    verifyArrayCreation(methodVisitor);
    verify(methodVisitor).visitInsn(Opcodes.DUP);
    verify(methodVisitor).visitInsn(Opcodes.ICONST_0);
    verify(stackManipulation).apply(methodVisitor, implementationContext);
    verify(methodVisitor).visitInsn(storageOpcode);
    verifyNoMoreInteractions(methodVisitor);
    verifyNoMoreInteractions(stackManipulation);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation)

Example 9 with StackManipulation

use of net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.

the class ArrayAccessOtherTest method testForEach.

@Test
public void testForEach() throws Exception {
    StackManipulation stackManipulation = mock(StackManipulation.class);
    assertThat(ArrayAccess.REFERENCE.forEach(Collections.singletonList(stackManipulation)), is((StackManipulation) new StackManipulation.Compound(new StackManipulation.Compound(Duplication.SINGLE, IntegerConstant.forValue(0), ArrayAccess.REFERENCE.new Loader(), stackManipulation))));
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 10 with StackManipulation

use of net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.

the class ClassConstantReferenceTest method testClassConstantLegacy.

@Test
public void testClassConstantLegacy() throws Exception {
    when(typeDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)).thenReturn(false);
    when(typeDescription.getName()).thenReturn(FOO);
    StackManipulation stackManipulation = ClassConstant.of(typeDescription);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(1));
    verify(typeDescription).getName();
    verify(typeDescription, times(9)).represents(any(Class.class));
    verifyNoMoreInteractions(typeDescription);
    verify(methodVisitor).visitLdcInsn(FOO);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Class.class), "forName", Type.getMethodDescriptor(Type.getType(Class.class), Type.getType(String.class)), false);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Aggregations

StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)88 Test (org.junit.Test)85 TypeDescription (net.bytebuddy.description.type.TypeDescription)18 Implementation (net.bytebuddy.implementation.Implementation)5 Assigner (net.bytebuddy.implementation.bytecode.assign.Assigner)5 MethodDescription (net.bytebuddy.description.method.MethodDescription)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 JavaConstant (net.bytebuddy.utility.JavaConstant)2 Handle (org.objectweb.asm.Handle)2 Field (java.lang.reflect.Field)1 MethodAccessorFactory (net.bytebuddy.implementation.MethodAccessorFactory)1 ElementMatcher (net.bytebuddy.matcher.ElementMatcher)1