use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.
the class MethodInvocationDynamicTest method testDynamicStaticBootstrap.
@Test
public void testDynamicStaticBootstrap() throws Exception {
when(methodDescription.isBootstrap()).thenReturn(true);
when(methodDescription.isStatic()).thenReturn(true);
StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).dynamic(FOO, returnType, Arrays.asList(firstType, secondType), Collections.singletonList(argument));
assertThat(stackManipulation.isValid(), is(true));
StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
assertThat(size.getSizeImpact(), is(0));
assertThat(size.getMaximalSize(), is(0));
verify(methodVisitor).visitInvokeDynamicInsn(FOO, "(" + FOO + BAR + ")" + QUX, new Handle(Opcodes.H_INVOKESTATIC, BAR, QUX, BAZ, false), argument);
}
use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation in project byte-buddy by raphw.
the class MethodInvocationGenericTest method testGenericMethodVirtualErasureEqual.
@Test
public void testGenericMethodVirtualErasureEqual() throws Exception {
when(methodReturnType.asErasure()).thenReturn(declaredErasure);
StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).virtual(targetType);
assertThat(stackManipulation.isValid(), is(true));
assertThat(stackManipulation, is(MethodInvocation.invoke(declaredMethod).virtual(targetType)));
}
use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation 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();
}
use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation in project beam by apache.
the class AvroByteBuddyUtils method readAndConvertParameter.
private static StackManipulation readAndConvertParameter(Class<?> constructorParameterType, int index) {
TypeConversionsFactory typeConversionsFactory = new AvroTypeConversionFactory();
// The types in the AVRO-generated constructor might be the types returned by Beam's Row class,
// so we have to convert the types used by Beam's Row class.
// We know that AVRO generates constructor parameters in the same order as fields
// in the schema, so we can just add the parameters sequentially.
TypeConversion<Type> convertType = typeConversionsFactory.createTypeConversion(true);
// Map the AVRO-generated type to the one Beam will use.
ForLoadedType convertedType = new ForLoadedType((Class) convertType.convert(TypeDescriptor.of(constructorParameterType)));
// This will run inside the generated creator. Read the parameter and convert it to the
// type required by the SpecificRecord constructor.
StackManipulation readParameter = new StackManipulation.Compound(MethodVariableAccess.REFERENCE.loadFrom(1), IntegerConstant.forValue(index), ArrayAccess.REFERENCE.load(), TypeCasting.to(convertedType));
// Convert to the parameter accepted by the SpecificRecord constructor.
return typeConversionsFactory.createSetterConversions(readParameter).convert(TypeDescriptor.of(constructorParameterType));
}
use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation in project curiostack by curioswitch.
the class DoParse method setRepeatedFieldValue.
/**
* Returns the {@link StackManipulation} for setting the value of a normal repeated field.
*
* <p>Roughly equivalent to:
*
* <pre>{@code
* ParseSupport.parseArrayStart(parser);
* while (!ParseSupport.checkArrayEnd(parser)) {
* builder.addFoo(readValue());
* }
* }</pre>
*/
private StackManipulation setRepeatedFieldValue(ProtoFieldInfo info, Label beforeReadField, LocalVariables<LocalVariable> locals, Map<String, FieldDescription> fieldsByName, StackManipulation setSingleValue) {
Label arrayStart = new Label();
StackManipulation.Compound beforeRead = new StackManipulation.Compound(locals.load(LocalVariable.parser), ParseSupport_parseArrayStart, new SetJumpTargetLabel(arrayStart), locals.load(LocalVariable.parser), ParseSupport_throwIfRepeatedNull, locals.load(LocalVariable.parser), ParseSupport_checkArrayEnd, new IfTrue(beforeReadField));
Label afterSet = new Label();
StackManipulation.Compound setValueAndPrepareForNext = new StackManipulation.Compound(setSingleValue, Removal.SINGLE, new SetJumpTargetLabel(afterSet), locals.load(LocalVariable.parser), Parser_nextValue, Removal.SINGLE, new Goto(arrayStart));
if (info.valueType() == Type.ENUM) {
// We special-case enum since we may need to skip unknown values.
return new StackManipulation.Compound(beforeRead, locals.load(LocalVariable.parser), readValue(info, fieldsByName, locals), locals.store(LocalVariable.intvalue), locals.load(LocalVariable.intvalue), IntegerConstant.forValue(-1), new IfEqual(int.class, afterSet), locals.load(LocalVariable.builder), locals.load(LocalVariable.intvalue), setValueAndPrepareForNext);
} else {
return new StackManipulation.Compound(beforeRead, locals.load(LocalVariable.builder), locals.load(LocalVariable.parser), readValue(info, fieldsByName, locals), setValueAndPrepareForNext);
}
}
Aggregations