Search in sources :

Example 6 with Label

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label in project hibernate-orm by hibernate.

the class InlineDirtyCheckingHandler method apply.

@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    // if (arg != field) {
    methodVisitor.visitVarInsn(Type.getType(persistentField.getType().asErasure().getDescriptor()).getOpcode(Opcodes.ILOAD), 1);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    if (persistentField.getDeclaringType().asErasure().equals(managedCtClass)) {
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, persistentField.getDeclaringType().asErasure().getInternalName(), persistentField.getName(), persistentField.getDescriptor());
    } else {
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, persistentField.getDeclaringType().asErasure().getInternalName(), EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentField.getName(), Type.getMethodDescriptor(Type.getType(persistentField.getDescriptor())), false);
    }
    int branchCode;
    if (persistentField.getType().isPrimitive()) {
        if (persistentField.getType().represents(long.class)) {
            methodVisitor.visitInsn(Opcodes.LCMP);
        } else if (persistentField.getType().represents(float.class)) {
            methodVisitor.visitInsn(Opcodes.FCMPL);
        } else if (persistentField.getType().represents(double.class)) {
            methodVisitor.visitInsn(Opcodes.DCMPL);
        } else {
            methodVisitor.visitInsn(Opcodes.ISUB);
        }
        branchCode = Opcodes.IFEQ;
    } else {
        methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(EqualsHelper.class), "areEqual", Type.getMethodDescriptor(Type.getType(boolean.class), Type.getType(Object.class), Type.getType(Object.class)), false);
        branchCode = Opcodes.IFNE;
    }
    Label skip = new Label();
    methodVisitor.visitJumpInsn(branchCode, skip);
    // this.$$_hibernate_trackChange(fieldName)
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitLdcInsn(persistentField.getName());
    methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, managedCtClass.getInternalName(), EnhancerConstants.TRACKER_CHANGER_NAME, Type.getMethodDescriptor(Type.getType(void.class), Type.getType(String.class)), false);
    // }
    methodVisitor.visitLabel(skip);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    }
    return new Size(1 + 2 * persistentField.getType().asErasure().getStackSize().getSize(), instrumentedMethod.getStackSize());
}
Also used : EqualsHelper(org.hibernate.internal.util.compare.EqualsHelper) Label(net.bytebuddy.jar.asm.Label)

Example 7 with Label

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label in project powermock by powermock.

the class ConditionalStateStackManipulation method apply.

@Override
public Size apply(final MethodVisitor mv, final Context implementationContext) {
    Size size = new Size(0, 0);
    Label proceed = new Label();
    Label exit = new Label();
    size = size.aggregate(condition.apply(mv, implementationContext));
    mv.visitJumpInsn(Opcodes.IFEQ, proceed);
    size = size.aggregate(action.apply(mv, implementationContext));
    mv.visitJumpInsn(Opcodes.GOTO, exit);
    mv.visitLabel(proceed);
    mv.visitFrame(Opcodes.F_FULL, frame.localSize(), frame.locals(), 0, null);
    size = size.aggregate(otherwise.apply(mv, implementationContext));
    mv.visitLabel(exit);
    mv.visitFrame(Opcodes.F_FULL, 0, null, 0, null);
    return size;
}
Also used : Label(net.bytebuddy.jar.asm.Label)

Example 8 with Label

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label 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 : CodeGenUtil.sorted(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, IntegerConstant.forValue(sortingMapKeys), IntegerConstant.forValue(field.mapKeyField().valueType() == Type.STRING), SerializeSupport_mapIterator, 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

Label (net.bytebuddy.jar.asm.Label)8 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)4 Goto (org.curioswitch.common.protobuf.json.bytebuddy.Goto)4 SetJumpTargetLabel (org.curioswitch.common.protobuf.json.bytebuddy.SetJumpTargetLabel)4 ForLoadedType (net.bytebuddy.description.type.TypeDescription.ForLoadedType)3 IfTrue (org.curioswitch.common.protobuf.json.bytebuddy.IfTrue)3 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)2 ArrayList (java.util.ArrayList)2 FieldDescription (net.bytebuddy.description.field.FieldDescription)2 TypeDescription (net.bytebuddy.description.type.TypeDescription)2 IfEqual (org.curioswitch.common.protobuf.json.bytebuddy.IfEqual)2 PersistentAttributeInterceptor (org.hibernate.engine.spi.PersistentAttributeInterceptor)2 SerializableString (com.fasterxml.jackson.core.SerializableString)1 ByteString (com.google.protobuf.ByteString)1 Entry (java.util.Map.Entry)1 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)1 IfFalse (org.curioswitch.common.protobuf.json.bytebuddy.IfFalse)1 IfRefsEqual (org.curioswitch.common.protobuf.json.bytebuddy.IfRefsEqual)1 IfRefsNotEqual (org.curioswitch.common.protobuf.json.bytebuddy.IfRefsNotEqual)1 EqualsHelper (org.hibernate.internal.util.compare.EqualsHelper)1