Search in sources :

Example 1 with MethodDelegationBinder

use of net.bytebuddy.implementation.bind.MethodDelegationBinder in project byte-buddy by raphw.

the class TargetMethodAnnotationDrivenBinderTest method testDoNotBindOnIllegalMethodInvocation.

@Test
public void testDoNotBindOnIllegalMethodInvocation() throws Exception {
    when(targetMethod.isAccessibleTo(instrumentedType)).thenReturn(true);
    when(assignmentBinding.isValid()).thenReturn(true);
    when(methodInvocation.isValid()).thenReturn(false);
    when(termination.isValid()).thenReturn(true);
    when(targetMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(firstParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Collections.singletonList(firstPseudoAnnotation)));
    when(secondParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Collections.singletonList(secondPseudoAnnotation)));
    MethodDelegationBinder.ParameterBinding<?> firstBinding = prepareArgumentBinder(firstParameterBinder, FirstPseudoAnnotation.class, new Key(FOO));
    MethodDelegationBinder.ParameterBinding<?> secondBinding = prepareArgumentBinder(secondParameterBinder, SecondPseudoAnnotation.class, new Key(BAR));
    MethodDelegationBinder methodDelegationBinder = TargetMethodAnnotationDrivenBinder.of(Arrays.<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>asList(firstParameterBinder, secondParameterBinder));
    MethodDelegationBinder.MethodBinding methodBinding = methodDelegationBinder.compile(targetMethod).bind(implementationTarget, sourceMethod, terminationHandler, methodInvoker, assigner);
    assertThat(methodBinding.isValid(), is(false));
    verify(firstBinding).isValid();
    verify(secondBinding).isValid();
    verify(termination).isValid();
}
Also used : MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Example 2 with MethodDelegationBinder

use of net.bytebuddy.implementation.bind.MethodDelegationBinder in project byte-buddy by raphw.

the class TargetMethodAnnotationDrivenBinderTest method testNonAccessible.

