Search in sources :

Example 1 with IfRefsEqual

use of org.curioswitch.common.protobuf.json.bytebuddy.IfRefsEqual in project curiostack by curioswitch.

the class DoParse method apply.

@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    Map<String, FieldDescription> fieldsByName = CodeGenUtil.fieldsByName(implementationContext);
    // Fields stored in order of number, so to get the highest number we take the last field.
    FieldDescriptor lastField = Iterables.getLast(descriptor.getFields(), null);
    // Field numbers are 1 or greater so we will allocate the same size as the last field number
    // and subtract 1 when keeping track.
    int lastFieldNumber = lastField != null ? lastField.getNumber() : 0;
    int numFieldPresenceWords = lastFieldNumber / Integer.SIZE;
    if (lastFieldNumber % Integer.SIZE > 0) {
        numFieldPresenceWords++;
    }
    List<LocalVariable> fieldPresenceVars = new ArrayList<>();
    for (int i = 0; i < numFieldPresenceWords; i++) {
        fieldPresenceVars.add(new LocalVariable("setFieldBits" + i));
    }
    LocalVariables.Builder<LocalVariable> localsBuilder = LocalVariables.builderForMethod(instrumentedMethod, ObjectArrays.concat(LocalVariable.values(), Iterables.toArray(fieldPresenceVars, LocalVariable.class), LocalVariable.class)).add(builderClass, LocalVariable.builder).add(String.class, LocalVariable.fieldName);
    for (LocalVariable fieldPresenceVar : fieldPresenceVars) {
        localsBuilder.add(int.class, fieldPresenceVar);
    }
    LocalVariables<LocalVariable> locals = localsBuilder.build();
    List<StackManipulation> stackManipulations = new ArrayList<>();
    Label beforeReadField = new Label();
    Label finished = new Label();
    // Initialize local variables for this method, e.g.,
    // void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder) {
    // T builder = (T) messageBuilder;
    // String fieldName = null;
    // Object fieldNumberUnboxed = null;
    // int fieldNumber = 0;
    // int setFieldBits0 = 0;
    // int setFieldBits1 = 0;
    // ...
    stackManipulations.addAll(Arrays.asList(locals.initialize(), locals.load(LocalVariable.messageBuilder), TypeCasting.to(new ForLoadedType(builderClass)), locals.store(LocalVariable.builder)));
    // Begins the loop that reads fields from the JSON, reading the field name and computing its
    // hash, e.g.,
    // ...
    // while (parser.nextValue() != ParseSupport.checkObjectEnd) {
    // fieldName = parser.getCurrentName();
    // fieldNumberUnboxed = FIELD_NUMBERS.get(fieldName):
    // if (fieldNumberUnboxed == null) {
    // ParseSupport.throwIfUnknownField(fieldName, descriptor.getFullName());
    // return
    // }
    // fieldNumber = (int) fieldNumberUnboxed;
    // ...
    stackManipulations.addAll(Arrays.asList(new SetJumpTargetLabel(beforeReadField), locals.load(LocalVariable.parser), Parser_nextValue, ParseSupport_checkObjectEnd, new IfTrue(finished), locals.load(LocalVariable.parser), Parser_getCurrentName, locals.store(LocalVariable.fieldName)));
    for (FieldDescriptor f : descriptor.getFields()) {
        Label afterNameCheck = new Label();
        Label afterField = new Label();
        ProtoFieldInfo field = new ProtoFieldInfo(f, prototype);
        int fieldNumberZeroBased = field.descriptor().getNumber() - 1;
        LocalVariable fieldPresenceVar = fieldPresenceVars.get(fieldNumberZeroBased / Integer.SIZE);
        int fieldPreserveVarBitIndex = fieldNumberZeroBased % Integer.SIZE;
        // if-statement for checking whether the current JSON field name matches this field, e.g.,
        // if (fieldName == String.intern(field.getName())
        // || fieldName == String.intern(field.getJsonName()) {
        // ...
        // } else if ...
        stackManipulations.addAll(Arrays.asList(locals.load(LocalVariable.fieldName), new TextConstant(field.descriptor().getJsonName()), new IfRefsEqual(afterNameCheck), locals.load(LocalVariable.fieldName), new TextConstant(field.descriptor().getName()), new IfRefsNotEqual(afterField), new SetJumpTargetLabel(afterNameCheck)));
        // Check whether we have already seen this field in the JSON, which is not allowed. e.g.,
        // setFieldBitsN = ParseSupport.throwIfFieldAlreadyWritten(
        // setFieldBitsN, 0x1 << field.getNumber() % 8);
        stackManipulations.addAll(Arrays.asList(locals.load(fieldPresenceVar), IntegerConstant.forValue(0x1 << fieldPreserveVarBitIndex), new TextConstant(field.descriptor().getFullName()), ParseSupport_throwIfFieldAlreadyWritten, locals.store(fieldPresenceVar)));
        // ParseSupport.throwIfOneofAlreadyWritten(builder.getSomeOneofCase(), field.getFullName());
        if (field.isInOneof()) {
            try {
                stackManipulations.addAll(Arrays.asList(locals.load(LocalVariable.builder), invoke(builderClass.getDeclaredMethod(field.getOneOfCaseMethodName())), new TextConstant(field.descriptor().getFullName()), ParseSupport_throwIfOneofAlreadyWritten));
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException("Could not find oneof case method.", e);
            }
        }
        // }
        if (mustSkipNull(field.descriptor())) {
            stackManipulations.addAll(Arrays.asList(locals.load(LocalVariable.parser), ParseSupport_checkNull, new IfTrue(beforeReadField)));
        }
        StackManipulation setValue = setFieldValue(field, beforeReadField, locals, fieldsByName);
        stackManipulations.add(setValue);
        stackManipulations.add(new SetJumpTargetLabel(afterField));
    }
    if (ignoringUnknownFields) {
        // If we found no corresponding field number, jump back to the beginning of the while loop.
        stackManipulations.add(new Goto(beforeReadField));
    } else {
        // If we found no corresponding field number, throw an exception.
        stackManipulations.addAll(Arrays.asList(locals.load(LocalVariable.fieldName), new TextConstant(descriptor.getFullName()), ParseSupport_throwIfUnknownField));
    }
    // End of the field processing while loop.
    stackManipulations.add(new SetJumpTargetLabel(finished));
    stackManipulations.add(MethodReturn.VOID);
    StackManipulation.Size operandStackSize = new StackManipulation.Compound(stackManipulations).apply(methodVisitor, implementationContext);
    return new Size(operandStackSize.getMaximalSize(), locals.stackSize());
}
Also used : 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) IfRefsEqual(org.curioswitch.common.protobuf.json.bytebuddy.IfRefsEqual) Goto(org.curioswitch.common.protobuf.json.bytebuddy.Goto) IfRefsNotEqual(org.curioswitch.common.protobuf.json.bytebuddy.IfRefsNotEqual) TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) FieldDescription(net.bytebuddy.description.field.FieldDescription) SetJumpTargetLabel(org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel) IfTrue(org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 ArrayList (java.util.ArrayList)1 FieldDescription (net.bytebuddy.description.field.FieldDescription)1 ForLoadedType (net.bytebuddy.description.type.TypeDescription.ForLoadedType)1 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)1 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)1 Label (net.bytebuddy.jar.asm.Label)1 Goto (org.curioswitch.common.protobuf.json.bytebuddy.Goto)1 IfRefsEqual (org.curioswitch.common.protobuf.json.bytebuddy.IfRefsEqual)1 IfRefsNotEqual (org.curioswitch.common.protobuf.json.bytebuddy.IfRefsNotEqual)1 IfTrue (org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)1 SetJumpTargetLabel (org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel)1