Search in sources :

Example 1 with Plugin

use of net.bytebuddy.build.Plugin in project byte-buddy by raphw.

the class AgentBuilderDefaultTest method testBuildPluginWithEntryPoint.

@Test
public void testBuildPluginWithEntryPoint() throws Exception {
    Plugin plugin = mock(Plugin.class);
    EntryPoint entryPoint = mock(EntryPoint.class);
    ByteBuddy byteBuddy = mock(ByteBuddy.class);
    when(entryPoint.getByteBuddy()).thenReturn(byteBuddy);
    assertThat(AgentBuilder.Default.of(entryPoint, plugin), is((AgentBuilder) new AgentBuilder.Default(byteBuddy).with(new AgentBuilder.TypeStrategy.ForBuildEntryPoint(entryPoint)).type(plugin).transform(new AgentBuilder.Transformer.ForBuildPlugin(plugin))));
}
Also used : EntryPoint(net.bytebuddy.build.EntryPoint) ByteBuddy(net.bytebuddy.ByteBuddy) Plugin(net.bytebuddy.build.Plugin) Test(org.junit.Test)

Example 2 with Plugin

use of net.bytebuddy.build.Plugin in project byte-buddy by raphw.

the class TransformationAction method processOutputDirectory.

/**
     * Processes all class files within the given directory.
     *
     * @param root      The root directory to process.
     * @param classPath A list of class path elements expected by the processed classes.
     * @throws IOException If an I/O exception occurs.
     */
private void processOutputDirectory(File root, Iterable<? extends File> classPath) throws IOException {
    if (!root.isDirectory()) {
        throw new GradleException("Target location does not exist or is no directory: " + root);
    }
    ClassLoaderResolver classLoaderResolver = new ClassLoaderResolver();
    try {
        List<Plugin> plugins = new ArrayList<Plugin>(byteBuddyExtension.getTransformations().size());
        for (Transformation transformation : byteBuddyExtension.getTransformations()) {
            String plugin = transformation.getPlugin();
            try {
                plugins.add((Plugin) Class.forName(plugin, false, classLoaderResolver.resolve(transformation.getClassPath(root, classPath))).getDeclaredConstructor().newInstance());
                project.getLogger().info("Created plugin: {}", plugin);
            } catch (Exception exception) {
                if (exception instanceof GradleException) {
                    throw (GradleException) exception;
                }
                throw new GradleException("Cannot create plugin: " + transformation.getRawPlugin(), exception);
            }
        }
        EntryPoint entryPoint = byteBuddyExtension.getInitialization().getEntryPoint(classLoaderResolver, root, classPath);
        project.getLogger().info("Resolved entry point: {}", entryPoint);
        transform(root, classPath, entryPoint, plugins);
    } finally {
        classLoaderResolver.close();
    }
}
Also used : GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) EntryPoint(net.bytebuddy.build.EntryPoint) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) Plugin(net.bytebuddy.build.Plugin)

Example 3 with Plugin

use of net.bytebuddy.build.Plugin in project byte-buddy by raphw.

the class ByteBuddyMojo method processOutputDirectory.

