Search in sources :

Example 21 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class AgentBuilderDefaultApplicationTest method testAdviceTransformer.

@Test
@IntegrationRule.Enforce
public void testAdviceTransformer() throws Exception {
    assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
    ClassFileTransformer classFileTransformer = new AgentBuilder.Default().with(poolStrategy).ignore(none()).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE).type(ElementMatchers.is(Foo.class), ElementMatchers.is(classLoader)).transform(new AgentBuilder.Transformer.ForAdvice().with(poolStrategy).with(AgentBuilder.LocationStrategy.ForClassLoader.STRONG).include(BarAdvice.class.getClassLoader()).with(Assigner.DEFAULT).withExceptionHandler(Removal.SINGLE).advice(named(FOO), BarAdvice.class.getName())).installOnByteBuddyAgent();
    try {
        Class<?> type = classLoader.loadClass(Foo.class.getName());
        assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) (FOO + BAR)));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Instrumentation(java.lang.instrument.Instrumentation) Test(org.junit.Test)

Example 22 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project byte-buddy by raphw.

the class LambdaFactory method register.

/**
     * Registers a class file transformer together with a factory for creating a lambda expression. It is possible to call this method independently
     * of the class loader's context as the supplied injector makes sure that the manipulated collection is the one that is held by the system class
     * loader.
     *
     * @param classFileTransformer The class file transformer to register.
     * @param classFileFactory     The lambda class file factory to use. This factory must define a visible instance method with the signature
     *                             {@code byte[] make(Object, String, Object, Object, Object, Object, boolean, List, List, Collection}. The arguments provided
     *                             are the invokedynamic call site's lookup object, the lambda method's name, the factory method's type, the lambda method's
     *                             type, the target method's handle, the specialized method type of the lambda expression, a boolean to indicate
     *                             serializability, a list of marker interfaces, a list of additional bridges and a collection of class file transformers to
     *                             apply.
     * @return {@code true} if this is the first registered transformer. This indicates that the {@code LambdaMetafactory} must be instrumented to delegate
     * to this alternative factory.
     */
@SuppressWarnings("all")
public static boolean register(ClassFileTransformer classFileTransformer, Object classFileFactory) {
    try {
        TypeDescription typeDescription = new TypeDescription.ForLoadedType(LambdaFactory.class);
        Class<?> lambdaFactory = ClassInjector.UsingReflection.ofSystemClassLoader().inject(Collections.singletonMap(typeDescription, ClassFileLocator.ForClassLoader.read(LambdaFactory.class).resolve())).get(typeDescription);
        @SuppressWarnings("unchecked") Map<ClassFileTransformer, Object> classFileTransformers = (Map<ClassFileTransformer, Object>) lambdaFactory.getField(FIELD_NAME).get(null);
        synchronized (classFileTransformers) {
            try {
                return classFileTransformers.isEmpty();
            } finally {
                classFileTransformers.put(classFileTransformer, lambdaFactory.getConstructor(Object.class, Method.class).newInstance(classFileFactory, classFileFactory.getClass().getMethod("make", Object.class, String.class, Object.class, Object.class, Object.class, Object.class, boolean.class, List.class, List.class, Collection.class)));
            }
        }
    } catch (RuntimeException exception) {
        throw exception;
    } catch (Exception exception) {
        throw new IllegalStateException("Could not register class file transformer", exception);
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) TypeDescription(net.bytebuddy.description.type.TypeDescription) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 23 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project spring-framework by spring-projects.

the class ShadowingClassLoader method applyTransformers.

private byte[] applyTransformers(String name, byte[] bytes) {
    String internalName = StringUtils.replace(name, ".", "/");
    try {
        for (ClassFileTransformer transformer : this.classFileTransformers) {
            byte[] transformed = transformer.transform(this, internalName, null, null, bytes);
            bytes = (transformed != null ? transformed : bytes);
        }
        return bytes;
    } catch (IllegalClassFormatException ex) {
        throw new IllegalStateException(ex);
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Example 24 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project spring-framework by spring-projects.

the class ReflectiveLoadTimeWeaverTests method testCtorWithClassLoaderThatDoesNotExposeAGetThrowawayClassLoaderMethodIsOkay.

@Test
public void testCtorWithClassLoaderThatDoesNotExposeAGetThrowawayClassLoaderMethodIsOkay() {
    JustAddTransformerClassLoader classLoader = new JustAddTransformerClassLoader();
    ReflectiveLoadTimeWeaver weaver = new ReflectiveLoadTimeWeaver(classLoader);
    weaver.addTransformer(new ClassFileTransformer() {

        @Override
        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
            return "CAFEDEAD".getBytes();
        }
    });
    assertEquals(1, classLoader.getNumTimesGetThrowawayClassLoaderCalled());
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) Test(org.junit.Test)

Example 25 with ClassFileTransformer

use of java.lang.instrument.ClassFileTransformer in project jdk8u_jdk by JetBrains.

the class TransformerManager method setNativeMethodPrefix.

boolean setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) {
    TransformerInfo[] transformerList = getSnapshotTransformerList();
    for (int x = 0; x < transformerList.length; x++) {
        TransformerInfo transformerInfo = transformerList[x];
        ClassFileTransformer aTransformer = transformerInfo.transformer();
        if (aTransformer == transformer) {
            transformerInfo.setPrefix(prefix);
            return true;
        }
    }
    return false;
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer)

Aggregations

ClassFileTransformer (java.lang.instrument.ClassFileTransformer)54 Test (org.junit.Test)38 Instrumentation (java.lang.instrument.Instrumentation)35 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)7 Callable (java.util.concurrent.Callable)7 SimpleType (net.bytebuddy.test.packaging.SimpleType)6 ProtectionDomain (java.security.ProtectionDomain)5 ByteBuddy (net.bytebuddy.ByteBuddy)5 URLClassLoader (java.net.URLClassLoader)3 MatchableClassFileTransformer (com.navercorp.pinpoint.profiler.plugin.MatchableClassFileTransformer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 PersistenceProvider (javax.persistence.spi.PersistenceProvider)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 SimpleOptionalType (net.bytebuddy.test.packaging.SimpleOptionalType)2 TempClassLoader (org.apache.openejb.core.TempClassLoader)2 PersistenceClassLoaderHandler (org.apache.openejb.persistence.PersistenceClassLoaderHandler)2 PersistenceUnitInfoImpl (org.apache.openejb.persistence.PersistenceUnitInfoImpl)2