Search in sources :

Example 6 with ByteArrayClassLoader

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

the class TypeDescriptionForLoadedTypeTest method testLazyResolution.

@Test
public void testLazyResolution() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Foo.class));
    new TypeDescription.ForLoadedType(classLoader.loadClass(Foo.class.getName()));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Test(org.junit.Test)

Example 7 with ByteArrayClassLoader

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

the class ByteBuddyTest method testImplicitStrategyInjectable.

@Test
public void testImplicitStrategyInjectable() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, Collections.<String, byte[]>emptyMap());
    Class<?> type = new ByteBuddy().subclass(Object.class).make().load(classLoader).getLoaded();
    assertThat(type.getClassLoader(), is(classLoader));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Test(org.junit.Test)

Example 8 with ByteArrayClassLoader

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

the class AgentBuilderDefaultApplicationSuperTypeLoadingTest method setUp.

@Before
public void setUp() throws Exception {
    classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Foo.class, Bar.class, AgentBuilderDefaultApplicationSuperTypeLoadingTest.class), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
    executorService = Executors.newCachedThreadPool();
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Before(org.junit.Before)

Example 9 with ByteArrayClassLoader

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

the class AnnotationAppenderDefaultTest method makeTypeWithAnnotation.

private Class<?> makeTypeWithAnnotation(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.visitAnnotation(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)

Example 10 with ByteArrayClassLoader

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

the class MethodDelegationSuperTest method testFinalType.

@Test
public void testFinalType() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(SimpleInterceptor.class));
    Class<?> type = new ByteBuddy().rebase(FinalType.class).modifiers(TypeManifestation.PLAIN, Visibility.PUBLIC).method(named(FOO)).intercept(ExceptionMethod.throwing(RuntimeException.class)).method(named(BAR)).intercept(MethodDelegation.to(SimpleInterceptor.class)).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION).getLoaded();
    assertThat(type.getDeclaredMethod(BAR).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

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