/**
     * Processes all class files within the given directory.
     *
     * @param root      The root directory to process.
     * @param classPath A list of class path elements expected by the processed classes.
     * @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.
     */
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Applies Maven exception wrapper")
private void processOutputDirectory(File root, List<? extends String> classPath) throws MojoExecutionException, MojoFailureException, IOException {
    if (!root.isDirectory()) {
        throw new MojoExecutionException("Target location does not exist or is no directory: " + root);
    }
    ClassLoaderResolver classLoaderResolver = new ClassLoaderResolver(getLog(), repositorySystem, repositorySystemSession, remoteRepositories);
    try {
        List<Plugin> plugins = new ArrayList<Plugin>(transformations.size());
        for (Transformation transformation : transformations) {
            String plugin = transformation.getPlugin();
            try {
                plugins.add((Plugin) Class.forName(plugin, false, classLoaderResolver.resolve(transformation.asCoordinate(groupId, artifactId, version))).getDeclaredConstructor().newInstance());
                getLog().info("Created plugin: " + plugin);
            } catch (Exception exception) {
                throw new MojoExecutionException("Cannot create plugin: " + transformation.getRawPlugin(), exception);
            }
        }
        EntryPoint entryPoint = (initialization == null ? Initialization.makeDefault() : initialization).getEntryPoint(classLoaderResolver, groupId, artifactId, version);
        getLog().info("Resolved entry point: " + entryPoint);
        transform(root, entryPoint, classPath, plugins);
    } finally {
        classLoaderResolver.close();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) EntryPoint(net.bytebuddy.build.EntryPoint) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Plugin(net.bytebuddy.build.Plugin) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 4 with Plugin

use of net.bytebuddy.build.Plugin in project byte-buddy by raphw.

the class ByteBuddyMojo method processClassFile.

/**
     * Processes a class file.
     *
     * @param root                  The root directory to process.
     * @param file                  The class file to process.
     * @param byteBuddy             The Byte Buddy instance to use.
     * @param entryPoint            The transformation's entry point.
     * @param methodNameTransformer The method name transformer to use.
     * @param classFileLocator      The class file locator to use.
     * @param typePool              The type pool to query for type descriptions.
     * @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.
     */
private void processClassFile(File root, String file, ByteBuddy byteBuddy, EntryPoint entryPoint, MethodNameTransformer methodNameTransformer, ClassFileLocator classFileLocator, TypePool typePool, List<Plugin> plugins) throws MojoExecutionException, MojoFailureException {
    String typeName = file.replace('/', '.').substring(0, file.length() - CLASS_FILE_EXTENSION.length());
    getLog().debug("Processing class file: " + typeName);
    TypeDescription typeDescription = typePool.describe(typeName).resolve();
    DynamicType.Builder<?> builder;
    try {
        builder = entryPoint.transform(typeDescription, byteBuddy, classFileLocator, methodNameTransformer);
    } catch (Throwable throwable) {
        throw new MojoExecutionException("Cannot transform type: " + typeName, throwable);
    }
    boolean transformed = false;
    for (Plugin plugin : plugins) {
        try {
            if (plugin.matches(typeDescription)) {
                builder = plugin.apply(builder, typeDescription);
                transformed = true;
            }
        } catch (Throwable throwable) {
            throw new MojoExecutionException("Cannot apply " + plugin + " on " + typeName, throwable);
        }
    }
    if (transformed) {
        getLog().info("Transformed type: " + typeName);
        DynamicType dynamicType = builder.make();
        for (Map.Entry<TypeDescription, LoadedTypeInitializer> entry : dynamicType.getLoadedTypeInitializers().entrySet()) {
            if (failOnLiveInitializer && entry.getValue().isAlive()) {
                throw new MojoExecutionException("Cannot apply live initializer for " + entry.getKey());
            }
        }
        try {
            dynamicType.saveIn(root);
        } catch (IOException exception) {
            throw new MojoFailureException("Cannot save " + typeName + " in " + root, exception);
        }
    } else {
        getLog().debug("Skipping non-transformed type: " + typeName);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DynamicType(net.bytebuddy.dynamic.DynamicType) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) TypeDescription(net.bytebuddy.description.type.TypeDescription) Map(java.util.Map) Plugin(net.bytebuddy.build.Plugin)

Example 5 with Plugin

use of net.bytebuddy.build.Plugin in project byte-buddy by raphw.

the class TransformationAction method processClassFile.

/**
     * Processes a class file.
     *
     * @param root                  The root directory to process.
     * @param file                  The class file to process.
     * @param byteBuddy             The Byte Buddy instance to use.
     * @param entryPoint            The transformation's entry point.
     * @param methodNameTransformer The method name transformer to use.
     * @param classFileLocator      The class file locator to use.
     * @param typePool              The type pool to query for type descriptions.
     * @param plugins               The plugins to apply.
     */
private void processClassFile(File root, String file, ByteBuddy byteBuddy, EntryPoint entryPoint, MethodNameTransformer methodNameTransformer, ClassFileLocator classFileLocator, TypePool typePool, List<Plugin> plugins) {
    String typeName = file.replace('/', '.').substring(0, file.length() - CLASS_FILE_EXTENSION.length());
    project.getLogger().debug("Processing class file: {}", typeName);
    TypeDescription typeDescription = typePool.describe(typeName).resolve();
    DynamicType.Builder<?> builder;
    try {
        builder = entryPoint.transform(typeDescription, byteBuddy, classFileLocator, methodNameTransformer);
    } catch (Throwable throwable) {
        throw new GradleException("Cannot transform type: " + typeName, throwable);
    }
    boolean transformed = false;
    for (Plugin plugin : plugins) {
        try {
            if (plugin.matches(typeDescription)) {
                builder = plugin.apply(builder, typeDescription);
                transformed = true;
            }
        } catch (Throwable throwable) {
            throw new GradleException("Cannot apply " + plugin + " on " + typeName, throwable);
        }
    }
    if (transformed) {
        project.getLogger().info("Transformed type: {}", typeName);
        DynamicType dynamicType = builder.make();
        for (Map.Entry<TypeDescription, LoadedTypeInitializer> entry : dynamicType.getLoadedTypeInitializers().entrySet()) {
            if (byteBuddyExtension.isFailOnLiveInitializer() && entry.getValue().isAlive()) {
                throw new GradleException("Cannot apply live initializer for " + entry.getKey());
            }
        }
        try {
            dynamicType.saveIn(root);
        } catch (IOException exception) {
            throw new GradleException("Cannot save " + typeName + " in " + root, exception);
        }
    } else {
        project.getLogger().debug("Skipping non-transformed type: {}", typeName);
    }
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) DynamicType(net.bytebuddy.dynamic.DynamicType) GradleException(org.gradle.api.GradleException) TypeDescription(net.bytebuddy.description.type.TypeDescription) IOException(java.io.IOException) Map(java.util.Map) Plugin(net.bytebuddy.build.Plugin)

Aggregations

Plugin (net.bytebuddy.build.Plugin)6 IOException (java.io.IOException)4 EntryPoint (net.bytebuddy.build.EntryPoint)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 DynamicType (net.bytebuddy.dynamic.DynamicType)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 GradleException (org.gradle.api.GradleException)2 Test (org.junit.Test)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ByteBuddy (net.bytebuddy.ByteBuddy)1