Search in sources :

Example 16 with ClassFileLocator

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

the class ByteBuddyMojo 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 MojoExecutionException If the user configuration results in an error.
     * @throws MojoFailureException   If the plugin application raises an error.
     * @throws IOException            If an I/O exception occurs.
     */
private void transform(File root, EntryPoint entryPoint, List<? extends String> classPath, List<Plugin> plugins) throws MojoExecutionException, MojoFailureException, IOException {
    List<ClassFileLocator> classFileLocators = new ArrayList<ClassFileLocator>(classPath.size() + 1);
    classFileLocators.add(new ClassFileLocator.ForFolder(root));
    for (String target : classPath) {
        File artifact = new File(target);
        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());
        getLog().info("Processing class files located in in: " + root);
        ByteBuddy byteBuddy;
        try {
            byteBuddy = entryPoint.getByteBuddy();
        } catch (Throwable throwable) {
            throw new MojoExecutionException("Cannot create Byte Buddy instance", throwable);
        }
        processDirectory(root, root, byteBuddy, entryPoint, suffix == null || suffix.isEmpty() ? MethodNameTransformer.Suffixing.withRandomSuffix() : new MethodNameTransformer.Suffixing(suffix), classFileLocator, typePool, plugins);
    } finally {
        classFileLocator.close();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) ByteBuddy(net.bytebuddy.ByteBuddy) ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) File(java.io.File) TypePool(net.bytebuddy.pool.TypePool)

Example 17 with ClassFileLocator

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

the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testTypeIsCached.

@Test
public void testTypeIsCached() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
    TypePool typePool = TypePool.Default.WithLazyResolution.of(classFileLocator);
    TypePool.Resolution resolution = typePool.describe(Object.class.getName());
    assertThat(resolution.resolve().getModifiers(), CoreMatchers.is(TypeDescription.OBJECT.getModifiers()));
    assertThat(resolution.resolve().getInterfaces(), CoreMatchers.is(TypeDescription.OBJECT.getInterfaces()));
    assertThat(typePool.describe(Object.class.getName()).resolve(), CoreMatchers.is(resolution.resolve()));
    verify(classFileLocator).locate(Object.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) Test(org.junit.Test) AbstractTypeDescriptionTest(net.bytebuddy.description.type.AbstractTypeDescriptionTest)

Example 18 with ClassFileLocator

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

the class TypePoolDefaultWithLazyResolutionTypeDescriptionTest method testGenericSuperInterfaceHierarchyResolutionIsLazy.

@Test
public void testGenericSuperInterfaceHierarchyResolutionIsLazy() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofClassPath());
    assertThat(describe(GenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()).getInterfaces().getOnly().asErasure(), CoreMatchers.is((TypeDescription) new TypeDescription.ForLoadedType(SampleGenericInterface.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)

Example 19 with ClassFileLocator

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

the class ClassReloadingStrategyTest method testResetEmptyNoEffect.

@Test
public void testResetEmptyNoEffect() throws Exception {
    Instrumentation instrumentation = mock(Instrumentation.class);
    ClassFileLocator classFileLocator = mock(ClassFileLocator.class);
    when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
    ClassReloadingStrategy.of(instrumentation).reset(classFileLocator);
    verify(instrumentation, times(2)).isRedefineClassesSupported();
    verifyNoMoreInteractions(instrumentation);
    verifyZeroInteractions(classFileLocator);
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 20 with ClassFileLocator

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

the class ClassReloadingStrategyTest method testAnonymousType.

@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce(retransformsClasses = true)
public void testAnonymousType() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(Class.forName(LAMBDA_SAMPLE_FACTORY)), ByteArrayClassLoader.PersistenceHandler.MANIFEST);
    Instrumentation instrumentation = ByteBuddyAgent.install();
    Class<?> factory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY);
    @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) factory.getDeclaredMethod("nonCapturing").invoke(factory.getDeclaredConstructor().newInstance());
    // Anonymous types can only be reset to their original format, if a retransformation is applied.
    ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(instrumentation, ClassReloadingStrategy.Strategy.RETRANSFORMATION).preregistered(instance.getClass());
    ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.of(instrumentation, instance.getClass());
    try {
        assertThat(instance.call(), is(FOO));
        new ByteBuddy().redefine(instance.getClass(), classFileLocator).method(named("call")).intercept(FixedValue.value(BAR)).make().load(instance.getClass().getClassLoader(), classReloadingStrategy);
        assertThat(instance.call(), is(BAR));
    } finally {
        classReloadingStrategy.reset(classFileLocator, instance.getClass());
        assertThat(instance.call(), is(FOO));
    }
}
Also used : ClassFileLocator(net.bytebuddy.dynamic.ClassFileLocator) Instrumentation(java.lang.instrument.Instrumentation) ByteBuddy(net.bytebuddy.ByteBuddy) RandomString(net.bytebuddy.utility.RandomString) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

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