@Test
public void testNonAccessible() throws Exception {
    when(targetMethod.isAccessibleTo(instrumentedType)).thenReturn(false);
    when(assignmentBinding.isValid()).thenReturn(true);
    when(methodInvocation.isValid()).thenReturn(true);
    when(termination.isValid()).thenReturn(true);
    when(targetMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(firstParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(secondParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    MethodDelegationBinder methodDelegationBinder = TargetMethodAnnotationDrivenBinder.of(Collections.<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>emptyList());
    assertThat(methodDelegationBinder.compile(targetMethod).bind(implementationTarget, sourceMethod, terminationHandler, methodInvoker, assigner).isValid(), is(false));
    verifyZeroInteractions(terminationHandler);
    verifyZeroInteractions(assigner);
    verifyZeroInteractions(methodInvoker);
}
Also used : MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Example 3 with MethodDelegationBinder

use of net.bytebuddy.implementation.bind.MethodDelegationBinder in project byte-buddy by raphw.

the class TargetMethodAnnotationDrivenBinderTest method testBindingByDefault.

@Test
@SuppressWarnings("unchecked")
public void testBindingByDefault() throws Exception {
    when(targetMethod.isAccessibleTo(instrumentedType)).thenReturn(true);
    when(assignmentBinding.isValid()).thenReturn(true);
    when(methodInvocation.isValid()).thenReturn(true);
    when(termination.isValid()).thenReturn(true);
    when(targetMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(firstParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(secondParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(firstParameter.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(secondParameter.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(sourceMethod.getParameters()).thenReturn(new ParameterList.Explicit(firstParameter, secondParameter));
    MethodDelegationBinder methodDelegationBinder = TargetMethodAnnotationDrivenBinder.of(Collections.<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>emptyList());
    MethodDelegationBinder.MethodBinding methodBinding = methodDelegationBinder.compile(targetMethod).bind(implementationTarget, sourceMethod, terminationHandler, methodInvoker, assigner);
    assertThat(methodBinding.isValid(), is(true));
    assertThat(methodBinding.getTarget(), is(targetMethod));
    assertThat(methodBinding.getTargetParameterIndex(new ArgumentTypeResolver.ParameterIndexToken(0)), is(0));
    assertThat(methodBinding.getTargetParameterIndex(new ArgumentTypeResolver.ParameterIndexToken(1)), is(1));
    StackManipulation.Size size = methodBinding.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(2));
    assertThat(size.getMaximalSize(), is(2));
    verify(firstParameter, atLeast(1)).getDeclaredAnnotations();
    verify(secondParameter, atLeast(1)).getDeclaredAnnotations();
    verify(targetMethod, atLeast(1)).getDeclaredAnnotations();
    verify(terminationHandler).resolve(assigner, typing, sourceMethod, targetMethod);
    verifyNoMoreInteractions(terminationHandler);
    verify(methodInvoker).invoke(targetMethod);
    verifyNoMoreInteractions(methodInvoker);
}
Also used : MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) ParameterList(net.bytebuddy.description.method.ParameterList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Example 4 with MethodDelegationBinder

use of net.bytebuddy.implementation.bind.MethodDelegationBinder in project byte-buddy by raphw.

the class TargetMethodAnnotationDrivenBinderTest method testIgnoreForBindingAnnotation.

@Test
public void testIgnoreForBindingAnnotation() throws Exception {
    when(targetMethod.isAccessibleTo(instrumentedType)).thenReturn(true);
    AnnotationDescription ignoreForBinding = mock(AnnotationDescription.class);
    when(ignoreForBinding.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(IgnoreForBinding.class));
    when(targetMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Collections.singletonList(ignoreForBinding)));
    when(termination.isValid()).thenReturn(true);
    MethodDelegationBinder methodDelegationBinder = TargetMethodAnnotationDrivenBinder.of(Collections.<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>emptyList());
    assertThat(methodDelegationBinder.compile(targetMethod).bind(implementationTarget, sourceMethod, terminationHandler, methodInvoker, assigner).isValid(), is(false));
    verifyZeroInteractions(assigner);
    verifyZeroInteractions(implementationTarget);
    verifyZeroInteractions(sourceMethod);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) TypeDescription(net.bytebuddy.description.type.TypeDescription) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Example 5 with MethodDelegationBinder

use of net.bytebuddy.implementation.bind.MethodDelegationBinder in project byte-buddy by raphw.

the class TargetMethodAnnotationDrivenBinderTest method testBindingByParameterAnnotations.

@Test
@SuppressWarnings("unchecked")
public void testBindingByParameterAnnotations() throws Exception {
    when(targetMethod.isAccessibleTo(instrumentedType)).thenReturn(true);
    when(assignmentBinding.isValid()).thenReturn(true);
    when(methodInvocation.isValid()).thenReturn(true);
    when(termination.isValid()).thenReturn(true);
    when(targetMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(firstParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Collections.singletonList(secondPseudoAnnotation)));
    when(secondParameter.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Collections.singletonList(firstPseudoAnnotation)));
    MethodDelegationBinder.ParameterBinding<?> firstBinding = prepareArgumentBinder(firstParameterBinder, FirstPseudoAnnotation.class, new Key(FOO));
    MethodDelegationBinder.ParameterBinding<?> secondBinding = prepareArgumentBinder(secondParameterBinder, SecondPseudoAnnotation.class, new Key(BAR));
    MethodDelegationBinder methodDelegationBinder = TargetMethodAnnotationDrivenBinder.of(Arrays.<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>asList(firstParameterBinder, secondParameterBinder));
    MethodDelegationBinder.MethodBinding methodBinding = methodDelegationBinder.compile(targetMethod).bind(implementationTarget, sourceMethod, terminationHandler, methodInvoker, assigner);
    assertThat(methodBinding.isValid(), is(true));
    assertThat(methodBinding.getTarget(), is(targetMethod));
    assertThat(methodBinding.getTargetParameterIndex(new Key(FOO)), is(1));
    assertThat(methodBinding.getTargetParameterIndex(new Key(BAR)), is(0));
    StackManipulation.Size size = methodBinding.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
    verifyZeroInteractions(methodVisitor);
    verify(targetMethod, atLeast(1)).getDeclaredAnnotations();
    verify(firstParameter, atLeast(1)).getDeclaredAnnotations();
    verify(secondParameter, atLeast(1)).getDeclaredAnnotations();
    verifyNoMoreInteractions(assigner);
    verify(terminationHandler).resolve(assigner, typing, sourceMethod, targetMethod);
    verifyNoMoreInteractions(terminationHandler);
    verify(methodInvoker).invoke(targetMethod);
    verifyNoMoreInteractions(methodInvoker);
    verify(firstParameterBinder, atLeast(1)).getHandledType();
    verify((TargetMethodAnnotationDrivenBinder.ParameterBinder) firstParameterBinder).bind(firstPseudoAnnotation, sourceMethod, secondParameter, implementationTarget, assigner, Assigner.Typing.STATIC);
    verifyNoMoreInteractions(firstParameterBinder);
    verify(secondParameterBinder, atLeast(1)).getHandledType();
    verify((TargetMethodAnnotationDrivenBinder.ParameterBinder) secondParameterBinder).bind(secondPseudoAnnotation, sourceMethod, firstParameter, implementationTarget, assigner, Assigner.Typing.STATIC);
    verifyNoMoreInteractions(secondParameterBinder);
    verify(firstBinding, atLeast(1)).isValid();
    verify(firstBinding).getIdentificationToken();
    verify(secondBinding, atLeast(1)).isValid();
    verify(secondBinding).getIdentificationToken();
}
Also used : MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Aggregations

AnnotationList (net.bytebuddy.description.annotation.AnnotationList)6 MethodDelegationBinder (net.bytebuddy.implementation.bind.MethodDelegationBinder)6 Test (org.junit.Test)6 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)2 AnnotationDescription (net.bytebuddy.description.annotation.AnnotationDescription)1 ParameterList (net.bytebuddy.description.method.ParameterList)1 TypeDescription (net.bytebuddy.description.type.TypeDescription)1