net.bytebuddy.description.annotation.AnnotationList Java Examples

The following examples show how to use net.bytebuddy.description.annotation.AnnotationList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeWriterRecordComponentPoolRecordTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(recordComponentDescription.getActualName()).thenReturn(FOO);
    when(recordComponentDescription.getDescriptor()).thenReturn(BAR);
    when(recordComponentDescription.getGenericSignature()).thenReturn(QUX);
    when(recordComponentDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription));
    when(recordComponentDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(classVisitor.visitRecordComponent(FOO, BAR, QUX)).thenReturn(recordComponentVisitor);
    when(classVisitor.visitRecordComponent(FOO, BAR, QUX)).thenReturn(recordComponentVisitor);
    when(annotationValueFilterFactory.on(recordComponentDescription)).thenReturn(valueFilter);
    when(recordComponentVisitor.visitAnnotation(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
    when(annotationDescription.getAnnotationType()).thenReturn(annotationType);
    when(annotationType.getDescriptor()).thenReturn(BAZ);
    when(annotationType.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.RUNTIME);
}
 
Example #2
Source File: TypeAttributeAppenderForInstrumentedTypeTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuperClassTypeAnnotationClassFileRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(simpleAnnotatedType.getDeclaredAnnotations())
            .thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(instrumentedType.getSuperClass()).thenReturn(simpleAnnotatedType);
    TypeAttributeAppender.ForInstrumentedType.INSTANCE.apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(), null, Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getSuperClass();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #3
Source File: MethodAttributeAppenderExplicitTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testOfParameterAnnotations() throws Exception {
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    MethodAttributeAppender methodAttributeAppender = MethodAttributeAppender.Explicit.ofParameterAnnotations(methodDescription).make(instrumentedType);
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor, times(0)).visitAnnotation(anyString(), anyBoolean());
    verify(methodVisitor).visitParameterAnnotation(PARAMETER_INDEX, Type.getDescriptor(Baz.class), true);
    verifyNoMoreInteractions(methodVisitor);
    verify(methodDescription, times(2)).getParameters();
    verify(methodDescription, times(0)).getDeclaredAnnotations();
    verifyNoMoreInteractions(methodDescription);
    verify(parameterDescription).getIndex();
    verify(parameterDescription).getDeclaredAnnotations();
    verifyNoMoreInteractions(parameterDescription);
    verifyZeroInteractions(instrumentedType);
}
 
Example #4
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testJdkTypeIsFiltered() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    TypeDescription annotationType = mock(TypeDescription.class);
    when(annotationType.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.RUNTIME);
    when(annotationDescription.getAnnotationType()).thenReturn(annotationType);
    when(annotationType.getActualName()).thenReturn("jdk.internal.Sample");
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
 
Example #5
Source File: AgentBuilderInitializationStrategyTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMinimalRegistrationIndependentType() throws Exception {
    Annotation eagerAnnotation = mock(AuxiliaryType.SignatureRelevant.class);
    when(eagerAnnotation.annotationType()).thenReturn((Class) AuxiliaryType.SignatureRelevant.class);
    TypeDescription independent = mock(TypeDescription.class), dependent = mock(TypeDescription.class);
    when(independent.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(eagerAnnotation));
    when(dependent.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    Map<TypeDescription, byte[]> map = new HashMap<TypeDescription, byte[]>();
    map.put(independent, QUX);
    map.put(dependent, BAZ);
    when(dynamicType.getAuxiliaryTypes()).thenReturn(map);
    ClassInjector classInjector = mock(ClassInjector.class);
    when(injectionStrategy.resolve(classLoader, protectionDomain)).thenReturn(classInjector);
    when(classInjector.inject(Collections.singletonMap(independent, QUX)))
            .thenReturn(Collections.<TypeDescription, Class<?>>singletonMap(independent, Foo.class));
    LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class);
    when(dynamicType.getLoadedTypeInitializers()).thenReturn(Collections.singletonMap(independent, loadedTypeInitializer));
    AgentBuilder.InitializationStrategy.Minimal.INSTANCE.register(dynamicType, classLoader, protectionDomain, injectionStrategy);
    verify(classInjector).inject(Collections.singletonMap(independent, QUX));
    verifyNoMoreInteractions(classInjector);
    verify(loadedTypeInitializer).onLoad(Foo.class);
    verifyNoMoreInteractions(loadedTypeInitializer);
}
 
Example #6
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testTypeVariableTypeAnnotationRuntimeRetention() throws Exception {
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(annotatedTypeVariableBound.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Explicit(annotatedTypeVariable));
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeParameterReference(TypeReference.METHOD_TYPE_PARAMETER, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.METHOD_TYPE_PARAMETER_BOUND, 0, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyZeroInteractions(methodVisitor);
}
 
Example #7
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodReturnTypeTypeAnnotationRuntimeRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(simpleAnnotatedType);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.METHOD_RETURN).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #8
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetterNameDiscovery() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FieldValue.Binder.Delegate.BEAN_PROPERTY);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getInternalName()).thenReturn("setFoo");
    when(source.getActualName()).thenReturn("setFoo");
    when(source.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(source.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(source, TypeDescription.Generic.OBJECT));
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #9
Source File: MethodAttributeAppenderForInstrumentedMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReceiverTypeTypeAnnotationsNoRetention() throws Exception {
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getReceiverType()).thenReturn(simpleAnnotatedType);
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
    MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
    verify(methodDescription).getDeclaredAnnotations();
    verify(methodDescription).getParameters();
    verify(methodDescription).getReturnType();
    verify(methodDescription).getExceptionTypes();
    verify(methodDescription).getTypeVariables();
    verify(methodDescription).getReceiverType();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #10
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalAssignmentExplicitTypeStaticMethod() throws Exception {
    doReturn(Foo.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(source.isStatic()).thenReturn(true);
    when(stackManipulation.isValid()).thenReturn(true);
    when(instrumentedType.isAssignableTo(TypeDescription.ForLoadedType.of(Foo.class))).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #11
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalAssignmentStaticMethod() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.isStatic()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #12
Source File: MethodAttributeAppenderForInstrumentedMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReceiverTypeTypeAnnotationsRuntimeRetention() throws Exception {
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getReceiverType()).thenReturn(simpleAnnotatedType);
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.METHOD_RECEIVER).getValue(),
            null,
            Type.getDescriptor(AbstractAttributeAppenderTest.Baz.class),
            true);
    verifyNoMoreInteractions(methodVisitor);
    verify(methodDescription).getDeclaredAnnotations();
    verify(methodDescription).getParameters();
    verify(methodDescription).getReturnType();
    verify(methodDescription).getExceptionTypes();
    verify(methodDescription).getTypeVariables();
    verify(methodDescription).getReceiverType();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #13
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterAnnotationClassFileRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    ParameterDescription parameterDescription = mock(ParameterDescription.class);
    when(parameterDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new QuxBaz.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitParameterAnnotation(0, Type.getDescriptor(QuxBaz.class), false);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #14
Source File: TypeAttributeAppenderForInstrumentedTypeDifferentiatingTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeVariableTypeAnnotationRuntimeRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Explicit(pseudoType, annotatedTypeVariable));
    when(annotatedTypeVariable.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(annotatedTypeVariableBound.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    new TypeAttributeAppender.ForInstrumentedType.Differentiating(0, 1, 0).apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterReference(TypeReference.CLASS_TYPE_PARAMETER, 1).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newTypeParameterBoundReference(TypeReference.CLASS_TYPE_PARAMETER_BOUND, 1, 0).getValue(),
            null,
            Type.getDescriptor(Baz.class),
            true);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #15
Source File: TargetMethodAnnotationDrivenBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #16
Source File: HashCodeAndEqualsPluginTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationComparatorEqualsLeftBiggerAnnotations() {
    Comparator<FieldDescription.InDefinedShape> comparator = HashCodeAndEqualsPlugin.AnnotationOrderComparator.INSTANCE;
    FieldDescription.InDefinedShape left = mock(FieldDescription.InDefinedShape.class), right = mock(FieldDescription.InDefinedShape.class);
    when(left.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 42)
            .build()));
    when(right.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 0)
            .build()));
    assertThat(comparator.compare(left, right), is(-1));
}
 
