Search in sources :

Example 56 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription 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 57 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class GenericSignatureResolutionTest method testGenericMethodWithoutGenericExceptionTypes.

@Test
public void testGenericMethodWithoutGenericExceptionTypes() throws Exception {
    DynamicType.Unloaded<?> unloaded = new ByteBuddy().redefine(GenericMethod.class).method(named(BAR)).intercept(FixedValue.nullValue()).make();
    Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(BAR, Object.class));
    MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(BAR, Object.class));
    assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
    assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
    assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
    assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 58 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class GenericSignatureResolutionTest method testGenericMethod.

@Test
public void testGenericMethod() throws Exception {
    DynamicType.Unloaded<?> unloaded = new ByteBuddy().redefine(GenericMethod.class).method(named(FOO)).intercept(FixedValue.nullValue()).make();
    Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(FOO, Exception.class));
    MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(FOO, Exception.class));
    assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
    assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
    assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
    assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 59 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class AbstractTypeDescriptionGenericTest method testMethodTypeVariableIsRetained.

@Test
public void testMethodTypeVariableIsRetained() throws Exception {
    TypeDescription.Generic typeDescription = describeType(MemberVariable.class.getDeclaredField(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getTypeArguments().size(), is(2));
    assertThat(typeDescription.getTypeArguments().get(0).getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(typeDescription.getTypeArguments().get(0).asErasure().represents(Number.class), is(true));
    assertThat(typeDescription.getTypeArguments().get(1).getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(typeDescription.getTypeArguments().get(1).asErasure().represents(Integer.class), is(true));
    MethodDescription methodDescription = typeDescription.getDeclaredMethods().filter(named(FOO)).getOnly();
    assertThat(methodDescription.getReturnType().getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(methodDescription.getReturnType().getSymbol(), is("S"));
    assertThat(methodDescription.getReturnType().getTypeVariableSource(), is((TypeVariableSource) methodDescription.asDefined()));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeVariableSource(net.bytebuddy.description.TypeVariableSource) Test(org.junit.Test)

Example 60 with MethodDescription

use of net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class AbstractTypeDescriptionGenericTest method testMethodTypeVariableWithExtensionErasedBound.

@Test
public void testMethodTypeVariableWithExtensionErasedBound() throws Exception {
    TypeDescription.Generic typeDescription = describeType(MemberVariable.class.getDeclaredField(BAR)).getSuperClass();
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    MethodDescription methodDescription = typeDescription.getDeclaredMethods().filter(named(QUX)).getOnly();
    assertThat(methodDescription.getReturnType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(methodDescription.getReturnType().asErasure(), is(TypeDescription.OBJECT));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) Test(org.junit.Test)

Aggregations

MethodDescription (net.bytebuddy.description.method.MethodDescription)105 Test (org.junit.Test)102 TypeDescription (net.bytebuddy.description.type.TypeDescription)62 MethodVisitor (org.objectweb.asm.MethodVisitor)15 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)12 MethodList (net.bytebuddy.description.method.MethodList)9 Implementation (net.bytebuddy.implementation.Implementation)8 AbstractImplementationTargetTest (net.bytebuddy.implementation.AbstractImplementationTargetTest)6 Serializable (java.io.Serializable)4 TypeVariableSource (net.bytebuddy.description.TypeVariableSource)4 FieldList (net.bytebuddy.description.field.FieldList)4 TypePool (net.bytebuddy.pool.TypePool)4 ClassVisitor (org.objectweb.asm.ClassVisitor)4 ByteBuddy (net.bytebuddy.ByteBuddy)3 DynamicType (net.bytebuddy.dynamic.DynamicType)3 AnnotationDescription (net.bytebuddy.description.annotation.AnnotationDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 ByteCodeAppender (net.bytebuddy.implementation.bytecode.ByteCodeAppender)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1