Example usage for org.objectweb.asm Label Label

List of usage examples for org.objectweb.asm Label Label

Introduction

In this page you can find the example usage for org.objectweb.asm Label Label.

Prototype

public Label() 

Source Link

Document

Constructs a new label.

Usage

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void overrideGetMany() {
    final KeyModel pk = model.getPrimaryKey();
    final StringBuilder query = new StringBuilder();
    query.append(model.getSelectSql(dialect, REL_ALIAS));
    query.append(" WHERE ");
    final ColumnModel pkcol = pk.getAllLeafColumns().iterator().next();
    query.append(REL_ALIAS);/*from  w  w w. j a va 2s.com*/
    query.append('.');
    query.append(pkcol.getColumnName());
    query.append(" IN");

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getBySqlIn",
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class),
                    new Type[] { Type.getType(Collection.class) }),
            null, new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    final int keyset = 1;
    final int psvar = 2;
    final int itrvar = 3;
    final int colvar = 4;
    final int keyvar = 5;

    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(query.toString());
    mv.visitVarInsn(ALOAD, keyset);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareBySqlIn",
            Type.getMethodDescriptor(Type.getType(PreparedStatement.class),
                    new Type[] { Type.getType(String.class), Type.getType(Collection.class) }));
    mv.visitVarInsn(ASTORE, psvar);

    mv.visitVarInsn(ALOAD, keyset);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Collection.class), "iterator",
            Type.getMethodDescriptor(Type.getType(Iterator.class), new Type[] {}));
    mv.visitVarInsn(ASTORE, itrvar);

    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ISTORE, colvar);

    final Label endbind = new Label();
    final Label again = new Label();
    mv.visitLabel(again);
    mv.visitVarInsn(ALOAD, itrvar);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Iterator.class), "hasNext",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
    mv.visitJumpInsn(IFEQ, endbind);

    mv.visitVarInsn(ALOAD, itrvar);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Iterator.class), "next",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, CodeGenSupport.toType(pk.getField()).getInternalName());
    mv.visitVarInsn(ASTORE, keyvar);

    final CodeGenSupport cgs = new CodeGenSupport(mv) {
        @Override
        public void pushSqlHandle() {
            mv.visitVarInsn(ALOAD, psvar);
        }

        @Override
        public void pushFieldValue() {
            appendGetField(getFieldReference());
        }

        @Override
        public void pushColumnIndex() {
            mv.visitVarInsn(ILOAD, colvar);
        }

        @Override
        protected void appendGetField(final ColumnModel c) {
            if (c.getParent() == null) {
                mv.visitVarInsn(ALOAD, keyvar);
            } else {
                super.appendGetField(c);
            }
        }
    };

    cgs.setFieldReference(pkcol);
    dialect.getSqlTypeInfo(pkcol).generatePreparedStatementSet(cgs);
    mv.visitIincInsn(colvar, 1);
    mv.visitJumpInsn(GOTO, again);

    mv.visitLabel(endbind);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, psvar);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "queryList",
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class),
                    new Type[] { Type.getType(PreparedStatement.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void sizeofMessage(final JavaColumnModel[] myFields, final MethodVisitor mv, final SizeofCGS cgs)
        throws OrmException {
    for (final JavaColumnModel f : myFields) {
        if (f.isNested()) {
            final Label end = new Label();
            cgs.setFieldReference(f);//from   ww w . j  ava 2  s .c  om
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNULL, end);

            final int oldVar = cgs.sizeVar;
            final int msgVar = cgs.newLocal();
            cgs.sizeVar = msgVar;
            cgs.push(0);
            mv.visitVarInsn(ISTORE, cgs.sizeVar);

            sizeofMessage(sort(f.getNestedColumns()), mv, cgs);
            cgs.sizeVar = oldVar;

            cgs.push(f.getColumnID());
            cgs.inc("computeTagSize", Type.INT_TYPE);

            mv.visitVarInsn(ILOAD, msgVar);
            cgs.inc("computeRawVarint32Size", Type.INT_TYPE);

            mv.visitVarInsn(ILOAD, msgVar);
            cgs.inc();

            cgs.freeLocal(msgVar);
            mv.visitLabel(end);
        } else {
            sizeofScalar(mv, cgs, f);
        }
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void sizeofScalar(final MethodVisitor mv, final SizeofCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);/*w  w  w  . jav a2 s. c  o  m*/

    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeBoolSize", Type.INT_TYPE, Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeUInt32Size", Type.INT_TYPE, Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeSInt32Size", Type.INT_TYPE, Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeFloatSize", Type.INT_TYPE, Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeDoubleSize", Type.INT_TYPE, Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeSInt64", Type.INT_TYPE, Type.LONG_TYPE);
        break;

    case Type.ARRAY:
    case Type.OBJECT: {
        final Label end = new Label();
        cgs.pushFieldValue();
        mv.visitJumpInsn(IFNULL, end);

        if (f.getPrimitiveType() == byte[].class) {
            cgs.push(f.getColumnID());
            cgs.inc("computeTagSize", Type.INT_TYPE);

            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            cgs.inc("computeRawVarint32Size", Type.INT_TYPE);

            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            cgs.inc();

        } else if (f.getPrimitiveType() == String.class) {
            cgs.push(f.getColumnID());
            cgs.pushFieldValue();
            cgs.inc("computeStringSize", Type.INT_TYPE, string);

        } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                || f.getPrimitiveType() == java.util.Date.class
                || f.getPrimitiveType() == java.sql.Date.class) {
            cgs.push(f.getColumnID());
            String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
            mv.visitMethodInsn(INVOKEVIRTUAL, tsType, "getTime",
                    Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {}));
            cgs.inc("computeFixed64Size", Type.INT_TYPE, Type.LONG_TYPE);

        } else {
            throw new OrmException(
                    "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
        }
        mv.visitLabel(end);
        break;
    }

    default:
        throw new OrmException(
                "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void encodeMessage(final JavaColumnModel[] myFields, final MethodVisitor mv, final EncodeCGS cgs)
        throws OrmException {
    final int oldVar = cgs.codedOutputStreamVar;
    cgs.codedOutputStreamVar = cgs.newLocal();

    final int strVar = cgs.newLocal();
    mv.visitMethodInsn(INVOKESTATIC, byteString.getInternalName(), "newOutput",
            Type.getMethodDescriptor(byteStringOutput, new Type[] {}));
    mv.visitVarInsn(ASTORE, strVar);/*www  .ja v  a2  s .  c o  m*/

    mv.visitVarInsn(ALOAD, strVar);
    mv.visitMethodInsn(INVOKESTATIC, codedOutputStream.getInternalName(), "newInstance",
            Type.getMethodDescriptor(codedOutputStream, new Type[] { Type.getType(OutputStream.class) }));
    mv.visitVarInsn(ASTORE, cgs.codedOutputStreamVar);

    for (final JavaColumnModel f : myFields) {
        if (f.isNested()) {
            final Label end = new Label();
            cgs.setFieldReference(f);
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNULL, end);

            final int v = cgs.newLocal();
            encodeMessage(sort(f.getNestedColumns()), mv, cgs);
            mv.visitVarInsn(ASTORE, v);

            mv.visitVarInsn(ALOAD, v);
            mv.visitMethodInsn(INVOKEVIRTUAL, byteString.getInternalName(), "size",
                    Type.getMethodDescriptor(Type.INT_TYPE, new Type[] {}));
            mv.visitJumpInsn(IFEQ, end);

            cgs.pushCodedOutputStream();
            cgs.push(f.getColumnID());
            mv.visitVarInsn(ALOAD, v);
            cgs.write("writeBytes", byteString);

            cgs.freeLocal(v);
            mv.visitLabel(end);
        } else {
            encodeScalar(mv, cgs, f);
        }
    }

    cgs.pushCodedOutputStream();
    mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "flush",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));

    cgs.freeLocal(cgs.codedOutputStreamVar);
    cgs.codedOutputStreamVar = oldVar;

    mv.visitVarInsn(ALOAD, strVar);
    mv.visitMethodInsn(INVOKEVIRTUAL, byteStringOutput.getInternalName(), "toByteString",
            Type.getMethodDescriptor(byteString, new Type[] {}));
    cgs.freeLocal(strVar);
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void encodeScalar(final MethodVisitor mv, final EncodeCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);//from   w  ww.j a va  2  s .c o  m

    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeBool", Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeUInt32", Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeSInt32", Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeFloat", Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeDouble", Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeSInt64", Type.LONG_TYPE);
        break;

    case Type.ARRAY:
    case Type.OBJECT: {
        final Label end = new Label();
        cgs.pushFieldValue();
        mv.visitJumpInsn(IFNULL, end);

        if (f.getPrimitiveType() == byte[].class) {
            cgs.pushCodedOutputStream();
            cgs.push(f.getColumnID());
            cgs.push(WireFormat.FieldType.BYTES.getWireType());
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeTag",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }));

            cgs.pushCodedOutputStream();
            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeRawVarint32",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));

            cgs.pushCodedOutputStream();
            cgs.pushFieldValue();
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeRawBytes",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(byte[].class) }));

        } else {
            cgs.pushCodedOutputStream();
            cgs.push(f.getColumnID());
            cgs.pushFieldValue();

            if (f.getPrimitiveType() == String.class) {
                cgs.write("writeString", string);

            } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                    || f.getPrimitiveType() == java.util.Date.class
                    || f.getPrimitiveType() == java.sql.Date.class) {
                String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
                mv.visitMethodInsn(INVOKEVIRTUAL, tsType, "getTime",
                        Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {}));
                cgs.write("writeFixed64", Type.LONG_TYPE);

            } else {
                throw new OrmException(
                        "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
            }
        }
        mv.visitLabel(end);
        break;
    }

    default:
        throw new OrmException(
                "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void decodeMessage(final JavaColumnModel[] myFields, final MethodVisitor mv, final DecodeCGS cgs)
        throws OrmException {
    final Label nextField = new Label();
    final Label end = new Label();
    mv.visitLabel(nextField);/*from w w w .j av  a 2 s  .  c  o  m*/

    // while (!ci.isAtEnd) { ...
    cgs.call("readTag", Type.INT_TYPE);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, cgs.tagVar);

    cgs.push(3);
    mv.visitInsn(IUSHR);

    final Label badField = new Label();
    final int[] caseTags = new int[1 + myFields.length];
    final Label[] caseLabels = new Label[caseTags.length];

    caseTags[0] = 0;
    caseLabels[0] = new Label();

    int gaps = 0;
    for (int i = 1; i < caseTags.length; i++) {
        caseTags[i] = myFields[i - 1].getColumnID();
        caseLabels[i] = new Label();
        gaps += caseTags[i] - (caseTags[i - 1] + 1);
    }

    if (2 * gaps / 3 <= myFields.length) {
        final int min = 0;
        final int max = caseTags[caseTags.length - 1];
        final Label[] table = new Label[max + 1];
        Arrays.fill(table, badField);
        for (int idx = 0; idx < caseTags.length; idx++) {
            table[caseTags[idx]] = caseLabels[idx];
        }
        mv.visitTableSwitchInsn(min, max, badField, table);
    } else {
        mv.visitLookupSwitchInsn(badField, caseTags, caseLabels);
    }

    mv.visitLabel(caseLabels[0]);
    mv.visitJumpInsn(GOTO, end);

    for (int idx = 1; idx < caseTags.length; idx++) {
        final JavaColumnModel f = myFields[idx - 1];
        mv.visitLabel(caseLabels[idx]);
        if (f.isNested()) {
            final Label load = new Label();
            cgs.setFieldReference(f);
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNONNULL, load);
            cgs.fieldSetBegin();
            mv.visitTypeInsn(NEW, Type.getType(f.getNestedClass()).getInternalName());
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, Type.getType(f.getNestedClass()).getInternalName(), "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
            cgs.fieldSetEnd();

            // read the length, set a new limit, decode the message, validate
            // we stopped at the end of it as expected.
            //
            mv.visitLabel(load);
            final int limitVar = cgs.newLocal();
            cgs.pushCodedInputStream();
            cgs.call("readRawVarint32", Type.INT_TYPE);
            cgs.ncallInt("pushLimit", Type.INT_TYPE);
            mv.visitVarInsn(ISTORE, limitVar);

            decodeMessage(sort(f.getNestedColumns()), mv, cgs);

            cgs.pushCodedInputStream();
            mv.visitVarInsn(ILOAD, limitVar);
            cgs.ncallInt("popLimit", Type.VOID_TYPE);
            cgs.freeLocal(limitVar);

        } else {
            decodeScalar(mv, cgs, f);
        }
        mv.visitJumpInsn(GOTO, nextField);
    }

    // default:
    mv.visitLabel(badField);
    cgs.pushCodedInputStream();
    mv.visitVarInsn(ILOAD, cgs.tagVar);
    cgs.ncallInt("skipField", Type.BOOLEAN_TYPE);
    mv.visitInsn(POP);
    mv.visitJumpInsn(GOTO, nextField);

    mv.visitLabel(end);
    cgs.pushCodedInputStream();
    cgs.push(0);
    cgs.ncallInt("checkLastTagWas", Type.VOID_TYPE);
}

