Search in sources :

Example 16 with ByteArrayClassLoader

use of net.bytebuddy.dynamic.loading.ByteArrayClassLoader in project byte-buddy by raphw.

the class AbstractDynamicTypeBuilderTest method testPreparedMethod.

@Test
public void testPreparedMethod() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(SampleAnnotation.class));
    Class<?> type = createPlain().defineMethod(BAR, String.class, Visibility.PUBLIC).intercept(new PreparedMethod()).make().load(classLoader, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethods().length, is(2));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getName(), is(FOO));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getReturnType(), CoreMatchers.<Class<?>>is(Object.class));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterTypes().length, is(1));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterTypes()[0], CoreMatchers.<Class<?>>is(Object.class));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getModifiers(), is(MODIFIERS));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getAnnotations().length, is(1));
    Annotation methodAnnotation = type.getDeclaredMethod(FOO, Object.class).getAnnotations()[0];
    assertThat(methodAnnotation.annotationType().getName(), is(SampleAnnotation.class.getName()));
    Method methodMethod = methodAnnotation.annotationType().getDeclaredMethod(FOO);
    assertThat(methodMethod.invoke(methodAnnotation), is((Object) BAR));
    assertThat(type.getDeclaredMethod(FOO, Object.class).getParameterAnnotations()[0].length, is(1));
    Annotation parameterAnnotation = type.getDeclaredMethod(FOO, Object.class).getParameterAnnotations()[0][0];
    assertThat(parameterAnnotation.annotationType().getName(), is(SampleAnnotation.class.getName()));
    Method parameterMethod = parameterAnnotation.annotationType().getDeclaredMethod(FOO);
    assertThat(parameterMethod.invoke(parameterAnnotation), is((Object) QUX));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 17 with ByteArrayClassLoader

use of net.bytebuddy.dynamic.loading.ByteArrayClassLoader in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationRedefineTest method setUp.

@Before
public void setUp() throws Exception {
    simpleTypeLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(SimpleType.class), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
    optionalTypeLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(SimpleOptionalType.class), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Before(org.junit.Before)

Example 18 with ByteArrayClassLoader

use of net.bytebuddy.dynamic.loading.ByteArrayClassLoader in project byte-buddy by raphw.

the class AbstractDynamicTypeBuilderForInliningTest method testVisibilityBridge.

@Test
public void testVisibilityBridge() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(PackagePrivateVisibilityBridgeExtension.class, VisibilityBridge.class, FooBar.class));
    Class<?> type = create(PackagePrivateVisibilityBridgeExtension.class).modifiers(Opcodes.ACC_PUBLIC).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION).getLoaded();
    assertThat(type.getDeclaredConstructors().length, is(1));
    Constructor<?> constructor = type.getDeclaredConstructor();
    constructor.setAccessible(true);
    assertThat(type.getDeclaredMethods().length, is(2));
    Method foo = type.getDeclaredMethod(FOO, String.class);
    foo.setAccessible(true);
    assertThat(foo.isBridge(), is(true));
    assertThat(foo.getDeclaredAnnotations().length, is(1));
    assertThat(foo.getDeclaredAnnotations()[0].annotationType().getName(), is(FooBar.class.getName()));
    assertThat(foo.invoke(constructor.newInstance(), BAR), is((Object) (FOO + BAR)));
    assertThat(foo.getParameterAnnotations()[0].length, is(1));
    assertThat(foo.getParameterAnnotations()[0][0].annotationType().getName(), is(FooBar.class.getName()));
    assertThat(foo.invoke(constructor.newInstance(), BAR), is((Object) (FOO + BAR)));
    Method bar = type.getDeclaredMethod(BAR, List.class);
    bar.setAccessible(true);
    assertThat(bar.isBridge(), is(true));
    assertThat(bar.getDeclaredAnnotations().length, is(0));
    List<?> list = new ArrayList<Object>();
    assertThat(bar.invoke(constructor.newInstance(), list), sameInstance((Object) list));
    assertThat(bar.getGenericReturnType(), instanceOf(Class.class));
    assertThat(bar.getGenericParameterTypes()[0], instanceOf(Class.class));
    assertThat(bar.getGenericExceptionTypes()[0], instanceOf(Class.class));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) StubMethod(net.bytebuddy.implementation.StubMethod) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 19 with ByteArrayClassLoader

use of net.bytebuddy.dynamic.loading.ByteArrayClassLoader in project byte-buddy by raphw.

the class AbstractDynamicTypeBuilderForInliningTest method testGenericType.

