Search in sources :

Example 31 with TypeDescription

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

the class TypeProxyCreationTest method testAllLegalNotIgnoreFinalizer.

@Test
public void testAllLegalNotIgnoreFinalizer() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    TypeDescription dynamicType = new TypeProxy(foo, implementationTarget, invocationFactory, false, false).make(BAR, ClassFileVersion.ofThisVm(), methodAccessorFactory).getTypeDescription();
    assertThat(dynamicType.getModifiers(), is(modifiers));
    assertThat(dynamicType.getSuperClass().asErasure(), is(foo));
    assertThat(dynamicType.getInterfaces(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(dynamicType.getName(), is(BAR));
    assertThat(dynamicType.getDeclaredMethods().size(), is(2));
    assertThat(dynamicType.isAssignableTo(Serializable.class), is(false));
    verify(methodAccessorFactory, times(fooMethods.size() + 1)).registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT);
    for (MethodDescription methodDescription : fooMethods) {
        verify(invocationFactory).invoke(implementationTarget, foo, methodDescription);
    }
    verify(invocationFactory).invoke(implementationTarget, foo, new MethodDescription.ForLoadedMethod(Object.class.getDeclaredMethod("finalize")));
    verifyNoMoreInteractions(invocationFactory);
    verify(specialMethodInvocation, times(fooMethods.size() + 1)).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
Also used : Serializable(java.io.Serializable) MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 32 with TypeDescription

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

the class TypeProxyCreationTest method testForConstructorConstruction.

@Test
public void testForConstructorConstruction() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    StackManipulation stackManipulation = new TypeProxy.ForSuperMethodByConstructor(foo, implementationTarget, Collections.singletonList((TypeDescription) new TypeDescription.ForLoadedType(Void.class)), true, false);
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(foo);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(3));
    verify(implementationContext).register(any(AuxiliaryType.class));
    verifyNoMoreInteractions(implementationContext);
    verify(methodVisitor).visitTypeInsn(Opcodes.NEW, Type.getInternalName(Foo.class));
    verify(methodVisitor, times(2)).visitInsn(Opcodes.DUP);
    verify(methodVisitor).visitInsn(Opcodes.ACONST_NULL);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, foo.getInternalName(), MethodDescription.CONSTRUCTOR_INTERNAL_NAME, foo.getDeclaredMethods().filter(isConstructor()).getOnly().getDescriptor(), false);
    verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, foo.getInternalName(), TypeProxy.INSTANCE_FIELD, Type.getDescriptor(Void.class));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Implementation(net.bytebuddy.implementation.Implementation) MethodVisitor(org.objectweb.asm.MethodVisitor) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 33 with TypeDescription

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

the class TypeProxyCreationTest method testAllIllegal.

@Test
public void testAllIllegal() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    TypeDescription dynamicType = new TypeProxy(foo, implementationTarget, invocationFactory, true, false).make(BAR, ClassFileVersion.ofThisVm(), methodAccessorFactory).getTypeDescription();
    assertThat(dynamicType.getModifiers(), is(modifiers));
    assertThat(dynamicType.getSuperClass().asErasure(), is(foo));
    assertThat(dynamicType.getInterfaces(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(dynamicType.getName(), is(BAR));
    assertThat(dynamicType.getDeclaredMethods().size(), is(2));
    assertThat(dynamicType.isAssignableTo(Serializable.class), is(false));
    verifyZeroInteractions(methodAccessorFactory);
    for (MethodDescription methodDescription : fooMethods) {
        verify(invocationFactory).invoke(implementationTarget, foo, methodDescription);
    }
    verifyNoMoreInteractions(invocationFactory);
    verify(specialMethodInvocation, times(fooMethods.size())).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
Also used : Serializable(java.io.Serializable) MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 34 with TypeDescription

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

the class TypeProxyCreationTest method testAllLegal.

@Test
public void testAllLegal() throws Exception {
    when(implementationTarget.getInstrumentedType()).thenReturn(foo);
    when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))).thenReturn(specialMethodInvocation);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))).thenReturn(new StackManipulation.Size(0, 0));
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    TypeDescription dynamicType = new TypeProxy(foo, implementationTarget, invocationFactory, true, false).make(BAR, ClassFileVersion.ofThisVm(), methodAccessorFactory).getTypeDescription();
    assertThat(dynamicType.getModifiers(), is(modifiers));
    assertThat(dynamicType.getSuperClass().asErasure(), is(foo));
    assertThat(dynamicType.getInterfaces(), is((TypeList.Generic) new TypeList.Generic.Empty()));
    assertThat(dynamicType.getName(), is(BAR));
    assertThat(dynamicType.getDeclaredMethods().size(), is(2));
    assertThat(dynamicType.isAssignableTo(Serializable.class), is(false));
    verify(methodAccessorFactory, times(fooMethods.size())).registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT);
    for (MethodDescription methodDescription : fooMethods) {
        verify(invocationFactory).invoke(implementationTarget, foo, methodDescription);
    }
    verifyNoMoreInteractions(invocationFactory);
    verify(specialMethodInvocation, times(fooMethods.size())).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
Also used : Serializable(java.io.Serializable) MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 35 with TypeDescription

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

the class TypeProxyCreationTest method testAccessorIsValid.

@Test
@SuppressWarnings("unchecked")
public void testAccessorIsValid() throws Exception {
    TypeProxy typeProxy = new TypeProxy(mock(TypeDescription.class), mock(Implementation.Target.class), mock(TypeProxy.InvocationFactory.class), false, false);
    TypeProxy.MethodCall methodCall = typeProxy.new MethodCall(mock(MethodAccessorFactory.class));
    TypeDescription instrumentedType = mock(TypeDescription.class);
    FieldList<FieldDescription.InDefinedShape> fieldList = mock(FieldList.class);
    when(fieldList.filter(any(ElementMatcher.class))).thenReturn(fieldList);
    when(fieldList.getOnly()).thenReturn(mock(FieldDescription.InDefinedShape.class));
    when(instrumentedType.getDeclaredFields()).thenReturn(fieldList);
    TypeProxy.MethodCall.Appender appender = methodCall.new Appender(instrumentedType);
    Implementation.SpecialMethodInvocation specialMethodInvocation = mock(Implementation.SpecialMethodInvocation.class);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    StackManipulation stackManipulation = appender.new AccessorMethodInvocation(mock(MethodDescription.class), specialMethodInvocation);
    assertThat(stackManipulation.isValid(), is(true));
    verify(specialMethodInvocation).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
Also used : MethodAccessorFactory(net.bytebuddy.implementation.MethodAccessorFactory) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) MethodDescription(net.bytebuddy.description.method.MethodDescription) Implementation(net.bytebuddy.implementation.Implementation) ElementMatcher(net.bytebuddy.matcher.ElementMatcher) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Aggregations

TypeDescription (net.bytebuddy.description.type.TypeDescription)178 Test (org.junit.Test)155 MethodDescription (net.bytebuddy.description.method.MethodDescription)75 ByteBuddy (net.bytebuddy.ByteBuddy)25 DynamicType (net.bytebuddy.dynamic.DynamicType)25 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)17 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)10 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)9 AbstractTypeDescriptionTest (net.bytebuddy.description.type.AbstractTypeDescriptionTest)9 Map (java.util.Map)8 MethodList (net.bytebuddy.description.method.MethodList)8 Field (java.lang.reflect.Field)7 List (java.util.List)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)5 Serializable (java.io.Serializable)4 URLClassLoader (java.net.URLClassLoader)4 TypeList (net.bytebuddy.description.type.TypeList)4 Before (org.junit.Before)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4