From source file:com.google.gwtorm.schema.sql.SqlBooleanTypeInfo.java

License:Apache License

@Override
public void generatePreparedStatementSet(final CodeGenSupport cgs) {
    cgs.pushSqlHandle();//  w  w  w  . j  a va2  s . c  o  m
    cgs.pushColumnIndex();
    cgs.pushFieldValue();

    final Label useNo = new Label();
    final Label end = new Label();
    cgs.mv.visitJumpInsn(Opcodes.IFEQ, useNo);
    cgs.mv.visitLdcInsn(getTrueValue());
    cgs.mv.visitJumpInsn(Opcodes.GOTO, end);
    cgs.mv.visitLabel(useNo);
    cgs.mv.visitLdcInsn(getFalseValue());
    cgs.mv.visitLabel(end);
    cgs.invokePreparedStatementSet(getJavaSqlTypeAlias());
}

From source file:com.google.java.contract.core.agent.LineNumberingMethodAdapter.java

License:Open Source License

@Override
protected void onMethodEnter() {
    if (lineNumbers != null && !lineNumbers.isEmpty()) {
        Long lineNumber = lineNumbers.get(0);
        if (lineNumber != null) {
            Label methodStart = new Label();
            mark(methodStart);/*from   ww w.j  av  a  2  s  .c  om*/
            mv.visitLineNumber(lineNumber.intValue(), methodStart);
        }
    }
}

