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