Search in sources :

Example 1 with ClassInjector

use of net.bytebuddy.dynamic.loading.ClassInjector 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);
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) HashMap(java.util.HashMap) AuxiliaryType(net.bytebuddy.implementation.auxiliary.AuxiliaryType) TypeDescription(net.bytebuddy.description.type.TypeDescription) Annotation(java.lang.annotation.Annotation) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) ClassInjector(net.bytebuddy.dynamic.loading.ClassInjector) Test(org.junit.Test)

Example 2 with ClassInjector

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

the class AgentBuilderDefaultTest method testBootstrapClassLoaderCapableInjectorFactoryReflection.

@Test
public void testBootstrapClassLoaderCapableInjectorFactoryReflection() throws Exception {
    AgentBuilder.Default.BootstrapInjectionStrategy bootstrapInjectionStrategy = mock(AgentBuilder.Default.BootstrapInjectionStrategy.class);
    ClassLoader classLoader = mock(ClassLoader.class);
    ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
    assertThat(new AgentBuilder.Default.Transformation.Simple.Resolution.BootstrapClassLoaderCapableInjectorFactory(bootstrapInjectionStrategy, classLoader, protectionDomain).resolve(), is((ClassInjector) new ClassInjector.UsingReflection(classLoader, protectionDomain)));
    verifyZeroInteractions(bootstrapInjectionStrategy);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassInjector(net.bytebuddy.dynamic.loading.ClassInjector) Test(org.junit.Test)

Example 3 with ClassInjector

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

the class AgentBuilderDefaultTest method testBootstrapClassLoaderCapableInjectorFactoryInstrumentation.

@Test
public void testBootstrapClassLoaderCapableInjectorFactoryInstrumentation() throws Exception {
    AgentBuilder.Default.BootstrapInjectionStrategy bootstrapInjectionStrategy = mock(AgentBuilder.Default.BootstrapInjectionStrategy.class);
    ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
    ClassInjector classInjector = mock(ClassInjector.class);
    when(bootstrapInjectionStrategy.make(protectionDomain)).thenReturn(classInjector);
    assertThat(new AgentBuilder.Default.Transformation.Simple.Resolution.BootstrapClassLoaderCapableInjectorFactory(bootstrapInjectionStrategy, null, protectionDomain).resolve(), is(classInjector));
    verify(bootstrapInjectionStrategy).make(protectionDomain);
    verifyNoMoreInteractions(bootstrapInjectionStrategy);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassInjector(net.bytebuddy.dynamic.loading.ClassInjector) Test(org.junit.Test)

Aggregations

ClassInjector (net.bytebuddy.dynamic.loading.ClassInjector)3 Test (org.junit.Test)3 ProtectionDomain (java.security.ProtectionDomain)2 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)1 TypeDescription (net.bytebuddy.description.type.TypeDescription)1 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)1 AuxiliaryType (net.bytebuddy.implementation.auxiliary.AuxiliaryType)1