Search in sources :

Example 21 with StackManipulation

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

the class MethodInvocationGenericTest method testGenericMethod.

@Test
public void testGenericMethod() throws Exception {
    TypeDescription genericErasure = mock(TypeDescription.class);
    when(methodReturnType.asErasure()).thenReturn(genericErasure);
    StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription);
    assertThat(stackManipulation.isValid(), is(true));
    assertThat(stackManipulation, is((StackManipulation) new MethodInvocation.OfGenericMethod(genericErasure, MethodInvocation.invoke(declaredMethod))));
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 22 with StackManipulation

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

the class MethodInvocationHandleTest method testExactHandleStatic.

@Test
public void testExactHandleStatic() throws Exception {
    when(methodDescription.isStatic()).thenReturn(true);
    StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).onHandle(MethodInvocation.HandleType.EXACT);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(-1));
    assertThat(size.getMaximalSize(), is(0));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", BAZ, false);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 23 with StackManipulation

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

the class MethodInvocationHandleTest method testExactHandleNonStatic.

@Test
public void testExactHandleNonStatic() throws Exception {
    StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).onHandle(MethodInvocation.HandleType.EXACT);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(-1));
    assertThat(size.getMaximalSize(), is(0));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(" + BAR + BAZ.substring(1), false);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 24 with StackManipulation

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

the class MethodInvocationHandleTest method testExactHandleConstructor.

@Test
public void testExactHandleConstructor() throws Exception {
    when(methodDescription.isConstructor()).thenReturn(true);
    StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).onHandle(MethodInvocation.HandleType.EXACT);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(-1));
    assertThat(size.getMaximalSize(), is(0));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", BAZ, false);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 25 with StackManipulation

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

the class MethodVariableAccessOfMethodArgumentsTest method testBridgeMethodWithoutCasting.

@Test
public void testBridgeMethodWithoutCasting() throws Exception {
    when(bridgeMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(bridgeMethod, Arrays.asList(firstParameterType, secondParameterType)));
    StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription).asBridgeOf(bridgeMethod);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(PARAMETER_STACK_SIZE));
    assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 2);
    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