Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

List of usage examples for org.objectweb.asm MethodVisitor visitLdcInsn

Introduction

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

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

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

License:Apache License

private void implementKeyQuery(final KeyModel info) {
    final Type keyType = CodeGenSupport.toType(info.getField());
    final StringBuilder query = new StringBuilder();
    query.append(model.getSelectSql(dialect, REL_ALIAS));
    query.append(" WHERE ");
    int nth = 1;/*from   w ww. ja  va 2 s  .  com*/
    for (final Iterator<ColumnModel> i = info.getAllLeafColumns().iterator(); i.hasNext();) {
        final ColumnModel c = i.next();
        query.append(REL_ALIAS);
        query.append('.');
        query.append(c.getColumnName());
        query.append('=');
        query.append(dialect.getParameterPlaceHolder(nth++));
        if (i.hasNext()) {
            query.append(" AND ");
        }
    }

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, info.getName(),
            Type.getMethodDescriptor(entityType, new Type[] { keyType }), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    final int keyvar = 1, psvar = keyvar + keyType.getSize();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(query.toString());
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareStatement", Type.getMethodDescriptor(
            Type.getType(PreparedStatement.class), new Type[] { Type.getType(String.class) }));
    mv.visitVarInsn(ASTORE, psvar);

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

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

        @Override
        protected void appendGetField(final ColumnModel c) {
            if (c.getParent() == null) {
                loadVar(keyType, keyvar);
            } else {
                super.appendGetField(c);
            }
        }
    };
    for (final ColumnModel c : info.getAllLeafColumns()) {
        cgs.setFieldReference(c);
        dialect.getSqlTypeInfo(c).generatePreparedStatementSet(cgs);
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, psvar);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "queryOne", Type.getMethodDescriptor(
            Type.getType(Object.class), new Type[] { Type.getType(PreparedStatement.class) }));
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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 v  a2 s  . c  o  m
    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.jdbc.AccessGen.java

License:Apache License

private void implementQuery(final QueryModel info) {
    final List<ColumnModel> pCols = info.getParameters();
    final boolean hasLimitParam = info.hasLimitParameter();
    final Type[] pTypes = new Type[pCols.size() + (hasLimitParam ? 1 : 0)];
    final int[] pVars = new int[pTypes.length];
    int nextVar = 1;
    for (int i = 0; i < pCols.size(); i++) {
        pTypes[i] = CodeGenSupport.toType(pCols.get(i));
        pVars[i] = nextVar;/*from   w w w . java 2s  .c  o  m*/
        nextVar += pTypes[i].getSize();
    }
    if (hasLimitParam) {
        pTypes[pTypes.length - 1] = Type.INT_TYPE;
        pVars[pTypes.length - 1] = nextVar;
        nextVar += Type.INT_TYPE.getSize();
    }

    final int psvar = nextVar++;
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, info.getName(),
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class), pTypes), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(info.getSelectSql(dialect, REL_ALIAS));
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareStatement", Type.getMethodDescriptor(
            Type.getType(PreparedStatement.class), new Type[] { Type.getType(String.class) }));
    mv.visitVarInsn(ASTORE, psvar);

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

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

        @Override
        protected void appendGetField(final ColumnModel c) {
            final int n = argIdx[0];
            if (c == pCols.get(n)) {
                loadVar(pTypes[n], pVars[n]);
            } else {
                super.appendGetField(c);
            }
        }
    };
    for (final ColumnModel c : pCols) {
        if (c.isNested()) {
            for (final ColumnModel n : c.getAllLeafColumns()) {
                cgs.setFieldReference(n);
                dialect.getSqlTypeInfo(n).generatePreparedStatementSet(cgs);
            }
        } else {
            cgs.setFieldReference(c);
            dialect.getSqlTypeInfo(c).generatePreparedStatementSet(cgs);
        }
        argIdx[0]++;
    }

    if (info.hasLimit()) {
        if (hasLimitParam || !dialect.selectHasLimit()) {
            mv.visitVarInsn(ALOAD, psvar);
            if (hasLimitParam) {
                mv.visitVarInsn(ILOAD, pVars[pTypes.length - 1]);
            } else {
                cgs.push(info.getStaticLimit());
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getType(PreparedStatement.class).getInternalName(),
                    "setMaxRows", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));
        }
    }

    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.nosql.AccessGen.java