Example #17
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodReturnTypeTypeAnnotationNoRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(simpleAnnotatedType);
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
 
Example #18
Source File: MethodAttributeAppenderForInstrumentedMethodTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMethodParameterAnnotationNoRetention() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    ParameterDescription parameterDescription = mock(ParameterDescription.class);
    when(parameterDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Qux.Instance()));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
 
Example #19
Source File: MethodAttributeAppenderExplicitTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationAppenderForParameterRuntimeRetention() throws Exception {
    new MethodAttributeAppender.Explicit(PARAMETER_INDEX, new AnnotationList.ForLoadedAnnotations(new Baz.Instance()))
            .apply(methodVisitor, methodDescription, annotationValueFilter);
    verify(methodVisitor).visitParameterAnnotation(PARAMETER_INDEX, Type.getDescriptor(Baz.class), true);
    verifyNoMoreInteractions(methodVisitor);
    verify(methodDescription).getParameters();
    verifyNoMoreInteractions(methodDescription);
}
 
Example #20
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testIllegalAssignmentExplicitTypeNoField() throws Exception {
    doReturn(Foo.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(BAR);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(instrumentedType.isAssignableTo(TypeDescription.ForLoadedType.of(Foo.class))).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #21
Source File: HashCodeAndEqualsPluginTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationComparatorEqualsRightBiggerAnnotations() {
    Comparator<FieldDescription.InDefinedShape> comparator = HashCodeAndEqualsPlugin.AnnotationOrderComparator.INSTANCE;
    FieldDescription.InDefinedShape left = mock(FieldDescription.InDefinedShape.class), right = mock(FieldDescription.InDefinedShape.class);
    when(left.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 0)
            .build()));
    when(right.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 42)
            .build()));
    assertThat(comparator.compare(left, right), is(1));
}
 
