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));
}
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);
}
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));
}
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));
}
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;
}
Aggregations