Search in sources :

Example 1 with LoadedTypeInitializer

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)));
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) Test(org.junit.Test)

Example 2 with 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));
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) Test(org.junit.Test)

Example 3 with 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)));
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) Test(org.junit.Test)

Example 4 with LoadedTypeInitializer

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);
}
Also used : HashMap(java.util.HashMap) DynamicType(net.bytebuddy.dynamic.DynamicType) Annotation(java.lang.annotation.Annotation) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AuxiliaryType(net.bytebuddy.implementation.auxiliary.AuxiliaryType) TypeDescription(net.bytebuddy.description.type.TypeDescription) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 5 with LoadedTypeInitializer

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

Aggregations

LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)9 Test (org.junit.Test)6 TypeDescription (net.bytebuddy.description.type.TypeDescription)5 Map (java.util.Map)3 DynamicType (net.bytebuddy.dynamic.DynamicType)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 HashMap (java.util.HashMap)2 Plugin (net.bytebuddy.build.Plugin)2 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)2 AuxiliaryType (net.bytebuddy.implementation.auxiliary.AuxiliaryType)2 ClassInjector (net.bytebuddy.dynamic.loading.ClassInjector)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 GradleException (org.gradle.api.GradleException)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1