Search in sources :

Example 1 with TypePool

use of net.bytebuddy.pool.TypePool in project byte-buddy by raphw.

the class ByteBuddyTutorialExamplesTest method testTutorialGettingStartedTypePool.

@Test
public void testTutorialGettingStartedTypePool() throws Exception {
    TypePool typePool = TypePool.Default.ofClassPath();
    // Assure repeatability.
    ClassLoader classLoader = new URLClassLoader(new URL[0], null);
    new ByteBuddy().redefine(typePool.describe(UnloadedBar.class.getName()).resolve(), ClassFileLocator.ForClassLoader.ofClassPath()).defineField("qux", String.class).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION);
    assertThat(classLoader.loadClass(UnloadedBar.class.getName()).getDeclaredField("qux"), notNullValue(java.lang.reflect.Field.class));
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) TypePool(net.bytebuddy.pool.TypePool) Test(org.junit.Test)

Example 2 with TypePool

use of net.bytebuddy.pool.TypePool 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 3 with TypePool

use of net.bytebuddy.pool.TypePool 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)

Aggregations

TypePool (net.bytebuddy.pool.TypePool)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ByteBuddy (net.bytebuddy.ByteBuddy)2 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)2 URLClassLoader (java.net.URLClassLoader)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 GradleException (org.gradle.api.GradleException)1 Test (org.junit.Test)1