Search in sources :

Example 6 with AnnotationDescription

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

the class MethodAnnotationMatch method isMatch.

@Override
public boolean isMatch(TypeDescription typeDescription) {
    for (MethodDescription.InDefinedShape methodDescription : typeDescription.getDeclaredMethods()) {
        List<String> annotationList = new ArrayList<String>(Arrays.asList(annotations));
        AnnotationList declaredAnnotations = methodDescription.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) MethodDescription(net.bytebuddy.description.method.MethodDescription) ArrayList(java.util.ArrayList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList)

Example 7 with AnnotationDescription

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

the class AbstractMethodDescriptionTest method testIsDefault.

@Test
@SuppressWarnings("unchecked")
public void testIsDefault() throws Exception {
    Map<String, AnnotationValue<?, ?>> properties = new LinkedHashMap<String, AnnotationValue<?, ?>>();
    properties.put("boolean_property", AnnotationValue.ForConstant.of(true));
    properties.put("boolean_property_array", AnnotationValue.ForConstant.of(new boolean[] { true }));
    properties.put("byte_property", AnnotationValue.ForConstant.of((byte) 0));
    properties.put("byte_property_array", AnnotationValue.ForConstant.of(new byte[] { 0 }));
    properties.put("short_property", AnnotationValue.ForConstant.of((short) 0));
    properties.put("short_property_array", AnnotationValue.ForConstant.of(new short[] { 0 }));
    properties.put("int_property", AnnotationValue.ForConstant.of(0));
    properties.put("int_property_array", AnnotationValue.ForConstant.of(new int[] { 0 }));
    properties.put("long_property", AnnotationValue.ForConstant.of(0L));
    properties.put("long_property_array", AnnotationValue.ForConstant.of(new long[] { 0 }));
    properties.put("float_property", AnnotationValue.ForConstant.of(0f));
    properties.put("float_property_array", AnnotationValue.ForConstant.of(new float[] { 0 }));
    properties.put("double_property", AnnotationValue.ForConstant.of(0d));
    properties.put("double_property_array", AnnotationValue.ForConstant.of(new double[] { 0d }));
    properties.put("string_property", AnnotationValue.ForConstant.of("foo"));
    properties.put("string_property_array", AnnotationValue.ForConstant.of(new String[] { "foo" }));
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    when(annotationDescription.getAnnotationType()).thenReturn(new TypeDescription.ForLoadedType(SampleAnnotation.class));
    properties.put("annotation_property", new AnnotationValue.ForAnnotationDescription(annotationDescription));
    properties.put("annotation_property_array", AnnotationValue.ForDescriptionArray.of(new TypeDescription.ForLoadedType(SampleAnnotation.class), new AnnotationDescription[] { annotationDescription }));
    EnumerationDescription enumerationDescription = mock(EnumerationDescription.class);
    when(enumerationDescription.getEnumerationType()).thenReturn(new TypeDescription.ForLoadedType(SampleEnumeration.class));
    properties.put("enum_property", AnnotationValue.ForEnumerationDescription.<Enum>of(enumerationDescription));
    properties.put("enum_property_array", AnnotationValue.ForDescriptionArray.<Enum>of(new TypeDescription.ForLoadedType(SampleEnumeration.class), new EnumerationDescription[] { enumerationDescription }));
    MethodList<?> methods = new TypeDescription.ForLoadedType(AnnotationValues.class).getDeclaredMethods();
    for (Map.Entry<String, AnnotationValue<?, ?>> entry : properties.entrySet()) {
        assertThat(methods.filter(named(entry.getKey())).getOnly().isDefaultValue(entry.getValue()), is(true));
        assertThat(methods.filter(named(entry.getKey())).getOnly().isDefaultValue(mock(AnnotationValue.class)), is(false));
    }
    when(annotationDescription.getAnnotationType()).thenReturn(TypeDescription.OBJECT);
    assertThat(methods.filter(named("annotation_property")).getOnly().isDefaultValue(new AnnotationValue.ForAnnotationDescription(annotationDescription)), is(false));
    assertThat(methods.filter(named("annotation_property_array")).getOnly().isDefaultValue(AnnotationValue.ForDescriptionArray.of(new TypeDescription.ForLoadedType(Object.class), new AnnotationDescription[] { annotationDescription })), is(false));
    when(enumerationDescription.getEnumerationType()).thenReturn(TypeDescription.OBJECT);
    assertThat(methods.filter(named("enum_property")).getOnly().isDefaultValue(AnnotationValue.ForEnumerationDescription.<Enum>of(enumerationDescription)), is(false));
    assertThat(methods.filter(named("enum_property_array")).getOnly().isDefaultValue(AnnotationValue.ForDescriptionArray.<Enum>of(new TypeDescription.ForLoadedType(Object.class), new EnumerationDescription[] { enumerationDescription })), is(false));
}
Also used : LinkedHashMap(java.util.LinkedHashMap) AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) EnumerationDescription(net.bytebuddy.description.enumeration.EnumerationDescription) AnnotationValue(net.bytebuddy.description.annotation.AnnotationValue) TypeDescription(net.bytebuddy.description.type.TypeDescription) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with AnnotationDescription

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

the class InstrumentedTypeDefaultTest method testWithAnnotation.

@Test
public void testWithAnnotation() throws Exception {
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    InstrumentedType instrumentedType = makePlainInstrumentedType();
    assertThat(instrumentedType.getDeclaredAnnotations().size(), is(0));
    instrumentedType = instrumentedType.withAnnotations(Collections.singletonList(annotationDescription));
    assertThat(instrumentedType.getDeclaredAnnotations(), is(Collections.singletonList(annotationDescription)));
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) Test(org.junit.Test)

Example 9 with AnnotationDescription

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

the class TypeAttributeAppenderForInstrumentedTypeDifferentiatingTest method testObjectProperties.

@Test
public void testObjectProperties() throws Exception {
    ObjectPropertyAssertion.of(TypeAttributeAppender.ForInstrumentedType.Differentiating.class).refine(new ObjectPropertyAssertion.Refinement<TypeDescription>() {

        @Override
        public void apply(TypeDescription mock) {
            AnnotationDescription[] annotationDescription = new AnnotationDescription[new Random().nextInt(10000)];
            when(mock.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(Arrays.asList(annotationDescription)));
            when(mock.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
            when(mock.getInterfaces()).thenReturn(new TypeList.Generic.Empty());
        }
    }).apply();
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) Random(java.util.Random) TypeDescription(net.bytebuddy.description.type.TypeDescription) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test)

Example 10 with AnnotationDescription

use of net.bytebuddy.description.annotation.AnnotationDescription 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);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) MethodDelegationBinder(net.bytebuddy.implementation.bind.MethodDelegationBinder) TypeDescription(net.bytebuddy.description.type.TypeDescription) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) 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