@Test
public void testGenericType() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(GenericType.class));
    Class<?> dynamicType = create(GenericType.Inner.class).method(named(FOO)).intercept(StubMethod.INSTANCE).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION).getLoaded();
    assertThat(dynamicType.getTypeParameters().length, is(2));
    assertThat(dynamicType.getTypeParameters()[0].getName(), is("T"));
    assertThat(dynamicType.getTypeParameters()[0].getBounds().length, is(1));
    assertThat(dynamicType.getTypeParameters()[0].getBounds()[0], instanceOf(Class.class));
    assertThat(dynamicType.getTypeParameters()[0].getBounds()[0], is((Type) String.class));
    assertThat(dynamicType.getTypeParameters()[1].getName(), is("S"));
    assertThat(dynamicType.getTypeParameters()[1].getBounds().length, is(1));
    assertThat(dynamicType.getTypeParameters()[1].getBounds()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(dynamicType.getGenericSuperclass(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments()[0], instanceOf(ParameterizedType.class));
    ParameterizedType superClass = (ParameterizedType) ((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments()[0];
    assertThat(superClass.getActualTypeArguments().length, is(2));
    assertThat(superClass.getActualTypeArguments()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(superClass.getActualTypeArguments()[1], is((Type) dynamicType.getTypeParameters()[1]));
    assertThat(superClass.getOwnerType(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getRawType(), instanceOf(Class.class));
    assertThat(((Class<?>) ((ParameterizedType) superClass.getOwnerType()).getRawType()).getName(), is(GenericType.class.getName()));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getActualTypeArguments()[0], is((Type) ((Class<?>) ((ParameterizedType) superClass.getOwnerType()).getRawType()).getTypeParameters()[0]));
    assertThat(dynamicType.getGenericInterfaces().length, is(1));
    assertThat(dynamicType.getGenericInterfaces()[0], instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0], instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getRawType(), is((Type) Callable.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getOwnerType(), nullValue(Type.class));
    assertThat(((ParameterizedType) ((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0]).getActualTypeArguments().length, is(2));
    ParameterizedType interfaceType = (ParameterizedType) ((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0];
    assertThat(interfaceType.getRawType(), is((Type) Map.class));
    assertThat(interfaceType.getActualTypeArguments().length, is(2));
    assertThat(interfaceType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getUpperBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getUpperBounds()[0], is((Type) Object.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getLowerBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getLowerBounds()[0], is((Type) String.class));
    assertThat(interfaceType.getActualTypeArguments()[1], instanceOf(WildcardType.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getUpperBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getUpperBounds()[0], is((Type) String.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getLowerBounds().length, is(0));
    Method foo = dynamicType.getDeclaredMethod(FOO, String.class);
    assertThat(foo.getGenericReturnType(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments()[0], instanceOf(GenericArrayType.class));
    assertThat(((GenericArrayType) ((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments()[0]).getGenericComponentType(), is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(foo.getTypeParameters().length, is(2));
    assertThat(foo.getTypeParameters()[0].getName(), is("V"));
    assertThat(foo.getTypeParameters()[0].getBounds().length, is(1));
    assertThat(foo.getTypeParameters()[0].getBounds()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(foo.getTypeParameters()[1].getName(), is("W"));
    assertThat(foo.getTypeParameters()[1].getBounds().length, is(1));
    assertThat(foo.getTypeParameters()[1].getBounds()[0], is((Type) Exception.class));
    assertThat(foo.getGenericParameterTypes().length, is(1));
    assertThat(foo.getGenericParameterTypes()[0], is((Type) foo.getTypeParameters()[0]));
    assertThat(foo.getGenericExceptionTypes().length, is(1));
    assertThat(foo.getGenericExceptionTypes()[0], is((Type) foo.getTypeParameters()[1]));
    Method call = dynamicType.getDeclaredMethod("call");
    assertThat(call.getGenericReturnType(), is((Type) interfaceType));
}
Also used : GenericType(net.bytebuddy.test.scope.GenericType) DynamicType(net.bytebuddy.dynamic.DynamicType) Type(java.lang.reflect.Type) GenericType(net.bytebuddy.test.scope.GenericType) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) StubMethod(net.bytebuddy.implementation.StubMethod) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 20 with ByteArrayClassLoader

use of net.bytebuddy.dynamic.loading.ByteArrayClassLoader in project byte-buddy by raphw.

the class AnnotationAppenderDefaultTest method makeTypeWithSuperClassAnnotation.

private Class<?> makeTypeWithSuperClassAnnotation(Annotation annotation) throws Exception {
    when(valueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    ClassWriter classWriter = new ClassWriter(AsmVisitorWrapper.NO_FLAGS);
    classWriter.visit(ClassFileVersion.ofThisVm().getMinorMajorVersion(), Opcodes.ACC_PUBLIC, BAR.replace('.', '/'), null, Type.getInternalName(Object.class), null);
    AnnotationVisitor annotationVisitor = classWriter.visitTypeAnnotation(TypeReference.newSuperTypeReference(-1).getValue(), null, Type.getDescriptor(annotation.annotationType()), true);
    when(target.visit(any(String.class), anyBoolean())).thenReturn(annotationVisitor);
    AnnotationDescription annotationDescription = AnnotationDescription.ForLoadedAnnotation.of(annotation);
    annotationAppender.append(annotationDescription, valueFilter);
    classWriter.visitEnd();
    Class<?> bar = new ByteArrayClassLoader(getClass().getClassLoader(), Collections.singletonMap(BAR, classWriter.toByteArray())).loadClass(BAR);
    assertThat(bar.getName(), is(BAR));
    assertThat(bar.getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
    return bar;
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader)

Aggregations

ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)20 Test (org.junit.Test)14 URLClassLoader (java.net.URLClassLoader)9 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)7 ByteBuddy (net.bytebuddy.ByteBuddy)4 Before (org.junit.Before)3 Annotation (java.lang.annotation.Annotation)2 AnnotationDescription (net.bytebuddy.description.annotation.AnnotationDescription)2 StubMethod (net.bytebuddy.implementation.StubMethod)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 DynamicType (net.bytebuddy.dynamic.DynamicType)1 SimpleType (net.bytebuddy.test.packaging.SimpleType)1 GenericType (net.bytebuddy.test.scope.GenericType)1