Search in sources :

Example 21 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class ClassLoadingStrategyForBootstrapInjectionTest method testClassLoaderInjection.

@Test
@AgentAttachmentRule.Enforce
public void testClassLoaderInjection() throws Exception {
    ClassLoadingStrategy<ClassLoader> bootstrapStrategy = new ClassLoadingStrategy.ForBootstrapInjection(ByteBuddyAgent.install(), file);
    String name = BAR + RandomString.make();
    ClassLoader classLoader = new URLClassLoader(new URL[0], null);
    DynamicType dynamicType = new ByteBuddy().subclass(Object.class).name(name).make();
    Map<TypeDescription, Class<?>> loaded = bootstrapStrategy.load(classLoader, Collections.singletonMap(dynamicType.getTypeDescription(), dynamicType.getBytes()));
    assertThat(loaded.size(), is(1));
    assertThat(loaded.get(dynamicType.getTypeDescription()).getName(), is(name));
    assertThat(loaded.get(dynamicType.getTypeDescription()).getClassLoader(), is(classLoader));
}
Also used : URLClassLoader(java.net.URLClassLoader) DynamicType(net.bytebuddy.dynamic.DynamicType) URLClassLoader(java.net.URLClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) TypeDescription(net.bytebuddy.description.type.TypeDescription) RandomString(net.bytebuddy.utility.RandomString) Test(org.junit.Test)

Example 22 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class ClassInjectorUsingInstrumentationTest method testBootstrapInjection.

@Test
@AgentAttachmentRule.Enforce
public void testBootstrapInjection() throws Exception {
    ClassInjector classInjector = ClassInjector.UsingInstrumentation.of(folder, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, ByteBuddyAgent.install());
    String name = FOO + RandomString.make();
    DynamicType dynamicType = new ByteBuddy().subclass(Object.class).name(name).make();
    Map<TypeDescription, Class<?>> types = classInjector.inject(Collections.singletonMap(dynamicType.getTypeDescription(), dynamicType.getBytes()));
    assertThat(types.size(), is(1));
    assertThat(types.get(dynamicType.getTypeDescription()).getName(), is(name));
    assertThat(types.get(dynamicType.getTypeDescription()).getClassLoader(), nullValue(ClassLoader.class));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) TypeDescription(net.bytebuddy.description.type.TypeDescription) RandomString(net.bytebuddy.utility.RandomString) Test(org.junit.Test)

Example 23 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class ClassLoadingStrategyForBootstrapInjectionTest method testBootstrapInjection.

@Test
@AgentAttachmentRule.Enforce
public void testBootstrapInjection() throws Exception {
    ClassLoadingStrategy<ClassLoader> bootstrapStrategy = new ClassLoadingStrategy.ForBootstrapInjection(ByteBuddyAgent.install(), file);
    String name = FOO + RandomString.make();
    DynamicType dynamicType = new ByteBuddy().subclass(Object.class).name(name).make();
    Map<TypeDescription, Class<?>> loaded = bootstrapStrategy.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, Collections.singletonMap(dynamicType.getTypeDescription(), dynamicType.getBytes()));
    assertThat(loaded.size(), is(1));
    assertThat(loaded.get(dynamicType.getTypeDescription()).getName(), is(name));
    assertThat(loaded.get(dynamicType.getTypeDescription()).getClassLoader(), nullValue(ClassLoader.class));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) URLClassLoader(java.net.URLClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) TypeDescription(net.bytebuddy.description.type.TypeDescription) RandomString(net.bytebuddy.utility.RandomString) Test(org.junit.Test)

Example 24 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType 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 25 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType 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

DynamicType (net.bytebuddy.dynamic.DynamicType)38 Test (org.junit.Test)33 ByteBuddy (net.bytebuddy.ByteBuddy)30 TypeDescription (net.bytebuddy.description.type.TypeDescription)23 Field (java.lang.reflect.Field)10 URLClassLoader (java.net.URLClassLoader)7 Type (java.lang.reflect.Type)5 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)5 GenericType (net.bytebuddy.test.scope.GenericType)5 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)4 RandomString (net.bytebuddy.utility.RandomString)4 TargetType (net.bytebuddy.dynamic.TargetType)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 Callable (java.util.concurrent.Callable)2 Plugin (net.bytebuddy.build.Plugin)2 MethodDescription (net.bytebuddy.description.method.MethodDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 StubMethod (net.bytebuddy.implementation.StubMethod)2