use of net.bytebuddy.implementation.bytecode.ByteCodeAppender in project byte-buddy by raphw.
the class InstrumentedTypeDefaultTest method testWithTypeInitializerSingle.
@Test
public void testWithTypeInitializerSingle() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
ByteCodeAppender byteCodeAppender = mock(ByteCodeAppender.class);
instrumentedType = instrumentedType.withInitializer(byteCodeAppender);
TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
assertThat(typeInitializer.isDefined(), is(true));
MethodDescription methodDescription = mock(MethodDescription.class);
typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
verify(byteCodeAppender).apply(methodVisitor, implementationContext, methodDescription);
}
use of net.bytebuddy.implementation.bytecode.ByteCodeAppender in project byte-buddy by raphw.
the class InstrumentedTypeDefaultTest method testWithTypeInitializerDouble.
@Test
public void testWithTypeInitializerDouble() throws Exception {
InstrumentedType instrumentedType = makePlainInstrumentedType();
assertThat(instrumentedType.getDeclaredFields().size(), is(0));
ByteCodeAppender first = mock(ByteCodeAppender.class), second = mock(ByteCodeAppender.class);
MethodDescription methodDescription = mock(MethodDescription.class);
when(first.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
when(second.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
assertThat(typeInitializer.isDefined(), is(true));
typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
verify(first).apply(methodVisitor, implementationContext, methodDescription);
verify(second).apply(methodVisitor, implementationContext, methodDescription);
}
use of net.bytebuddy.implementation.bytecode.ByteCodeAppender in project beam by apache.
the class ByteBuddyUtils method createCollectionTransformFunction.
// When processing a container (e.g. List<T>) we need to recursively process the element type.
// This function
// generates a subclass of Function that can be used to recursively transform each element of the
// container.
static Class createCollectionTransformFunction(Type fromType, Type toType, Function<StackManipulation, StackManipulation> convertElement) {
// Generate a TypeDescription for the class we want to generate.
TypeDescription.Generic functionGenericType = TypeDescription.Generic.Builder.parameterizedType(Function.class, Primitives.wrap((Class) fromType), Primitives.wrap((Class) toType)).build();
DynamicType.Builder<Function> builder = (DynamicType.Builder<Function>) BYTE_BUDDY.with(new InjectPackageStrategy((Class) fromType)).subclass(functionGenericType).method(ElementMatchers.named("apply")).intercept(new Implementation() {
@Override
public ByteCodeAppender appender(Target target) {
return (methodVisitor, implementationContext, instrumentedMethod) -> {
// this + method parameters.
int numLocals = 1 + instrumentedMethod.getParameters().size();
StackManipulation readValue = MethodVariableAccess.REFERENCE.loadFrom(1);
StackManipulation stackManipulation = new StackManipulation.Compound(convertElement.apply(readValue), MethodReturn.REFERENCE);
StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
return new ByteCodeAppender.Size(size.getMaximalSize(), numLocals);
};
}
@Override
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType;
}
});
return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(((Class) fromType).getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded();
}
Aggregations