Search in sources :

Example 91 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 MethodVariableAccessOfMethodArgumentsTest method testNonStaticMethod.

@Test
public void testNonStaticMethod() throws Exception {
    StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(PARAMETER_STACK_SIZE));
    assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 2);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 92 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 MethodVariableAccessOfMethodArgumentsTest method testBridgeMethodWithCasting.

@Test
public void testBridgeMethodWithCasting() throws Exception {
    when(secondRawParameterType.asErasure()).thenReturn(secondRawParameterType);
    when(bridgeMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(bridgeMethod, secondParameterType, secondParameterType));
    StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription).asBridgeOf(bridgeMethod);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(PARAMETER_STACK_SIZE));
    assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verify(methodVisitor).visitTypeInsn(Opcodes.CHECKCAST, FOO);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 2);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 93 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 MethodVariableAccessOfMethodArgumentsTest method testStaticMethod.

@Test
public void testStaticMethod() throws Exception {
    when(methodDescription.isStatic()).thenReturn(true);
    StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(PARAMETER_STACK_SIZE));
    assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verifyNoMoreInteractions(methodVisitor);
}
Also used : StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) Test(org.junit.Test)

Example 94 with StackManipulation

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation in project curiostack by curioswitch.

the class DoWrite method apply.

@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    Map<String, FieldDescription> fieldsByName = CodeGenUtil.fieldsByName(implementationContext);
    List<StackManipulation> stackManipulations = new ArrayList<>();
    final StackManipulation getDefaultInstance;
    try {
        getDefaultInstance = invoke(messageClass.getDeclaredMethod("getDefaultInstance"));
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Could not find getDefaultInstance on a Message class.");
    }
    LocalVariables<LocalVariable> locals = LocalVariables.builderForMethod(instrumentedMethod, LocalVariable.values()).add(Iterator.class, LocalVariable.iterator).add(Map.Entry.class, LocalVariable.entry).build();
    stackManipulations.add(locals.initialize());
    // based on the includeDefaults parameter.
    for (FieldDescriptor f : descriptor.getFields()) {
        ProtoFieldInfo field = new ProtoFieldInfo(f, prototype);
        StackManipulation getValue = new StackManipulation.Compound(locals.load(LocalVariable.message), invoke(field.getValueMethod()));
        Label afterSerializeField = new Label();
        // }
        if (!includeDefaults || // one-of).
        field.isInOneof() || // a message has itself as a sub-field.
        (field.descriptor().isOptional() && field.valueJavaType() == JavaType.MESSAGE)) {
            stackManipulations.add(checkDefaultValue(field, locals, getValue, getDefaultInstance, afterSerializeField));
        }
        stackManipulations.addAll(Arrays.asList(locals.load(LocalVariable.gen), FieldAccess.forField(fieldsByName.get(CodeGenUtil.fieldNameForSerializedFieldName(field))).read(), JsonGenerator_writeFieldName_SerializableString));
        // gen.writeEndObject();
        if (field.isMapField()) {
            final StackManipulation keyToString;
            switch(field.mapKeyField().descriptor().getType()) {
                case INT32:
                case SINT32:
                case SFIXED32:
                    keyToString = Integer_toString;
                    break;
                case INT64:
                case SINT64:
                case SFIXED64:
                    keyToString = Long_toString;
                    break;
                case BOOL:
                    keyToString = Boolean_toString;
                    break;
                case UINT32:
                case FIXED32:
                    keyToString = new StackManipulation.Compound(SerializeSupport_normalizeUnsignedInt32, Long_toString);
                    break;
                case UINT64:
                case FIXED64:
                    keyToString = SerializeSupport_normalizeUnsignedInt64;
                    break;
                case STRING:
                    keyToString = Trivial.INSTANCE;
                    break;
                default:
                    throw new IllegalStateException("Unexpected map key type: " + field.mapKeyField().descriptor().getType());
            }
            final Label loopStart = new Label();
            final Label loopEnd = new Label();
            final StackManipulation printValue = printValue(fieldsByName, field);
            final StackManipulation printMapFieldValue = new StackManipulation.Compound(locals.load(LocalVariable.gen), JsonGenerator_writeStartObject, getValue, Map_entrySet, Set_iterator, locals.store(LocalVariable.iterator), new SetJumpTargetLabel(loopStart), locals.load(LocalVariable.iterator), Iterator_hasNext, new IfFalse(loopEnd), locals.load(LocalVariable.iterator), Iterator_next, TypeCasting.to(new ForLoadedType(Entry.class)), locals.store(LocalVariable.entry), locals.load(LocalVariable.gen), locals.load(LocalVariable.entry), Map_Entry_getKey, unbox(field.mapKeyField()), keyToString, JsonGenerator_writeFieldName_String, locals.load(LocalVariable.entry), Map_Entry_getValue, unbox(field.valueField()), locals.load(LocalVariable.gen), printValue, new Goto(loopStart), new SetJumpTargetLabel(loopEnd), locals.load(LocalVariable.gen), JsonGenerator_writeEndObject);
            stackManipulations.add(printMapFieldValue);
        } else {
            // Simply calls the SerializeSupport method that prints out this field. Any iteration will
            // be handled there.
            // 
            // e.g.,
            // SerializeSupport.printUnsignedInt32(message.getFoo());
            // SerializeSupport.printRepeatedString(message.getBar());
            final StackManipulation printValue = printValue(fieldsByName, field);
            stackManipulations.addAll(Arrays.asList(getValue, locals.load(LocalVariable.gen), printValue));
        }
        stackManipulations.add(new SetJumpTargetLabel(afterSerializeField));
    }
    stackManipulations.add(MethodReturn.VOID);
    StackManipulation.Size operandStackSize = new StackManipulation.Compound(stackManipulations).apply(methodVisitor, implementationContext);
    return new Size(operandStackSize.getMaximalSize(), locals.stackSize());
}
Also used : Goto(org.curioswitch.common.protobuf.json.bytebuddy.Goto) StackManipulation(net.bytebuddy.implementation.bytecode.StackManipulation) ForLoadedType(net.bytebuddy.description.type.TypeDescription.ForLoadedType) ArrayList(java.util.ArrayList) SetJumpTargetLabel(org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel) Label(net.bytebuddy.jar.asm.Label) SerializableString(com.fasterxml.jackson.core.SerializableString) ByteString(com.google.protobuf.ByteString) FieldDescription(net.bytebuddy.description.field.FieldDescription) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Entry(java.util.Map.Entry) SetJumpTargetLabel(org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel) IfFalse(org.curioswitch.common.protobuf.json.bytebuddy.IfFalse)

Aggregations

StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)92 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 MethodDescription (net.bytebuddy.description.method.MethodDescription)4 ForLoadedType (net.bytebuddy.description.type.TypeDescription.ForLoadedType)4 Goto (org.curioswitch.common.protobuf.json.bytebuddy.Goto)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4 ArrayList (java.util.ArrayList)3 Label (net.bytebuddy.jar.asm.Label)3 SetJumpTargetLabel (org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel)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 IfTrue (org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)2