Search in sources :

Example 6 with TypeDescription

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

the class TypePoolResolutionTest method testArrayResolutionPositiveArity.

@Test
public void testArrayResolutionPositiveArity() throws Exception {
    TypePool.Resolution resolution = mock(TypePool.Resolution.class);
    when(resolution.isResolved()).thenReturn(true);
    when(resolution.resolve()).thenReturn(mock(TypeDescription.class));
    assertThat(TypePool.Default.ArrayTypeResolution.of(resolution, 1), not(resolution));
    TypeDescription typeDescription = TypePool.Default.ArrayTypeResolution.of(resolution, 1).resolve();
    assertThat(typeDescription.isArray(), is(true));
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 7 with TypeDescription

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

the class JavaConstantMethodHandleTest method testMethodNotSpecializable.

@Test(expected = IllegalArgumentException.class)
public void testMethodNotSpecializable() throws Exception {
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    TypeDescription typeDescription = mock(TypeDescription.class);
    when(methodDescription.isSpecializableFor(typeDescription)).thenReturn(false);
    JavaConstant.MethodHandle.ofSpecial(methodDescription, typeDescription);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 8 with TypeDescription

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

the class JavaConstantMethodHandleTest method testMethodHandleOfLoadedMethodHandle.

@Test
@SuppressWarnings("unchecked")
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testMethodHandleOfLoadedMethodHandle() throws Exception {
    Method publicLookup = Class.forName("java.lang.invoke.MethodHandles").getDeclaredMethod("publicLookup");
    Object lookup = publicLookup.invoke(null);
    Method unreflected = Class.forName("java.lang.invoke.MethodHandles$Lookup").getDeclaredMethod("unreflect", Method.class);
    Object methodHandleLoaded = unreflected.invoke(lookup, Foo.class.getDeclaredMethod(BAR, Void.class));
    JavaConstant.MethodHandle methodHandle = JavaConstant.MethodHandle.ofLoaded(methodHandleLoaded);
    assertThat(methodHandle.getHandleType(), is(JavaConstant.MethodHandle.HandleType.INVOKE_VIRTUAL));
    assertThat(methodHandle.getName(), is(BAR));
    assertThat(methodHandle.getOwnerType(), is((TypeDescription) new TypeDescription.ForLoadedType(Foo.class)));
    assertThat(methodHandle.getReturnType(), is(TypeDescription.VOID));
    assertThat(methodHandle.getParameterTypes(), is((List<TypeDescription>) new TypeList.ForLoadedTypes(Void.class)));
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) List(java.util.List) TypeList(net.bytebuddy.description.type.TypeList) Method(java.lang.reflect.Method) TypeList(net.bytebuddy.description.type.TypeList) Test(org.junit.Test)

Example 9 with TypeDescription

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

the class TypePoolDefaultCacheTest method testCache.

@Test
public void testCache() throws Exception {
    TypePool typePool = TypePool.Default.ofClassPath();
    TypeDescription typeDescription = typePool.describe(Void.class.getName()).resolve();
    assertThat(typePool.describe(Void.class.getName()).resolve(), sameInstance(typeDescription));
    typePool.clear();
    assertThat(typePool.describe(Void.class.getName()).resolve(), not(sameInstance(typeDescription)));
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 10 with TypeDescription

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

the class TypePoolDefaultComponentPoolStrategyTest method testForAnnotationProperty.

@Test
public void testForAnnotationProperty() throws Exception {
    TypePool typePool = mock(TypePool.class);
    TypeDescription typeDescription = mock(TypeDescription.class);
    when(typePool.describe(BAR)).thenReturn(new TypePool.Resolution.Simple(typeDescription));
    MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
    when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(methodDescription));
    when(methodDescription.getActualName()).thenReturn(FOO);
    TypeDescription.Generic returnType = mock(TypeDescription.Generic.class);
    TypeDescription rawReturnType = mock(TypeDescription.class);
    when(returnType.asErasure()).thenReturn(rawReturnType);
    when(methodDescription.getReturnType()).thenReturn(returnType);
    TypeDescription rawComponentType = mock(TypeDescription.class);
    when(rawReturnType.getComponentType()).thenReturn(rawComponentType);
    when(rawComponentType.getName()).thenReturn(QUX);
    assertThat(new TypePool.Default.ComponentTypeLocator.ForAnnotationProperty(typePool, BAR_DESCRIPTOR).bind(FOO).lookup(), is(QUX));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) 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