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);
}
}
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);
}
}
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);
}
}
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());
}
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;
}
Aggregations