Example #22
Source File: AllArgumentsBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testLegalStrictBindingNoRuntimeType() throws Exception {
    when(target.getIndex()).thenReturn(1);
    RuntimeType runtimeType = mock(RuntimeType.class);
    doReturn(RuntimeType.class).when(runtimeType).annotationType();
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(runtimeType));
    testLegalStrictBinding(Assigner.Typing.DYNAMIC);
}
 
Example #23
Source File: MethodDescriptionLatentTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeInitializer() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    MethodDescription.InDefinedShape typeInitializer = new MethodDescription.Latent.TypeInitializer(typeDescription);
    assertThat(typeInitializer.getDeclaringType(), is(typeDescription));
    assertThat(typeInitializer.getReturnType(), is(TypeDescription.Generic.VOID));
    assertThat(typeInitializer.getParameters(), is((ParameterList<ParameterDescription.InDefinedShape>) new ParameterList.Empty<ParameterDescription.InDefinedShape>()));
    assertThat(typeInitializer.getExceptionTypes(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(typeInitializer.getDeclaredAnnotations(), is((AnnotationList) new AnnotationList.Empty()));
    assertThat(typeInitializer.getModifiers(), is(MethodDescription.TYPE_INITIALIZER_MODIFIER));
}
 
Example #24
Source File: TransformerForFieldTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(fieldType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(fieldType);
    when(fieldDescription.asToken(matchesPrototype(none()))).thenReturn(fieldToken);
    when(fieldDescription.getDeclaringType()).thenReturn(declaringType);
    when(fieldDescription.asDefined()).thenReturn(definedField);
    when(fieldToken.getName()).thenReturn(FOO);
    when(fieldToken.getModifiers()).thenReturn(MODIFIERS);
    when(fieldToken.getType()).thenReturn(fieldType);
    when(fieldToken.getAnnotations()).thenReturn(new AnnotationList.Explicit(fieldAnnotation));
    when(modifierContributor.getMask()).thenReturn(MASK);
    when(modifierContributor.getRange()).thenReturn(RANGE);
}
 
Example #25
Source File: AbstractMethodDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotations() throws Exception {
    assertThat(describe(firstMethod).getDeclaredAnnotations(),
            is((AnnotationList) new AnnotationList.Empty()));
    assertThat(describe(secondMethod).getDeclaredAnnotations(),
            is((AnnotationList) new AnnotationList.Empty()));
    assertThat(describe(thirdMethod).getDeclaredAnnotations(),
            is((AnnotationList) new AnnotationList.ForLoadedAnnotations(thirdMethod.getDeclaredAnnotations())));
    assertThat(describe(firstConstructor).getDeclaredAnnotations(), is((AnnotationList) new AnnotationList.Empty()));
    assertThat(describe(secondConstructor).getDeclaredAnnotations(),
            is((AnnotationList) new AnnotationList.ForLoadedAnnotations(secondConstructor.getDeclaredAnnotations())));
}
 
Example #26
Source File: RuntimeTypeVerifierTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckElementValid() throws Exception {
    when(annotationSource.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(runtimeType));
    assertThat(RuntimeType.Verifier.check(annotationSource), is(Assigner.Typing.DYNAMIC));
    verify(annotationSource).getDeclaredAnnotations();
    verifyNoMoreInteractions(annotationSource);
}
 
Example #27
Source File: TypeAttributeAppenderForInstrumentedTypeDifferentiatingTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationClassFileRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance(), new QuxBaz.Instance()));
    new TypeAttributeAppender.ForInstrumentedType.Differentiating(1, 0, 0).apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitAnnotation(Type.getDescriptor(QuxBaz.class), false);
    verifyZeroInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #28
