Search in sources :

Example 51 with TypeDescription

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

Example 52 with TypeDescription

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

Example 53 with TypeDescription

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

Example 54 with 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);
}
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 55 with TypeDescription

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

TypeDescription (net.bytebuddy.description.type.TypeDescription)178 Test (org.junit.Test)155 MethodDescription (net.bytebuddy.description.method.MethodDescription)75 ByteBuddy (net.bytebuddy.ByteBuddy)25 DynamicType (net.bytebuddy.dynamic.DynamicType)25 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)17 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)10 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)9 AbstractTypeDescriptionTest (net.bytebuddy.description.type.AbstractTypeDescriptionTest)9 Map (java.util.Map)8 MethodList (net.bytebuddy.description.method.MethodList)8 Field (java.lang.reflect.Field)7 List (java.util.List)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)5 Serializable (java.io.Serializable)4 URLClassLoader (java.net.URLClassLoader)4 TypeList (net.bytebuddy.description.type.TypeList)4 Before (org.junit.Before)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4