Search in sources :

Example 21 with MethodDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class MethodGraphCompilerDefaultTest method testClassInheritance.

@Test
public void testClassInheritance() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(ClassBase.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
    MethodDescription method = typeDescription.getDeclaredMethods().filter(isMethod()).getOnly();
    MethodGraph.Node node = methodGraph.locate(method.asSignatureToken());
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(1));
    assertThat(node.getMethodTypes().contains(method.asTypeToken()), is(true));
    assertThat(node.getRepresentative(), is(method));
    assertThat(node.getVisibility(), is(method.getVisibility()));
    assertThat(methodGraph.listNodes().contains(node), is(true));
    MethodGraph.Node baseNode = methodGraph.getSuperClassGraph().locate(method.asSignatureToken());
    assertThat(node, not(baseNode));
    assertThat(typeDescription.getSuperClass().getDeclaredMethods().filter(ElementMatchers.is(baseNode.getRepresentative())).getOnly(), is(baseNode.getRepresentative()));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 22 with MethodDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class MethodGraphCompilerForDeclaredMethodsTest method testCompilationNonBridge.

@Test
public void testCompilationNonBridge() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(methodDescription));
    when(methodDescription.isVirtual()).thenReturn(true);
    when(methodDescription.getModifiers()).thenReturn(Opcodes.ACC_BRIDGE);
    when(methodDescription.isVisibleTo(typeDescription)).thenReturn(true);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.ForDeclaredMethods.INSTANCE.compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(0));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) Test(org.junit.Test)

Example 23 with MethodDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class ImplementationContextDefaultTest method testFieldGetterRegistrationWritesFirst.

@Test
public void testFieldGetterRegistrationWritesFirst() throws Exception {
    Implementation.Context.Default implementationContext = new Implementation.Context.Default(instrumentedType, classFileVersion, auxiliaryTypeNamingStrategy, typeInitializer, auxiliaryClassFileVersion);
    MethodDescription firstMethodDescription = implementationContext.registerGetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT);
    assertThat(implementationContext.registerGetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT), is(firstMethodDescription));
    implementationContext.drain(drain, classVisitor, annotationValueFilterFactory);
    verify(classVisitor).visitMethod(eq(accessorMethodModifiers), Matchers.startsWith(FOO), eq("()" + BAR), isNull(String.class), isNull(String[].class));
    verify(methodVisitor).visitCode();
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verify(methodVisitor).visitFieldInsn(Opcodes.GETFIELD, QUX, FOO, BAR);
    verify(methodVisitor).visitInsn(Opcodes.ARETURN);
    verify(methodVisitor).visitMaxs(1, 1);
    verify(methodVisitor).visitEnd();
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) Test(org.junit.Test)

Example 24 with MethodDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class ImplementationContextDefaultTest method testFieldSetterRegistrationWritesSecond.

@Test
public void testFieldSetterRegistrationWritesSecond() throws Exception {
    when(secondField.isStatic()).thenReturn(true);
    Implementation.Context.Default implementationContext = new Implementation.Context.Default(instrumentedType, classFileVersion, auxiliaryTypeNamingStrategy, typeInitializer, auxiliaryClassFileVersion);
    MethodDescription secondMethodDescription = implementationContext.registerSetterFor(secondField, MethodAccessorFactory.AccessType.DEFAULT);
    assertThat(implementationContext.registerSetterFor(secondField, MethodAccessorFactory.AccessType.DEFAULT), is(secondMethodDescription));
    implementationContext.drain(drain, classVisitor, annotationValueFilterFactory);
    verify(classVisitor).visitMethod(eq(accessorMethodModifiers | Opcodes.ACC_STATIC), Matchers.startsWith(BAR), eq("(" + QUX + ")V"), isNull(String.class), isNull(String[].class));
    verify(methodVisitor).visitCode();
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verify(methodVisitor).visitFieldInsn(Opcodes.PUTSTATIC, BAZ, BAR, FOO);
    verify(methodVisitor).visitInsn(Opcodes.RETURN);
    verify(methodVisitor).visitMaxs(1, 0);
    verify(methodVisitor).visitEnd();
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) Test(org.junit.Test)

Example 25 with MethodDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription in project byte-buddy by raphw.

the class ImplementationContextDefaultTest method testFieldSetterRegistrationWritesFirst.

@Test
public void testFieldSetterRegistrationWritesFirst() throws Exception {
    Implementation.Context.Default implementationContext = new Implementation.Context.Default(instrumentedType, classFileVersion, auxiliaryTypeNamingStrategy, typeInitializer, auxiliaryClassFileVersion);
    MethodDescription firstMethodDescription = implementationContext.registerSetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT);
    assertThat(implementationContext.registerSetterFor(firstField, MethodAccessorFactory.AccessType.DEFAULT), is(firstMethodDescription));
    implementationContext.drain(drain, classVisitor, annotationValueFilterFactory);
    verify(classVisitor).visitMethod(eq(accessorMethodModifiers), Matchers.startsWith(FOO), eq("(" + BAR + ")V"), isNull(String.class), isNull(String[].class));
    verify(methodVisitor).visitCode();
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, QUX, FOO, BAR);
    verify(methodVisitor).visitInsn(Opcodes.RETURN);
    verify(methodVisitor).visitMaxs(2, 1);
    verify(methodVisitor).visitEnd();
}
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