Search in sources :

Example 31 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 TypeConstantAdjustmentTest method testInstrumentationModernClassFile.

@Test
public void testInstrumentationModernClassFile() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class), this.classVisitor, mock(Implementation.Context.class), mock(TypePool.class), new FieldList.Empty<FieldDescription.InDefinedShape>(), new MethodList.Empty<MethodDescription>(), IGNORED, IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    assertThat(classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ }), is(methodVisitor));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    verifyNoMoreInteractions(this.classVisitor);
    verifyZeroInteractions(methodVisitor);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) ClassVisitor(org.objectweb.asm.ClassVisitor) TypePool(net.bytebuddy.pool.TypePool) FieldList(net.bytebuddy.description.field.FieldList) Test(org.junit.Test)

Example 32 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 TypeConstantAdjustmentTest method testInstrumentationLegacyClassFileArrayType.

@Test
public void testInstrumentationLegacyClassFileArrayType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class), this.classVisitor, mock(Implementation.Context.class), mock(TypePool.class), new FieldList.Empty<FieldDescription.InDefinedShape>(), new MethodList.Empty<MethodDescription>(), IGNORED, IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(Type.getType(Object[].class));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(Type.getType(Object[].class).getInternalName().replace('/', '.'));
    verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(Class.class).getInternalName(), "forName", Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(), false);
    verifyNoMoreInteractions(this.methodVisitor);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) MethodList(net.bytebuddy.description.method.MethodList) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldList(net.bytebuddy.description.field.FieldList) MethodVisitor(org.objectweb.asm.MethodVisitor) TypeDescription(net.bytebuddy.description.type.TypeDescription) TypePool(net.bytebuddy.pool.TypePool) Test(org.junit.Test)

Example 33 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 InstrumentedTypeDefaultTest method testWithTypeInitializerDouble.

@Test
public void testWithTypeInitializerDouble() throws Exception {
    InstrumentedType instrumentedType = makePlainInstrumentedType();
    assertThat(instrumentedType.getDeclaredFields().size(), is(0));
    ByteCodeAppender first = mock(ByteCodeAppender.class), second = mock(ByteCodeAppender.class);
    MethodDescription methodDescription = mock(MethodDescription.class);
    when(first.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
    when(second.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
    instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
    TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
    assertThat(typeInitializer.isDefined(), is(true));
    typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
    verify(first).apply(methodVisitor, implementationContext, methodDescription);
    verify(second).apply(methodVisitor, implementationContext, methodDescription);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) ByteCodeAppender(net.bytebuddy.implementation.bytecode.ByteCodeAppender) Test(org.junit.Test)

Example 34 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 AbstractTypeDescriptionGenericTest method testMethodTypeVariableWithExtensionIsRetained.

@Test
public void testMethodTypeVariableWithExtensionIsRetained() 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(QUX)).getOnly();
    assertThat(methodDescription.getReturnType().getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(methodDescription.getReturnType().getSymbol(), is("S"));
    assertThat(methodDescription.getReturnType().getTypeVariableSource(), is((TypeVariableSource) methodDescription.asDefined()));
    assertThat(methodDescription.getReturnType().getUpperBounds().size(), is(1));
    assertThat(methodDescription.getReturnType().getUpperBounds().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(methodDescription.getReturnType().getUpperBounds().getOnly().asErasure().represents(Number.class), is(true));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeVariableSource(net.bytebuddy.description.TypeVariableSource) Test(org.junit.Test)

Example 35 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 AbstractTypeDescriptionGenericTest method testNestedInnerType.

@Test
public void testNestedInnerType() throws Exception {
    TypeDescription.Generic foo = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(FOO));
    assertThat(foo.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(foo.getSymbol(), is(T));
    assertThat(foo.getUpperBounds().size(), is(1));
    assertThat(foo.getUpperBounds().getOnly(), is(TypeDescription.Generic.OBJECT));
    assertThat(foo.getTypeVariableSource(), is((TypeVariableSource) new TypeDescription.ForLoadedType(NestedInnerType.class)));
    TypeDescription.Generic bar = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(BAR));
    assertThat(bar.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(bar.getSymbol(), is(S));
    assertThat(bar.getUpperBounds().size(), is(1));
    assertThat(bar.getUpperBounds().getOnly(), is(foo));
    assertThat(bar.getTypeVariableSource(), is((TypeVariableSource) new TypeDescription.ForLoadedType(NestedInnerType.InnerType.class)));
    TypeDescription.Generic qux = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(QUX));
    assertThat(qux.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(qux.getSymbol(), is(U));
    assertThat(qux.getUpperBounds().size(), is(1));
    assertThat(qux.getUpperBounds().getOnly(), is(bar));
    MethodDescription quxMethod = new MethodDescription.ForLoadedMethod(NestedInnerType.InnerType.class.getDeclaredMethod(QUX));
    assertThat(qux.getTypeVariableSource(), is((TypeVariableSource) quxMethod));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeVariableSource(net.bytebuddy.description.TypeVariableSource) 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