Search in sources :

Example 1 with ByteArrayClassLoader

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

the class SubclassDynamicTypeBuilderTest method testDoesNotOverrideMethodWithPackagePrivateArgumentType.

@Test
public void testDoesNotOverrideMethodWithPackagePrivateArgumentType() throws Exception {
    Class<?> type = new ByteBuddy().subclass(PackagePrivateArgumentType.class).name("net.bytebuddy.test.generated." + FOO).method(isDeclaredBy(PackagePrivateArgumentType.class)).intercept(StubMethod.INSTANCE).make().load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(PackagePrivateArgumentType.class, PackagePrivateArgumentType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethods().length, is(0));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 2 with ByteArrayClassLoader

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

the class SubclassDynamicTypeBuilderTest method testDoesNotOverrideMethodWithPackagePrivateReturnType.

@Test
public void testDoesNotOverrideMethodWithPackagePrivateReturnType() throws Exception {
    Class<?> type = new ByteBuddy().subclass(PackagePrivateReturnType.class).name("net.bytebuddy.test.generated." + FOO).method(isDeclaredBy(PackagePrivateReturnType.class)).intercept(StubMethod.INSTANCE).make().load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(PackagePrivateReturnType.class, PackagePrivateReturnType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethods().length, is(0));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 3 with ByteArrayClassLoader

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

the class AnnotationDescriptionForLoadedAnnotationDifferentClassLoaderTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Sample.class, SampleDefault.class, Other.class, SampleEnumeration.class, ExplicitTarget.class));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) Before(org.junit.Before)

Example 4 with ByteArrayClassLoader

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

the class AbstractDynamicTypeBuilderTest method testPreparedField.

@Test
public void testPreparedField() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(SampleAnnotation.class));
    Class<?> type = createPlain().defineMethod(BAR, String.class, Visibility.PUBLIC).intercept(new PreparedField()).make().load(classLoader, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredFields().length, is(1));
    assertThat(type.getDeclaredField(FOO).getName(), is(FOO));
    assertThat(type.getDeclaredField(FOO).getType(), CoreMatchers.<Class<?>>is(Object.class));
    assertThat(type.getDeclaredField(FOO).getModifiers(), is(MODIFIERS));
    assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1));
    Annotation annotation = type.getDeclaredField(FOO).getAnnotations()[0];
    assertThat(annotation.annotationType().getName(), is(SampleAnnotation.class.getName()));
    Method foo = annotation.annotationType().getDeclaredMethod(FOO);
    assertThat(foo.invoke(annotation), is((Object) BAR));
}
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 5 with ByteArrayClassLoader

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

the class AbstractDynamicTypeBuilderTest method testTypeInitializer.

@Test
public void testTypeInitializer() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Bar.class));
    Class<?> type = createPlain().invokable(isTypeInitializer()).intercept(MethodCall.invoke(Bar.class.getDeclaredMethod("invoke"))).make().load(classLoader, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredConstructor().newInstance(), notNullValue(Object.class));
    Class<?> foo = classLoader.loadClass(Bar.class.getName());
    assertThat(foo.getDeclaredField(FOO).get(null), is((Object) FOO));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) 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