Search in sources :

Example 1 with AnnotationDescription

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

the class AnnotationAppenderDefaultTest method makeTypeWithAnnotation.

private Class<?> makeTypeWithAnnotation(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.visitAnnotation(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 2 with AnnotationDescription

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

the class MethodAttributeAppenderForInstrumentedMethodTest method testJdkTypeIsFiltered.

@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);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test) AnnotationDescriptionBuilderTest(net.bytebuddy.description.annotation.AnnotationDescriptionBuilderTest)

Example 3 with AnnotationDescription

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

the class ElementMatchersTest method testNoneAnnotation.

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

Example 4 with AnnotationDescription

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

the class ElementMatchersTest method testAnnotationType.

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

Example 5 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription in project incubator-skywalking by apache.

the class ClassAnnotationMatch method isMatch.

@Override
public boolean isMatch(TypeDescription typeDescription) {
    List<String> annotationList = new ArrayList<String>(Arrays.asList(annotations));
    AnnotationList declaredAnnotations = typeDescription.getDeclaredAnnotations();
    for (AnnotationDescription annotation : declaredAnnotations) {
        annotationList.remove(annotation.getAnnotationType().getActualName());
    }
    if (annotationList.isEmpty()) {
        return true;
    }
    return false;
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) ArrayList(java.util.ArrayList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList)

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