|
| 1 | +package net.bytebuddy.implementation.bind.annotation; |
| 2 | + |
| 3 | +import net.bytebuddy.description.type.TypeDescription; |
| 4 | +import net.bytebuddy.description.type.TypeList; |
| 5 | +import net.bytebuddy.implementation.bind.MethodDelegationBinder; |
| 6 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
| 7 | +import net.bytebuddy.utility.JavaConstant; |
| 8 | +import net.bytebuddy.utility.JavaType; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.mockito.Mock; |
| 12 | + |
| 13 | +import static org.hamcrest.CoreMatchers.is; |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.mockito.Mockito.doReturn; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +public class DynamicConstantBinderTest extends AbstractAnnotationBinderTest<DynamicConstant> { |
| 19 | + |
| 20 | + private static final String FOO = "foo", BAR = "bar"; |
| 21 | + |
| 22 | + @Mock |
| 23 | + private TypeDescription targetType; |
| 24 | + |
| 25 | + @Mock |
| 26 | + private TypeDescription.Generic typeDescription; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private TypeList.Generic interfaces; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private TypeList rawInterfaces; |
| 33 | + |
| 34 | + public DynamicConstantBinderTest() { |
| 35 | + super(DynamicConstant.class); |
| 36 | + } |
| 37 | + |
| 38 | + @Before |
| 39 | + @Override |
| 40 | + public void setUp() throws Exception { |
| 41 | + super.setUp(); |
| 42 | + when(target.getType()).thenReturn(typeDescription); |
| 43 | + when(typeDescription.asErasure()).thenReturn(targetType); |
| 44 | + when(instrumentedType.getInterfaces()).thenReturn(interfaces); |
| 45 | + when(interfaces.asErasures()).thenReturn(rawInterfaces); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected TargetMethodAnnotationDrivenBinder.ParameterBinder<DynamicConstant> getSimpleBinder() { |
| 50 | + return DynamicConstant.Binder.INSTANCE; |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testDynamicConstant() throws Exception { |
| 55 | + doReturn(void.class).when(annotation).bootstrapReturnType(); |
| 56 | + doReturn(void.class).when(annotation).bootstrapOwner(); |
| 57 | + doReturn(new Class<?>[] {Object.class}).when(annotation).bootstrapParameterTypes(); |
| 58 | + when(annotation.invokedynamic()).thenReturn(false); |
| 59 | + when(annotation.name()).thenReturn(FOO); |
| 60 | + when(annotation.bootstrapName()).thenReturn(BAR); |
| 61 | + when(annotation.bootstrapType()).thenReturn(JavaConstant.MethodHandle.HandleType.INVOKE_STATIC); |
| 62 | + when(targetType.isAssignableFrom(JavaType.METHOD_HANDLE.getTypeStub())).thenReturn(true); |
| 63 | + MethodDelegationBinder.ParameterBinding<?> parameterBinding = DynamicConstant.Binder.INSTANCE |
| 64 | + .bind(annotationDescription, source, target, implementationTarget, assigner, Assigner.Typing.STATIC); |
| 65 | + assertThat(parameterBinding.isValid(), is(true)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testDynamicConstantInvokedynamic() throws Exception { |
| 70 | + doReturn(void.class).when(annotation).bootstrapReturnType(); |
| 71 | + doReturn(void.class).when(annotation).bootstrapOwner(); |
| 72 | + doReturn(new Class<?>[] {Object.class}).when(annotation).bootstrapParameterTypes(); |
| 73 | + when(annotation.invokedynamic()).thenReturn(true); |
| 74 | + when(annotation.name()).thenReturn(FOO); |
| 75 | + when(annotation.bootstrapName()).thenReturn(BAR); |
| 76 | + when(annotation.bootstrapType()).thenReturn(JavaConstant.MethodHandle.HandleType.INVOKE_STATIC); |
| 77 | + when(targetType.isAssignableFrom(JavaType.METHOD_HANDLE.getTypeStub())).thenReturn(true); |
| 78 | + MethodDelegationBinder.ParameterBinding<?> parameterBinding = DynamicConstant.Binder.INSTANCE |
| 79 | + .bind(annotationDescription, source, target, implementationTarget, assigner, Assigner.Typing.STATIC); |
| 80 | + assertThat(parameterBinding.isValid(), is(true)); |
| 81 | + } |
| 82 | +} |
0 commit comments