Search in sources :

Example 6 with ClassFileLocator

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

the class AgentBuilderLocationStrategySimpleTest method testLocation.

@Test
public void testLocation() throws Exception {
    ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
    assertThat(new AgentBuilder.LocationStrategy.Simple(classFileLocator).classFileLocator(mock(ClassLoader.class), mock(JavaModule.class)), is(classFileLocator));
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) Test(org.junit.Test)

Example 7 with ClassFileLocator

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

the class AdviceTest method testIOExceptionOnRead.

@Test(expected = IllegalStateException.class)
public void testIOExceptionOnRead() throws Exception {
    ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
    when(classFileLocator.locate(TrivialAdvice.class.getName())).thenThrow(new IOException());
    Advice.to(TrivialAdvice.class, classFileLocator);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with ClassFileLocator

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

the class TransformationAction method transform.

/**
     * Applies all registered transformations.
     *
     * @param root       The root directory to process.
     * @param entryPoint The transformation's entry point.
     * @param classPath  A list of class path elements expected by the processed classes.
     * @param plugins    The plugins to apply.
     * @throws IOException If an I/O exception occurs.
     */
private void transform(File root, Iterable<? extends File> classPath, EntryPoint entryPoint, List<Plugin> plugins) throws IOException {
    List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>();
    classFileLocators.add(new ClassFileLocator.ForFolder(root));
    for (File artifact : classPath) {
        classFileLocators.add(artifact.isFile() ? ClassFileLocator.ForJarFile.of(artifact) : new ClassFileLocator.ForFolder(artifact));
    }
    ClassFileLocator classFileLocator = new ClassFileLocator.Compound(classFileLocators);
    try {
        TypePool typePool = new TypePool.Default.WithLazyResolution(new TypePool.CacheProvider.Simple(), classFileLocator, TypePool.Default.ReaderMode.FAST, TypePool.ClassLoading.ofBootPath());
        project.getLogger().info("Processing class files located in in: {}", root);
        ByteBuddy byteBuddy;
        try {
            byteBuddy = entryPoint.getByteBuddy();
        } catch (Throwable throwable) {
            throw new GradleException("Cannot create Byte Buddy instance", throwable);
        }
        processDirectory(root, root, byteBuddy, entryPoint, byteBuddyExtension.getMethodNameTransformer(), classFileLocator, typePool, plugins);
    } finally {
        classFileLocator.close();
    }
}
Also used : ArrayList(java.util.ArrayList) ByteBuddy(net.bytebuddy.ByteBuddy) ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) GradleException(org.gradle.api.GradleException) File(java.io.File) TypePool(net.bytebuddy.pool.TypePool)

Example 9 with ClassFileLocator

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

the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testGenericSuperClassNavigatedHierarchyResolutionIsLazy.

@Test
public void testGenericSuperClassNavigatedHierarchyResolutionIsLazy() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
    assertThat(describe(GenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()).getSuperClass().getSuperClass().asErasure(), CoreMatchers.is((TypeDescription) new TypeDescription.ForLoadedType(SuperClass.class)));
    verify(classFileLocator).locate(GenericType.class.getName());
    verify(classFileLocator).locate(SampleGenericClass.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test) AbstractTypeDescriptionTest(net.bytebuddy.description.type.AbstractTypeDescriptionTest)

Example 10 with ClassFileLocator

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

the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testGenericSuperClassHierarchyResolutionIsLazy.

@Test
public void testGenericSuperClassHierarchyResolutionIsLazy() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
    assertThat(describe(GenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()).getSuperClass().asErasure(), CoreMatchers.is((TypeDescription) new TypeDescription.ForLoadedType(SampleGenericClass.class)));
    verify(classFileLocator).locate(GenericType.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test) AbstractTypeDescriptionTest(net.bytebuddy.description.type.AbstractTypeDescriptionTest)

Aggregations

ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)31 Test (org.junit.Test)29 TypeDescription (net.bytebuddy.description.type.TypeDescription)17 AbstractTypeDescriptionTest (net.bytebuddy.description.type.AbstractTypeDescriptionTest)15 List (java.util.List)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 ByteBuddy (net.bytebuddy.ByteBuddy)6 File (java.io.File)2 Instrumentation (java.lang.instrument.Instrumentation)2 ArrayList (java.util.ArrayList)2 TypePool (net.bytebuddy.pool.TypePool)2 IOException (java.io.IOException)1 ClassDefinition (java.lang.instrument.ClassDefinition)1 Callable (java.util.concurrent.Callable)1 RandomString (net.bytebuddy.utility.RandomString)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 GradleException (org.gradle.api.GradleException)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1