use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InjectionClassLoaderTest method testInjection.
@Test
@SuppressWarnings("unchecked")
public void testInjection() throws Exception {
InjectionClassLoader classLoader = mock(InjectionClassLoader.class);
TypeDescription typeDescription = mock(TypeDescription.class);
byte[] binaryRepresentation = new byte[0];
when(typeDescription.getName()).thenReturn(FOO);
when(classLoader.defineClass(FOO, binaryRepresentation)).thenReturn((Class) Object.class);
assertThat(InjectionClassLoader.Strategy.INSTANCE.load(classLoader, Collections.singletonMap(typeDescription, binaryRepresentation)), is(Collections.<TypeDescription, Class<?>>singletonMap(typeDescription, Object.class)));
verify(classLoader).defineClass(FOO, binaryRepresentation);
verifyNoMoreInteractions(classLoader);
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class ClassFileLocatorSimpleTest method testDynamicType.
@Test
public void testDynamicType() throws Exception {
DynamicType dynamicType = mock(DynamicType.class);
TypeDescription typeDescription = mock(TypeDescription.class);
when(typeDescription.getName()).thenReturn(FOO);
when(dynamicType.getAllTypes()).thenReturn(Collections.singletonMap(typeDescription, QUX));
ClassFileLocator classFileLocator = ClassFileLocator.Simple.of(dynamicType);
assertThat(classFileLocator.locate(FOO).isResolved(), is(true));
assertThat(classFileLocator.locate(FOO).resolve(), is(QUX));
assertThat(classFileLocator.locate(BAR).isResolved(), is(false));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class AgentBuilderDescriptionStrategyTest method testDescriptionHybridWithoutLoaded.
@Test
public void testDescriptionHybridWithoutLoaded() throws Exception {
when(typePool.describe(Object.class.getName())).thenReturn(new TypePool.Resolution.Simple(typeDescription));
TypeDescription typeDescription = AgentBuilder.DescriptionStrategy.Default.HYBRID.apply(Object.class.getName(), null, typePool, mock(AgentBuilder.CircularityLock.class), Object.class.getClassLoader(), JavaModule.ofType(Object.class));
assertThat(typeDescription, is(this.typeDescription));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class AgentBuilderInitializationStrategySelfInjectionDispatcherTest method setUp.
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
when(builder.initializer((any(ByteCodeAppender.class)))).thenReturn((DynamicType.Builder) appendedBuilder);
when(injectorFactory.resolve()).thenReturn(classInjector);
when(dynamicType.getTypeDescription()).thenReturn(instrumented);
Map<TypeDescription, byte[]> auxiliaryTypes = new HashMap<TypeDescription, byte[]>();
auxiliaryTypes.put(dependent, FOO);
auxiliaryTypes.put(independent, BAR);
when(dynamicType.getAuxiliaryTypes()).thenReturn(auxiliaryTypes);
Map<TypeDescription, LoadedTypeInitializer> loadedTypeInitializers = new HashMap<TypeDescription, LoadedTypeInitializer>();
loadedTypeInitializers.put(instrumented, instrumentedInitializer);
loadedTypeInitializers.put(dependent, dependentInitializer);
loadedTypeInitializers.put(independent, independentInitializer);
when(dynamicType.getLoadedTypeInitializers()).thenReturn(loadedTypeInitializers);
when(instrumented.getName()).thenReturn(Qux.class.getName());
when(classInjector.inject(any(Map.class))).then(new Answer<Map<TypeDescription, Class<?>>>() {
@Override
public Map<TypeDescription, Class<?>> answer(InvocationOnMock invocationOnMock) throws Throwable {
Map<TypeDescription, Class<?>> loaded = new HashMap<TypeDescription, Class<?>>();
for (TypeDescription typeDescription : ((Map<TypeDescription, byte[]>) invocationOnMock.getArguments()[0]).keySet()) {
if (typeDescription.equals(dependent)) {
loaded.put(dependent, Foo.class);
} else if (typeDescription.equals(independent)) {
loaded.put(independent, Bar.class);
} else {
throw new AssertionError();
}
}
return loaded;
}
});
Annotation eagerAnnotation = mock(AuxiliaryType.SignatureRelevant.class);
when(eagerAnnotation.annotationType()).thenReturn((Class) AuxiliaryType.SignatureRelevant.class);
when(independent.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(eagerAnnotation));
when(dependent.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
when(instrumentedInitializer.isAlive()).thenReturn(true);
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class AgentBuilderInitializationStrategyTest method testMinimalRegistrationIndependentType.
@Test
@SuppressWarnings("unchecked")
public void testMinimalRegistrationIndependentType() throws Exception {
Annotation eagerAnnotation = mock(AuxiliaryType.SignatureRelevant.class);
when(eagerAnnotation.annotationType()).thenReturn((Class) AuxiliaryType.SignatureRelevant.class);
TypeDescription independent = mock(TypeDescription.class), dependent = mock(TypeDescription.class);
when(independent.getDeclaredAnnotations()).thenReturn(new AnnotationList.ForLoadedAnnotations(eagerAnnotation));
when(dependent.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
Map<TypeDescription, byte[]> map = new HashMap<TypeDescription, byte[]>();
map.put(independent, QUX);
map.put(dependent, BAZ);
when(dynamicType.getAuxiliaryTypes()).thenReturn(map);
ClassInjector classInjector = mock(ClassInjector.class);
when(injectorFactory.resolve()).thenReturn(classInjector);
when(classInjector.inject(Collections.singletonMap(independent, QUX))).thenReturn(Collections.<TypeDescription, Class<?>>singletonMap(independent, Foo.class));
LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class);
when(dynamicType.getLoadedTypeInitializers()).thenReturn(Collections.singletonMap(independent, loadedTypeInitializer));
AgentBuilder.InitializationStrategy.Minimal.INSTANCE.register(dynamicType, classLoader, injectorFactory);
verify(classInjector).inject(Collections.singletonMap(independent, QUX));
verifyNoMoreInteractions(classInjector);
verify(loadedTypeInitializer).onLoad(Foo.class);
verifyNoMoreInteractions(loadedTypeInitializer);
}
Aggregations