Search in sources :

Example 16 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class RedefinitionDynamicTypeBuilderTest method testObjectProperties.

@Test
@SuppressWarnings("unchecked")
public void testObjectProperties() throws Exception {
    ObjectPropertyAssertion.of(RedefinitionDynamicTypeBuilder.class).create(new ObjectPropertyAssertion.Creator<List<?>>() {

        @Override
        public List<?> create() {
            TypeDescription typeDescription = mock(TypeDescription.class);
            when(typeDescription.asErasure()).thenReturn(typeDescription);
            return Collections.singletonList(typeDescription);
        }
    }).create(new ObjectPropertyAssertion.Creator<TypeDescription>() {

        @Override
        public TypeDescription create() {
            TypeDescription rawTypeDescription = mock(TypeDescription.class);
            when(rawTypeDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
            when(rawTypeDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
            TypeDescription.Generic typeDescription = mock(TypeDescription.Generic.class);
            when(typeDescription.asErasure()).thenReturn(rawTypeDescription);
            when(typeDescription.asGenericType()).thenReturn(typeDescription);
            when(rawTypeDescription.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(typeDescription));
            when(rawTypeDescription.getDeclaredFields()).thenReturn(new FieldList.Empty<FieldDescription.InDefinedShape>());
            when(rawTypeDescription.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
            return rawTypeDescription;
        }
    }).apply();
}
Also used : MethodList(net.bytebuddy.description.method.MethodList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) FieldList(net.bytebuddy.description.field.FieldList) TypeDescription(net.bytebuddy.description.type.TypeDescription) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) TypeList(net.bytebuddy.description.type.TypeList) MethodList(net.bytebuddy.description.method.MethodList) List(java.util.List) FieldList(net.bytebuddy.description.field.FieldList) ObjectPropertyAssertion(net.bytebuddy.test.utility.ObjectPropertyAssertion) Test(org.junit.Test)

Example 17 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class ImplementationSpecialMethodInvocationSimpleTest method testEquality.

@Test
public void testEquality() throws Exception {
    MethodDescription firstMethod = mock(MethodDescription.class), secondMethod = mock(MethodDescription.class);
    MethodDescription.SignatureToken firstToken = mock(MethodDescription.SignatureToken.class), secondToken = mock(MethodDescription.SignatureToken.class);
    when(firstMethod.asSignatureToken()).thenReturn(firstToken);
    when(secondMethod.asSignatureToken()).thenReturn(secondToken);
    TypeDescription firstType = mock(TypeDescription.class), secondType = mock(TypeDescription.class);
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), is(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class))));
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), not(new Implementation.SpecialMethodInvocation.Simple(secondMethod, firstType, mock(StackManipulation.class))));
    assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), not(new Implementation.SpecialMethodInvocation.Simple(firstMethod, secondType, mock(StackManipulation.class))));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 18 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class InvokeDynamicTest method testChainedInvocation.

@Test
@JavaVersionRule.Enforce(7)
public void testChainedInvocation() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
    DynamicType.Loaded<SimpleWithArgument> dynamicType = new ByteBuddy().subclass(SimpleWithArgument.class).method(isDeclaredBy(SimpleWithArgument.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withArgument(0).andThen(FixedValue.value(BAZ))).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(FOO), is(BAZ));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 19 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class InvokeDynamicTest method testArgumentCannotAssignIllegalInstanceType.

@Test(expected = IllegalArgumentException.class)
@JavaVersionRule.Enforce(7)
public void testArgumentCannotAssignIllegalInstanceType() throws Exception {
    Class<?> type = Class.forName(ARGUMENT_BOOTSTRAP);
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
    InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withReference(new Object()).as(String.class);
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 20 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersWrapperReference.

@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapOfMethodsWithParametersWrapperReference() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
    Object value = new Object();
    DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(BAR, String.class).withReference(BOOLEAN, BYTE, SHORT, CHARACTER, INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeEnum(), makeMethodType(CLASS)).withReference(makeMethodHandle()).as(// avoid direct method handle
    JavaType.METHOD_HANDLE.load()).withReference(value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(14));
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

TypeDescription (net.bytebuddy.description.type.TypeDescription)177 Test (org.junit.Test)155 MethodDescription (net.bytebuddy.description.method.MethodDescription)75 ByteBuddy (net.bytebuddy.ByteBuddy)26 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 MethodList (net.bytebuddy.description.method.MethodList)8 Field (java.lang.reflect.Field)7 Map (java.util.Map)7 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)5 Before (org.junit.Before)5 MethodVisitor (org.objectweb.asm.MethodVisitor)5 Serializable (java.io.Serializable)4 URLClassLoader (java.net.URLClassLoader)4 TypeList (net.bytebuddy.description.type.TypeList)4 HashMap (java.util.HashMap)3