use of net.bytebuddy.implementation.LoadedTypeInitializer in project byte-buddy by raphw.
the class InstrumentedTypeDefaultTest method testWithLoadedTypeInitializerSingle.
@Test
public void testWithLoadedTypeInitializerSingle() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class);
instrumentedType = instrumentedType.withInitializer(loadedTypeInitializer);
assertThat(instrumentedType.getLoadedTypeInitializer(), is((LoadedTypeInitializer) new LoadedTypeInitializer.Compound(LoadedTypeInitializer.NoOp.INSTANCE, loadedTypeInitializer)));
}
use of net.bytebuddy.implementation.LoadedTypeInitializer in project byte-buddy by raphw.
the class InstrumentedTypeFrozenTest method testLoadedTypeInitializer.
@Test
public void testLoadedTypeInitializer() throws Exception {
LoadedTypeInitializer loadedTypeInitializer = mock(LoadedTypeInitializer.class);
assertThat(new InstrumentedType.Frozen(TypeDescription.STRING, loadedTypeInitializer).getLoadedTypeInitializer(), is(loadedTypeInitializer));
}
use of net.bytebuddy.implementation.LoadedTypeInitializer in project byte-buddy by raphw.
the class InstrumentedTypeDefaultTest method testWithLoadedTypeInitializerDouble.
@Test
public void testWithLoadedTypeInitializerDouble() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
LoadedTypeInitializer first = mock(LoadedTypeInitializer.class), second = mock(LoadedTypeInitializer.class);
instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
assertThat(instrumentedType.getLoadedTypeInitializer(), is((LoadedTypeInitializer) new LoadedTypeInitializer.Compound(new LoadedTypeInitializer.Compound(LoadedTypeInitializer.NoOp.INSTANCE, first), second)));
}
use of net.bytebuddy.implementation.LoadedTypeInitializer 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.implementation.LoadedTypeInitializer 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