From source file:com.google.java.contract.core.agent.SpecificationMethodAdapter.java

License:Open Source License

/**
 * Constructs a new SpecificationClassAdapter.
 *
 * @param ca the class adapter which has spawned this method adapter
 * @param mv the method visitor to delegate to
 * @param access the method access bit mask
 * @param methodName the name of the method
 * @param methodDesc the descriptor of the method
 *//*from   w  w w  . ja v  a 2 s.  c  o  m*/
@Requires({ "ca != null", "mv != null", "methodName != null", "methodDesc != null" })
public SpecificationMethodAdapter(SpecificationClassAdapter ca, MethodVisitor mv, int access, String methodName,
        String methodDesc) {
    super(Opcodes.ASM5, mv, access, methodName, methodDesc);

    methodStart = new Label();
    methodEnd = new Label();

    this.contracts = ca.getContracts();
    className = ca.getClassName();
    this.methodName = methodName;
    this.methodDesc = methodDesc;
    thisType = Type.getType("L" + className + ";");

    statik = (access & ACC_STATIC) != 0;
    isConstructor = methodName.equals("<init>");
    isStaticInit = methodName.endsWith("<clinit>");

    contextLocal = -1;
    checkInvariantsLocal = -1;
    oldValueLocals = new ArrayList<Integer>();
    signalOldValueLocals = new ArrayList<Integer>();

    classAdapter = ca;

    ActivationRuleManager am = ActivationRuleManager.getInstance();
    withPreconditions = am.hasPreconditionsEnabled(className);
    withPostconditions = am.hasPostconditionsEnabled(className);
    withInvariants = am.hasInvariantsEnabled(className);
}

