Search in sources :

Example 6 with LoadedTypeInitializer

use of net.bytebuddy.implementation.LoadedTypeInitializer in project byte-buddy by raphw.

the class InstrumentedTypeDefaultTest method testWithLoadedTypeInitializerInitial.

@Test
public void testWithLoadedTypeInitializerInitial() throws Exception {
    LoadedTypeInitializer loadedTypeInitializer = makePlainInstrumentedType().getLoadedTypeInitializer();
    assertThat(loadedTypeInitializer.isAlive(), is(false));
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) Test(org.junit.Test)

Example 7 with LoadedTypeInitializer

use of net.bytebuddy.implementation.LoadedTypeInitializer 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 8 with LoadedTypeInitializer

use of net.bytebuddy.implementation.LoadedTypeInitializer 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)

Example 9 with LoadedTypeInitializer

use of net.bytebuddy.implementation.LoadedTypeInitializer in project byte-buddy by raphw.

the class AgentBuilderDefaultTest method testAuxiliaryTypeInitialization.

@Test
public void testAuxiliaryTypeInitialization() throws Exception {
    when(dynamicType.getAuxiliaryTypes()).thenReturn(Collections.<TypeDescription, byte[]>singletonMap(new TypeDescription.ForLoadedType(AUXILIARY), QUX));
    Map<TypeDescription, LoadedTypeInitializer> loadedTypeInitializers = new HashMap<TypeDescription, LoadedTypeInitializer>();
    loadedTypeInitializers.put(new TypeDescription.ForLoadedType(REDEFINED), loadedTypeInitializer);
    LoadedTypeInitializer auxiliaryInitializer = mock(LoadedTypeInitializer.class);
    loadedTypeInitializers.put(new TypeDescription.ForLoadedType(AUXILIARY), auxiliaryInitializer);
    when(dynamicType.getLoadedTypeInitializers()).thenReturn(loadedTypeInitializers);
    when(dynamicType.getBytes()).thenReturn(BAZ);
    when(resolution.resolve()).thenReturn(new TypeDescription.ForLoadedType(REDEFINED));
    when(typeMatcher.matches(new TypeDescription.ForLoadedType(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain())).thenReturn(true);
    ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy).with(initializationStrategy).with(poolStrategy).with(typeStrategy).with(installationListener).with(listener).disableNativeMethodPrefix().ignore(none()).type(typeMatcher).transform(transformer).installOn(instrumentation);
    assertThat(transform(classFileTransformer, JavaModule.ofType(REDEFINED), REDEFINED.getClassLoader(), REDEFINED.getName(), REDEFINED, REDEFINED.getProtectionDomain(), QUX), is(BAZ));
    verify(listener).onTransformation(new TypeDescription.ForLoadedType(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true, dynamicType);
    verify(listener).onComplete(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
    verifyNoMoreInteractions(listener);
    verify(instrumentation).addTransformer(classFileTransformer, false);
    verifyNoMoreInteractions(instrumentation);
    verify(initializationStrategy).dispatcher();
    verifyNoMoreInteractions(initializationStrategy);
    verify(dispatcher).apply(builder);
    verify(dispatcher).register(dynamicType, REDEFINED.getClassLoader(), new AgentBuilder.Default.Transformation.Simple.Resolution.BootstrapClassLoaderCapableInjectorFactory(AgentBuilder.Default.BootstrapInjectionStrategy.Disabled.INSTANCE, REDEFINED.getClassLoader(), REDEFINED.getProtectionDomain()));
    verifyNoMoreInteractions(dispatcher);
    verify(installationListener).onInstall(instrumentation, classFileTransformer);
    verifyNoMoreInteractions(installationListener);
}
Also used : LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Aggregations

LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)9 Test (org.junit.Test)6 TypeDescription (net.bytebuddy.description.type.TypeDescription)5 Map (java.util.Map)3 DynamicType (net.bytebuddy.dynamic.DynamicType)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 HashMap (java.util.HashMap)2 Plugin (net.bytebuddy.build.Plugin)2 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)2 AuxiliaryType (net.bytebuddy.implementation.auxiliary.AuxiliaryType)2 ClassInjector (net.bytebuddy.dynamic.loading.ClassInjector)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 GradleException (org.gradle.api.GradleException)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1