Search in sources :

Example 11 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project byte-buddy by raphw.

the class AnnotationAppenderDefaultTest method makeTypeWithSuperClassAnnotation.

private Class<?> makeTypeWithSuperClassAnnotation(Annotation annotation) throws Exception {
    when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
    classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(), Opcodes.ACC_PUBLIC, BAR.replace('.', '/'), null, Type.getInternalName(Object.class), null);
    AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(), null, Type.getDescriptor(annotation.annotationType()), true);
    when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
    AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
    annotationAppender.append(annotationDescription, valueFilter);
    classWriter.visitEnd();
    Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR);
    assertThat(bar.getName(), is(BAR));
    assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
    return bar;
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader)

Example 12 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project byte-buddy by raphw.

the class AnnotationAppenderDefaultTest method testSourceRetentionAnnotation.

@Test
public void testSourceRetentionAnnotation() throws Exception {
    AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class);
    when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor);
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.SOURCE);
    annotationAppender.append(annotationDescription, valueFilter);
    verifyZeroInteractions(valueFilter);
    verifyZeroInteractions(annotationVisitor);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) Test(org.junit.Test)

Example 13 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project byte-buddy by raphw.

the class AnnotationAppenderDefaultTest method testSourceRetentionTypeAnnotation.

@Test
public void testSourceRetentionTypeAnnotation() throws Exception {
    AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class);
    when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor);
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.SOURCE);
    annotationAppender.append(annotationDescription, valueFilter, 0, null);
    verifyZeroInteractions(valueFilter);
    verifyZeroInteractions(annotationVisitor);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) Test(org.junit.Test)

Example 14 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project byte-buddy by raphw.

the class AnnotationValueFilterDefaultTest method testAppendsDefaults.

@Test
public void testAppendsDefaults() throws Exception {
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    assertThat(AnnotationValueFilter.Default.APPEND_DEFAULTS.isRelevant(annotationDescription, methodDescription), is(true));
    verifyZeroInteractions(annotationDescription);
    verifyZeroInteractions(methodDescription);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) MethodDescription(net.bytebuddy.description.method.MethodDescription) Test(org.junit.Test)

Example 15 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project byte-buddy by raphw.

the class ElementMatchersTest method testAnyOfAnnotation.

@Test
public void testAnyOfAnnotation() throws Exception {
    AnnotationDescription annotationDescription = new TypeDescription.ForLoadedType(IsAnnotatedWith.class).getDeclaredAnnotations().ofType(IsAnnotatedWithAnnotation.class);
    assertThat(ElementMatchers.anyOf(IsAnnotatedWith.class.getAnnotation(IsAnnotatedWithAnnotation.class)).matches(annotationDescription), is(true));
    assertThat(ElementMatchers.anyOf(IsAnnotatedWith.class.getAnnotation(IsAnnotatedWithAnnotation.class), Other.class.getAnnotation(OtherAnnotation.class)).matches(annotationDescription), is(true));
    assertThat(ElementMatchers.anyOf(Other.class.getAnnotation(OtherAnnotation.class)).matches(annotationDescription), is(false));
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) Test(org.junit.Test)

Aggregations

AnnotationDescription (net.bytebuddy.description.annotation.AnnotationDescription)16 Test (org.junit.Test)12 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)5 TypeDescription (net.bytebuddy.description.type.TypeDescription)4 ArrayList (java.util.ArrayList)2 MethodDescription (net.bytebuddy.description.method.MethodDescription)2 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 AnnotationDescriptionBuilderTest (net.bytebuddy.description.annotation.AnnotationDescriptionBuilderTest)1 AnnotationValue (net.bytebuddy.description.annotation.AnnotationValue)1 EnumerationDescription (net.bytebuddy.description.enumeration.EnumerationDescription)1 MethodList (net.bytebuddy.description.method.MethodList)1 MethodDelegationBinder (net.bytebuddy.implementation.bind.MethodDelegationBinder)1