Source File: FieldAttributeAppenderForInstrumentedFieldTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationAppenderRuntimeRetention() throws Exception {
    when(fieldDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    FieldAttributeAppender.ForInstrumentedField.INSTANCE.apply(fieldVisitor, fieldDescription, annotationValueFilter);
    verify(fieldVisitor).visitAnnotation(Type.getDescriptor(Baz.class), true);
    verifyNoMoreInteractions(fieldVisitor);
    verify(fieldDescription).getDeclaredAnnotations();
    verify(fieldDescription).getType();
    verifyNoMoreInteractions(fieldDescription);
}
 
Example #29
Source File: TypeAttributeAppenderForInstrumentedTypeDifferentiatingTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterfaceTypeAnnotationRuntimeRetention() throws Exception {
    when(instrumentedType.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(instrumentedType.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(pseudoType, simpleAnnotatedType));
    when(simpleAnnotatedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(new Baz.Instance()));
    when(instrumentedType.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    new TypeAttributeAppender.ForInstrumentedType.Differentiating(0, 0, 1).apply(classVisitor, instrumentedType, annotationValueFilter);
    verify(classVisitor).visitTypeAnnotation(TypeReference.newSuperTypeReference(1).getValue(), null, Type.getDescriptor(Baz.class), true);
    verifyNoMoreInteractions(classVisitor);
    verify(instrumentedType).getDeclaredAnnotations();
    verify(instrumentedType).getInterfaces();
    verify(instrumentedType).getTypeVariables();
    verifyNoMoreInteractions(instrumentedType);
}
 
Example #30
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testLegalAssignmentExplicitType() throws Exception {
    doReturn(Foo.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(instrumentedType.isAssignableTo(TypeDescription.ForLoadedType.of(Foo.class))).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}