Search in sources :

Example 46 with StackManipulation

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);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Handle(org.objectweb.asm.Handle) Test(org.junit.Test)

Example 47 with StackManipulation

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)));
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 48 with StackManipulation

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();
}
Also used : Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ArrayFactory(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayFactory) ByteBuffer(java.nio.ByteBuffer) Opcodes(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Opcodes) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Label(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label) ClassUtils(org.apache.commons.lang3.ClassUtils) MethodVisitor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.MethodVisitor) NullConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.NullConstant) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) Method(java.lang.reflect.Method) Internal(org.apache.beam.sdk.annotations.Internal) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) Collection(java.util.Collection) Set(java.util.Set) ReadableInstant(org.joda.time.ReadableInstant) Objects(java.util.Objects) List(java.util.List) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) BaseLocal(org.joda.time.base.BaseLocal) Context(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation.Context) Typing(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner.Typing) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) SortedMap(java.util.SortedMap) ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) RandomString(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.utility.RandomString) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ArrayUtils(org.apache.commons.lang3.ArrayUtils) NamingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy) Constructor(java.lang.reflect.Constructor) FieldAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.FieldAccess) ArrayList(java.util.ArrayList) TypeParameter(org.apache.beam.sdk.values.TypeParameter) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) Collections2(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) Parameter(java.lang.reflect.Parameter) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Assigner(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) ClassWriter(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.ClassWriter) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) ForLoadedConstructor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedConstructor) Instant(org.joda.time.Instant) ReadablePartial(org.joda.time.ReadablePartial) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) Collections(java.util.Collections) BaseNameResolver(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy.SuffixingRandom.BaseNameResolver) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) Primitives(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Primitives) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType)

Example 49 with StackManipulation

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));
}
Also used : DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Type(java.lang.reflect.Type) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) AvroTypeConversionFactory(org.apache.beam.sdk.schemas.utils.AvroUtils.AvroTypeConversionFactory) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory)

Example 50 with StackManipulation

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);
    }
}
Also used : IfEqual(org.curioswitch.common.protobuf.json.bytebuddy.IfEqual) Goto(org.curioswitch.common.protobuf.json.bytebuddy.Goto) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) SetJumpTargetLabel(org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel) Label(net.bytebuddy.jar.asm.Label) SetJumpTargetLabel(org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel) IfTrue(org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)

Aggregations

StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)93 Test (org.junit.Test)85 TypeDescription (net.bytebuddy.description.type.TypeDescription)18 Implementation (net.bytebuddy.implementation.Implementation)5 Assigner (net.bytebuddy.implementation.bytecode.assign.Assigner)5 Goto (org.curioswitch.common.protobuf.json.bytebuddy.Goto)5 MethodDescription (net.bytebuddy.description.method.MethodDescription)4 ForLoadedType (net.bytebuddy.description.type.TypeDescription.ForLoadedType)4 Label (net.bytebuddy.jar.asm.Label)4 SetJumpTargetLabel (org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 ArrayList (java.util.ArrayList)3 IfTrue (org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)3 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)2 Type (java.lang.reflect.Type)2 FieldDescription (net.bytebuddy.description.field.FieldDescription)2 JavaConstant (net.bytebuddy.utility.JavaConstant)2 ForLoadedType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType)2 DynamicType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType)2 StackManipulation (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation)2