From source file:com.google.java.contract.core.agent.SpecificationMethodAdapter.java

License:Open Source License

/**
 * Advises the method by injecting exceptional postconditions and
 * invariants after the original code. This code only gets executed
 * if an exception has been thrown (otherwise, a {@code return}
 * instruction would have ended execution of the method already).
 *///  ww w.ja  v  a2s.  co  m
@Override
public void visitMaxs(int maxStack, int maxLocals) {
    if (withPreconditions || withPostconditions || withInvariants) {
        mark(methodEnd);
        catchException(methodStart, methodEnd, null);

        if (withPostconditions) {
            Label skipEx = new Label();
            dup();
            instanceOf(EXCEPTION_TYPE);
            ifZCmp(EQ, skipEx);

            Label skip = enterBusySection();
            int throwIndex = newLocal(EXCEPTION_TYPE);
            checkCast(EXCEPTION_TYPE);
            storeLocal(throwIndex);

            invokeCommonPostconditions(ContractKind.SIGNAL, signalOldValueLocals, throwIndex);
            if (withInvariants && !statik) {
                invokeInvariants();
            }

            loadLocal(throwIndex);
            leaveBusySection(skip);

            mark(skipEx);
        }

        /*
         * The exception to throw is on the stack and
         * leaveContractedMethod() does not alter that fact.
         */
        leaveContractedMethod();
        throwException();
    }
    super.visitMaxs(maxStack, maxLocals);
}