License:Apache License

private void implementGetString(final String methodName, final String returnValue) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName,
            Type.getMethodDescriptor(string, new Type[] {}), null, null);
    mv.visitCode();/*ww  w . j  ava  2 s  . co  m*/
    mv.visitLdcInsn(returnValue);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.nosql.IndexFunctionGen.java

License:Apache License

private void implementGetName() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getName",
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}), null, null);
    mv.visitCode();/*from  ww w .j av a  2s  .c om*/
    mv.visitLdcInsn(query.getName());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.nosql.IndexFunctionGen.java

License:Apache License

private void checkConstants(Tree node, MethodVisitor mv, IncludeCGS cgs) throws OrmException {
    switch (node.getType()) {
    // These don't impact the constant evaluation
    case QueryParser.ORDER:
    case QueryParser.LIMIT:
        break;// www . j a v  a 2  s .  co m

    case 0: // nil node used to join other nodes together
    case QueryParser.WHERE:
    case QueryParser.AND:
        for (int i = 0; i < node.getChildCount(); i++) {
            checkConstants(node.getChild(i), mv, cgs);
        }
        break;

    case QueryParser.LT:
    case QueryParser.LE:
    case QueryParser.GT:
    case QueryParser.GE:
    case QueryParser.EQ: {
        final Tree lhs = node.getChild(0);
        final Tree rhs = node.getChild(1);
        if (lhs.getType() != QueryParser.ID) {
            throw new OrmException("Unsupported query token");
        }

        cgs.setFieldReference(((QueryParser.Column) lhs).getField());
        switch (rhs.getType()) {
        case QueryParser.PLACEHOLDER:
            // Parameter evaluated at runtime
            break;

        case QueryParser.TRUE:
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFEQ, cgs.no);
            break;

        case QueryParser.FALSE:
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNE, cgs.no);
            break;

        case QueryParser.CONSTANT_INTEGER:
            cgs.pushFieldValue();
            cgs.push(Integer.parseInt(rhs.getText()));
            mv.visitJumpInsn(IF_ICMPNE, cgs.no);
            break;

        case QueryParser.CONSTANT_STRING:
            if (cgs.getFieldReference().getPrimitiveType() == Character.TYPE) {
                cgs.push(dequote(rhs.getText()).charAt(0));
                cgs.pushFieldValue();
                mv.visitJumpInsn(IF_ICMPNE, cgs.no);
            } else {
                mv.visitLdcInsn(dequote(rhs.getText()));
                cgs.pushFieldValue();
                mv.visitMethodInsn(INVOKEVIRTUAL, string.getInternalName(), "equals",
                        Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { object }));
                mv.visitJumpInsn(IFEQ, cgs.no);
            }
            break;
        }
        break;
    }

    default:
        throw new OrmException("Unsupported query token " + node.toStringTree());
    }
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementSequenceMethods() {
    for (final SequenceModel seq : schema.getSequences()) {
        final Type retType = Type.getType(seq.getResultType());
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, seq.getMethodName(),
                Type.getMethodDescriptor(retType, new Type[] {}), null,
                new String[] { Type.getType(OrmException.class).getInternalName() });
        mv.visitCode();/* w  w w. j  a  va  2s  .c  o m*/

        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(seq.getSequenceName());
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(schemaSuperClass), "nextLong",
                Type.getMethodDescriptor(Type.getType(Long.TYPE), new Type[] { Type.getType(String.class) }));
        if (retType.getSize() == 1) {
            mv.visitInsn(L2I);
            mv.visitInsn(IRETURN);
        } else {
            mv.visitInsn(LRETURN);
        }
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private void reBuildInstructions(IrMethod ir, MethodVisitor asm) {
    asm = new LdcOptimizeAdapter(asm);
    int maxLocalIndex = 0;
    for (Local local : ir.locals) {
        maxLocalIndex = Math.max(maxLocalIndex, local._ls_index);
    }// ww  w .j  a  v  a2s. c  om
    Map<String, Integer> lockMap = new HashMap<String, Integer>();
    for (Stmt st : ir.stmts) {
        switch (st.st) {
        case LABEL:
            LabelStmt labelStmt = (LabelStmt) st;
            Label label = (Label) labelStmt.tag;
            asm.visitLabel(label);
            if (labelStmt.lineNumber >= 0) {
                asm.visitLineNumber(labelStmt.lineNumber, label);
            }
            break;
        case ASSIGN: {
            E2Stmt e2 = (E2Stmt) st;
            Value v1 = e2.op1;
            Value v2 = e2.op2;
            switch (v1.vt) {
            case LOCAL:

                Local local = ((Local) v1);
                int i = local._ls_index;

                if (v2.vt == VT.LOCAL && (i == ((Local) v2)._ls_index)) {//
                    continue;
                }

                boolean skipOrg = false;
                if (v1.valueType.charAt(0) == 'I') {// check for IINC
                    if (v2.vt == VT.ADD) {
                        E2Expr e = (E2Expr) v2;
                        if ((e.op1 == local && e.op2.vt == VT.CONSTANT)
                                || (e.op2 == local && e.op1.vt == VT.CONSTANT)) {
                            int increment = (Integer) ((Constant) (e.op1 == local ? e.op2 : e.op1)).value;
                            if (increment >= Short.MIN_VALUE && increment <= Short.MAX_VALUE) {
                                asm.visitIincInsn(i, increment);
                                skipOrg = true;
                            }
                        }
                    } else if (v2.vt == VT.SUB) {
                        E2Expr e = (E2Expr) v2;
                        if (e.op1 == local && e.op2.vt == VT.CONSTANT) {
                            int increment = -(Integer) ((Constant) e.op2).value;
                            if (increment >= Short.MIN_VALUE && increment <= Short.MAX_VALUE) {
                                asm.visitIincInsn(i, increment);
                                skipOrg = true;
                            }
                        }
                    }
                }
                if (!skipOrg) {
                    accept(v2, asm);
                    if (i >= 0) {
                        asm.visitVarInsn(getOpcode(v1, ISTORE), i);
                    } else if (!v1.valueType.equals("V")) { // skip void type locals
                        switch (v1.valueType.charAt(0)) {
                        case 'J':
                        case 'D':
                            asm.visitInsn(POP2);
                            break;
                        default:
                            asm.visitInsn(POP);
                            break;
                        }
                    }
                }
                break;
            case STATIC_FIELD: {
                StaticFieldExpr fe = (StaticFieldExpr) v1;
                accept(v2, asm);
                insertI2x(v2.valueType, fe.type, asm);
                asm.visitFieldInsn(PUTSTATIC, toInternal(fe.owner), fe.name, fe.type);
                break;
            }
            case FIELD: {
                FieldExpr fe = (FieldExpr) v1;
                accept(fe.op, asm);
                accept(v2, asm);
                insertI2x(v2.valueType, fe.type, asm);
                asm.visitFieldInsn(PUTFIELD, toInternal(fe.owner), fe.name, fe.type);
                break;
            }
            case ARRAY:
                ArrayExpr ae = (ArrayExpr) v1;
                accept(ae.op1, asm);
                accept(ae.op2, asm);
                accept(v2, asm);
                String tp1 = ae.op1.valueType;
                String tp2 = ae.valueType;
                if (tp1.charAt(0) == '[') {
                    String arrayElementType = tp1.substring(1);
                    insertI2x(v2.valueType, arrayElementType, asm);
                    asm.visitInsn(getOpcode(arrayElementType, IASTORE));
                } else {
                    asm.visitInsn(getOpcode(tp2, IASTORE));
                }
                break;
            }
        }
            break;
        case IDENTITY: {
            E2Stmt e2 = (E2Stmt) st;
            if (e2.op2.vt == VT.EXCEPTION_REF) {
                int index = ((Local) e2.op1)._ls_index;
                if (index >= 0) {
                    asm.visitVarInsn(ASTORE, index);
                } else {
                    asm.visitInsn(POP);
                }
            }
        }
            break;

        case FILL_ARRAY_DATA: {
            E2Stmt e2 = (E2Stmt) st;
            Object arrayData = ((Constant) e2.getOp2()).value;
            int arraySize = Array.getLength(arrayData);
            String arrayValueType = e2.getOp1().valueType;
            String elementType;
            if (arrayValueType.charAt(0) == '[') {
                elementType = arrayValueType.substring(1);
            } else {
                elementType = "I";
            }
            int iastoreOP = getOpcode(elementType, IASTORE);
            accept(e2.getOp1(), asm);
            for (int i = 0; i < arraySize; i++) {
                asm.visitInsn(DUP);
                asm.visitLdcInsn(i);
                asm.visitLdcInsn(Array.get(arrayData, i));
                asm.visitInsn(iastoreOP);
            }
            asm.visitInsn(POP);
        }
            break;
        case GOTO:
            asm.visitJumpInsn(GOTO, (Label) ((GotoStmt) st).target.tag);
            break;
        case IF:
            reBuildJumpInstructions((IfStmt) st, asm);
            break;
        case LOCK: {
            Value v = ((UnopStmt) st).op;
            accept(v, asm);
            if (optimizeSynchronized) {
                switch (v.vt) {
                case LOCAL:
                    // FIXME do we have to disable local due to OptSyncTest ?
                    // break;
                case CONSTANT: {
                    String key;
                    if (v.vt == VT.LOCAL) {
                        key = "L" + ((Local) v)._ls_index;
                    } else {
                        key = "C" + ((Constant) v).value;
                    }
                    Integer integer = lockMap.get(key);
                    int nIndex = integer != null ? integer : ++maxLocalIndex;
                    asm.visitInsn(DUP);
                    asm.visitVarInsn(getOpcode(v, ISTORE), nIndex);
                    lockMap.put(key, nIndex);
                }
                    break;
                default:
                    throw new RuntimeException();
                }
            }
            asm.visitInsn(MONITORENTER);
        }
            break;
        case UNLOCK: {
            Value v = ((UnopStmt) st).op;
            if (optimizeSynchronized) {
                switch (v.vt) {
                case LOCAL:
                case CONSTANT: {
                    String key;
                    if (v.vt == VT.LOCAL) {
                        key = "L" + ((Local) v)._ls_index;
                    } else {
                        key = "C" + ((Constant) v).value;
                    }
                    Integer integer = lockMap.get(key);
                    if (integer != null) {
                        asm.visitVarInsn(getOpcode(v, ILOAD), integer);
                    } else {
                        accept(v, asm);
                    }
                }
                    break;
                // TODO other
                default: {
                    accept(v, asm);
                    break;
                }
                }
            } else {
                accept(v, asm);
            }
            asm.visitInsn(MONITOREXIT);
        }
            break;
        case NOP:
            break;
        case RETURN: {
            Value v = ((UnopStmt) st).op;
            accept(v, asm);
            insertI2x(v.valueType, ir.ret, asm);
            asm.visitInsn(getOpcode(v, IRETURN));
        }
            break;
        case RETURN_VOID:
            asm.visitInsn(RETURN);
            break;
        case LOOKUP_SWITCH: {
            LookupSwitchStmt lss = (LookupSwitchStmt) st;
            accept(lss.op, asm);
            Label targets[] = new Label[lss.targets.length];
            for (int i = 0; i < targets.length; i++) {
                targets[i] = (Label) lss.targets[i].tag;
            }
            asm.visitLookupSwitchInsn((Label) lss.defaultTarget.tag, lss.lookupValues, targets);
        }
            break;
        case TABLE_SWITCH: {
            TableSwitchStmt tss = (TableSwitchStmt) st;
            accept(tss.op, asm);
            Label targets[] = new Label[tss.targets.length];
            for (int i = 0; i < targets.length; i++) {
                targets[i] = (Label) tss.targets[i].tag;
            }
            asm.visitTableSwitchInsn(tss.lowIndex, tss.lowIndex + targets.length - 1,
                    (Label) tss.defaultTarget.tag, targets);
        }
            break;
        case THROW:
            accept(((UnopStmt) st).op, asm);
            asm.visitInsn(ATHROW);
            break;
        case VOID_INVOKE:
            InvokeExpr invokeExpr = (InvokeExpr) st.getOp();
            accept(invokeExpr, asm);
            String ret = invokeExpr.ret;
            if (invokeExpr.vt == VT.INVOKE_NEW) {
                asm.visitInsn(POP);
            } else if (!"V".equals(ret)) {
                switch (ret.charAt(0)) {
                case 'J':
                case 'D':
                    asm.visitInsn(POP2);
                    break;
                default:
                    asm.visitInsn(POP);
                    break;
                }
            }
            break;
        default:
            throw new RuntimeException("not support st: " + st.st);
        }

    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private static void accept(Value value, MethodVisitor asm) {

    switch (value.et) {
    case E0:/*from  w  w w . j av  a2s  .  c  om*/
        switch (value.vt) {
        case LOCAL:
            asm.visitVarInsn(getOpcode(value, ILOAD), ((Local) value)._ls_index);
            break;
        case CONSTANT:
            Constant cst = (Constant) value;
            if (cst.value.equals(Constant.Null)) {
                asm.visitInsn(ACONST_NULL);
            } else if (cst.value instanceof Constant.Type) {
                asm.visitLdcInsn(Type.getType(((Constant.Type) cst.value).desc));
            } else {
                asm.visitLdcInsn(cst.value);
            }
            break;
        case NEW:
            asm.visitTypeInsn(NEW, toInternal(((NewExpr) value).type));
            break;
        case STATIC_FIELD:
            StaticFieldExpr sfe = (StaticFieldExpr) value;
            asm.visitFieldInsn(GETSTATIC, toInternal(sfe.owner), sfe.name, sfe.type);
            break;
        }
        break;
    case E1:
        reBuildE1Expression((E1Expr) value, asm);
        break;
    case E2:
        reBuildE2Expression((E2Expr) value, asm);
        break;
    case En:
        reBuildEnExpression((EnExpr) value, asm);
        break;
    }
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private static void reBuildEnExpression(EnExpr value, MethodVisitor asm) {
    if (value.vt == VT.FILLED_ARRAY) {
        FilledArrayExpr fae = (FilledArrayExpr) value;
        reBuildE1Expression(Exprs.nNewArray(fae.type, Exprs.nInt(fae.ops.length)), asm);
        String tp1 = fae.valueType;
        int xastore = IASTORE;
        String elementType = null;
        if (tp1.charAt(0) == '[') {
            elementType = tp1.substring(1);
            xastore = getOpcode(elementType, IASTORE);
        }/*w w w. j  a  v  a  2  s. co m*/

        for (int i = 0; i < fae.ops.length; i++) {
            if (fae.ops[i] == null)
                continue;
            asm.visitInsn(DUP);
            asm.visitLdcInsn(i);
            accept(fae.ops[i], asm);
            String tp2 = fae.ops[i].valueType;
            if (elementType != null) {
                insertI2x(tp2, elementType, asm);
            }
            asm.visitInsn(xastore);
        }
        return;
    }

    switch (value.vt) {
    case NEW_MUTI_ARRAY:
        for (Value vb : value.ops) {
            accept(vb, asm);
        }
        NewMutiArrayExpr nmae = (NewMutiArrayExpr) value;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nmae.dimension; i++) {
            sb.append('[');
        }
        sb.append(nmae.baseType);
        asm.visitMultiANewArrayInsn(sb.toString(), nmae.dimension);
        break;
    case INVOKE_NEW:
        asm.visitTypeInsn(NEW, toInternal(((InvokeExpr) value).owner));
        asm.visitInsn(DUP);
        // pass through
    case INVOKE_INTERFACE:
    case INVOKE_SPECIAL:
    case INVOKE_STATIC:
    case INVOKE_VIRTUAL:
        InvokeExpr ie = (InvokeExpr) value;
        int i = 0;
        if (value.vt != VT.INVOKE_STATIC && value.vt != VT.INVOKE_NEW) {
            i = 1;
            accept(value.ops[0], asm);
        }
        for (int j = 0; i < value.ops.length; i++, j++) {
            Value vb = value.ops[i];
            accept(vb, asm);
            insertI2x(vb.valueType, ie.args[j], asm);
        }

        int opcode;
        switch (value.vt) {
        case INVOKE_VIRTUAL:
            opcode = INVOKEVIRTUAL;
            break;
        case INVOKE_INTERFACE:
            opcode = INVOKEINTERFACE;
            break;
        case INVOKE_NEW:
        case INVOKE_SPECIAL:
            opcode = INVOKESPECIAL;
            break;
        case INVOKE_STATIC:
            opcode = INVOKESTATIC;
            break;
        default:
            opcode = -1;
        }

        asm.visitMethodInsn(opcode, toInternal(ie.owner), ie.name,
                buildMethodDesc(ie.vt == VT.INVOKE_NEW ? "V" : ie.ret, ie.args));
        break;
    }
}