Example usage for org.objectweb.asm Opcodes ICONST_0

List of usage examples for org.objectweb.asm Opcodes ICONST_0

Introduction

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

Prototype

int ICONST_0

To view the source code for org.objectweb.asm Opcodes ICONST_0.

Click Source Link

Usage

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPreemptionPoint(MethodVisitor mv, int type) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    switch (type) {
    case 0:/*www  .  j  av a  2s  .  c  om*/
        mv.visitInsn(Opcodes.ICONST_0);
        break;
    case 1:
        mv.visitInsn(Opcodes.ICONST_1);
        break;
    case 2:
        mv.visitInsn(Opcodes.ICONST_2);
        break;
    case 3:
        mv.visitInsn(Opcodes.ICONST_3);
        break;
    default:
        throw new AssertionError("Unsupported type: " + type);
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "preemptionPoint", "(I)V");
}

From source file:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

/**
 * Rewrites the method bytecode to remove the "Stub!" exception.
 *///from w  w w  .  j  a  v  a 2  s  . c o m
private void fixMethodBody(MethodNode methodNode, ClassNode classNode) {
    if ((methodNode.access & Opcodes.ACC_NATIVE) != 0 || (methodNode.access & Opcodes.ACC_ABSTRACT) != 0) {
        // Abstract and native method don't have bodies to rewrite.
        return;
    }

    if ((classNode.access & Opcodes.ACC_ENUM) != 0 && ENUM_METHODS.contains(methodNode.name)) {
        // Don't break enum classes.
        return;
    }

    Type returnType = Type.getReturnType(methodNode.desc);
    InsnList instructions = methodNode.instructions;

    if (methodNode.name.equals(CONSTRUCTOR)) {
        // Keep the call to parent constructor, delete the exception after that.

        boolean deadCode = false;
        for (AbstractInsnNode instruction : instructions.toArray()) {
            if (!deadCode) {
                if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) {
                    instructions.insert(instruction, new InsnNode(Opcodes.RETURN));
                    // Start removing all following instructions.
                    deadCode = true;
                }
            } else {
                instructions.remove(instruction);
            }
        }
    } else {
        instructions.clear();

        if (returnDefaultValues || methodNode.name.equals(CLASS_CONSTRUCTOR)) {
            if (INTEGER_LIKE_TYPES.contains(returnType)) {
                instructions.add(new InsnNode(Opcodes.ICONST_0));
            } else if (returnType.equals(Type.LONG_TYPE)) {
                instructions.add(new InsnNode(Opcodes.LCONST_0));
            } else if (returnType.equals(Type.FLOAT_TYPE)) {
                instructions.add(new InsnNode(Opcodes.FCONST_0));
            } else if (returnType.equals(Type.DOUBLE_TYPE)) {
                instructions.add(new InsnNode(Opcodes.DCONST_0));
            } else {
                instructions.add(new InsnNode(Opcodes.ACONST_NULL));
            }

            instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN)));
        } else {
            instructions.insert(throwExceptionsList(methodNode, classNode));
        }
    }
}

From source file:com.android.tools.layoutlib.create.StubMethodAdapter.java

License:Apache License

private void generateInvoke() {
    /* Generates the code:
     *  OverrideMethod.invoke("signature", mIsNative ? true : false, null or this);
     *//*from   w  w w . j a va  2 s. co m*/
    mParentVisitor.visitLdcInsn(mInvokeSignature);
    // push true or false
    mParentVisitor.visitInsn(mIsNative ? Opcodes.ICONST_1 : Opcodes.ICONST_0);
    // push null or this
    if (mIsStatic) {
        mParentVisitor.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mParentVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    }

    int sort = mReturnType != null ? mReturnType.getSort() : Type.VOID;
    switch (sort) {
    case Type.VOID:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeV",
                "(Ljava/lang/String;ZLjava/lang/Object;)V");
        mParentVisitor.visitInsn(Opcodes.RETURN);
        break;
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeI",
                "(Ljava/lang/String;ZLjava/lang/Object;)I");
        switch (sort) {
        case Type.BOOLEAN:
            Label l1 = new Label();
            mParentVisitor.visitJumpInsn(Opcodes.IFEQ, l1);
            mParentVisitor.visitInsn(Opcodes.ICONST_1);
            mParentVisitor.visitInsn(Opcodes.IRETURN);
            mParentVisitor.visitLabel(l1);
            mParentVisitor.visitInsn(Opcodes.ICONST_0);
            break;
        case Type.CHAR:
            mParentVisitor.visitInsn(Opcodes.I2C);
            break;
        case Type.BYTE:
            mParentVisitor.visitInsn(Opcodes.I2B);
            break;
        case Type.SHORT:
            mParentVisitor.visitInsn(Opcodes.I2S);
            break;
        }
        mParentVisitor.visitInsn(Opcodes.IRETURN);
        break;
    case Type.LONG:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeL",
                "(Ljava/lang/String;ZLjava/lang/Object;)J");
        mParentVisitor.visitInsn(Opcodes.LRETURN);
        break;
    case Type.FLOAT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeF",
                "(Ljava/lang/String;ZLjava/lang/Object;)F");
        mParentVisitor.visitInsn(Opcodes.FRETURN);
        break;
    case Type.DOUBLE:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeD",
                "(Ljava/lang/String;ZLjava/lang/Object;)D");
        mParentVisitor.visitInsn(Opcodes.DRETURN);
        break;
    case Type.ARRAY:
    case Type.OBJECT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeA",
                "(Ljava/lang/String;ZLjava/lang/Object;)Ljava/lang/Object;");
        mParentVisitor.visitTypeInsn(Opcodes.CHECKCAST, mReturnType.getInternalName());
        mParentVisitor.visitInsn(Opcodes.ARETURN);
        break;
    }

}

From source file:com.android.tools.lint.checks.ApiDetector.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API
private static void checkSwitchBlock(ClassContext context, ClassNode classNode, FieldInsnNode field,
        MethodNode method, String name, String owner, int api, int minSdk) {
    // Switch statements on enums are tricky. The compiler will generate a method
    // which returns an array of the enum constants, indexed by their ordinal() values.
    // However, we only want to complain if the code is actually referencing one of
    // the non-available enum fields.
    ////from w  ww  .jav  a  2  s . c o  m
    // For the android.graphics.PorterDuff.Mode enum for example, the first few items
    // in the array are populated like this:
    //
    //   L0
    //    ALOAD 0
    //    GETSTATIC android/graphics/PorterDuff$Mode.ADD : Landroid/graphics/PorterDuff$Mode;
    //    INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //    ICONST_1
    //    IASTORE
    //   L1
    //    GOTO L3
    //   L2
    //   FRAME FULL [[I] [java/lang/NoSuchFieldError]
    //    POP
    //   L3
    //   FRAME SAME
    //    ALOAD 0
    //    GETSTATIC android/graphics/PorterDuff$Mode.CLEAR : Landroid/graphics/PorterDuff$Mode;
    //    INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //    ICONST_2
    //    IASTORE
    //    ...
    // So if we for example find that the "ADD" field isn't accessible, since it requires
    // API 11, we need to
    //   (1) First find out what its ordinal number is. We can look at the following
    //       instructions to discover this; it's the "ICONST_1" and "IASTORE" instructions.
    //       (After ICONST_5 it moves on to BIPUSH 6, BIPUSH 7, etc.)
    //   (2) Find the corresponding *usage* of this switch method. For the above enum,
    //       the switch ordinal lookup method will be called
    //         "$SWITCH_TABLE$android$graphics$PorterDuff$Mode" with desc "()[I".
    //       This means we will be looking for an invocation in some other method which looks
    //       like this:
    //         INVOKESTATIC (current class).$SWITCH_TABLE$android$graphics$PorterDuff$Mode ()[I
    //       (obviously, it can be invoked more than once)
    //       Note that it can be used more than once in this class and all sites should be
    //       checked!
    //   (3) Look up the corresponding table switch, which should look something like this:
    //        INVOKESTATIC (current class).$SWITCH_TABLE$android$graphics$PorterDuff$Mode ()[I
    //        ALOAD 0
    //        INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //        IALOAD
    //        LOOKUPSWITCH
    //          2: L1
    //          11: L2
    //          default: L3
    //       Here we need to see if the LOOKUPSWITCH instruction is referencing our target
    //       case. Above we were looking for the "ADD" case which had ordinal 1. Since this
    //       isn't explicitly referenced, we can ignore this field reference.
    AbstractInsnNode next = field.getNext();
    if (next == null || next.getOpcode() != Opcodes.INVOKEVIRTUAL) {
        return;
    }
    next = next.getNext();
    if (next == null) {
        return;
    }
    int ordinal;
    switch (next.getOpcode()) {
    case Opcodes.ICONST_0:
        ordinal = 0;
        break;
    case Opcodes.ICONST_1:
        ordinal = 1;
        break;
    case Opcodes.ICONST_2:
        ordinal = 2;
        break;
    case Opcodes.ICONST_3:
        ordinal = 3;
        break;
    case Opcodes.ICONST_4:
        ordinal = 4;
        break;
    case Opcodes.ICONST_5:
        ordinal = 5;
        break;
    case Opcodes.BIPUSH: {
        IntInsnNode iin = (IntInsnNode) next;
        ordinal = iin.operand;
        break;
    }
    default:
        return;
    }

    // Find usages of this call site
    List methodList = classNode.methods;
    for (Object m : methodList) {
        InsnList nodes = ((MethodNode) m).instructions;
        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            if (instruction.getOpcode() != Opcodes.INVOKESTATIC) {
                continue;
            }
            MethodInsnNode node = (MethodInsnNode) instruction;
            if (node.name.equals(method.name) && node.desc.equals(method.desc)
                    && node.owner.equals(classNode.name)) {
                // Find lookup switch
                AbstractInsnNode target = getNextInstruction(node);
                while (target != null) {
                    if (target.getOpcode() == Opcodes.LOOKUPSWITCH) {
                        LookupSwitchInsnNode lookup = (LookupSwitchInsnNode) target;
                        @SuppressWarnings("unchecked") // ASM API
                        List<Integer> keys = lookup.keys;
                        if (keys != null && keys.contains(ordinal)) {
                            String fqcn = ClassContext.getFqcn(owner) + '#' + name;
                            String message = String.format(
                                    "Enum value requires API level %1$d " + "(current min is %2$d): %3$s", api,
                                    minSdk, fqcn);
                            report(context, message, lookup, (MethodNode) m, name, null,
                                    SearchHints.create(FORWARD).matchJavaSymbol());

                            // Break out of the inner target search only; the switch
                            // statement could be used in other places in this class as
                            // well and we want to report all problematic usages.
                            break;
                        }
                    }
                    target = getNextInstruction(target);
                }
            }
        }
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value/*from ww w  . ja  va 2  s .  c o  m*/
 */
public static void getInt(MethodVisitor method, int value) {
    if (-1 <= value && value <= 5) {
        method.visitInsn(Opcodes.ICONST_0 + value);
    } else if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) {
        method.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) {
        method.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        method.visitLdcInsn(value);
    }
}

From source file:com.axway.jmb.builders.Methods.java

License:Open Source License

public static void beginProcedureCall(MethodBuilder method, Type clazz, Method getInstanceMethod,
        int arraySize) {
    method.invokeStatic(clazz, getInstanceMethod);
    method.visitInsn(Opcodes.ICONST_0 + arraySize);
    method.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

@Override
public Void visitFunctionInvocation(FunctionInvocationContext ctx) {
    debug("visitFunctionInvocation()");
    String functionFullName = ctx.functionName().getText();
    String moduleName = "";
    String functionName = functionFullName;

    isStatementCall = true;//from   ww w . jav a2s  .com
    statementCallCurrentParameterIndex = 0;

    if (functionFullName.contains(".")) {
        moduleName = functionFullName.split("\\.")[0];
        functionName = functionFullName.split("\\.")[1];
    }
    debug("call function :" + Utils.getJavaFullyQualifiedClassName(moduleName) + "."
            + Utils.getJavaMethodName(functionName) + "(...)");

    Type moduleType = Utils.getJavaFullyQualifiedClassType(moduleName);

    Method getModuleMethod = new Method("getModule", moduleType, new Type[0]);
    Method calledFunction = new Method(Utils.getJavaMethodName(functionName),
            Type.getObjectType("java/lang/Object"), new Type[] { Type.getObjectType("[Ljava/lang/Object;") });
    int arraySize = ctx.argumentList() != null ? ctx.argumentList().expression().size() : 0;

    if ("".equals(moduleName)) {
        // internal module in an executable module
    } else {
        if (currentMethod != null) {
            currentMethod.invokeStatic(moduleType, getModuleMethod);
            currentMethod.visitInsn(Opcodes.ICONST_0 + arraySize);
            currentMethod.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        } else {
            currentConstructor.invokeStatic(moduleType, getModuleMethod);
            currentConstructor.visitInsn(Opcodes.ICONST_0 + arraySize);
            currentConstructor.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        }
    }

    super.visitFunctionInvocation(ctx);

    if ("".equals(moduleName)) {
        // internal module in an executable module
    } else {
        if (currentMethod != null) {
            currentMethod.invokeVirtual(moduleType, calledFunction);
        } else {
            currentConstructor.invokeVirtual(moduleType, calledFunction);
        }
    }

    isStatementCall = false;

    return null;
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

@Override
public Void visitVariableIdentifier(VariableIdentifierContext ctx) {
    super.visitVariableIdentifier(ctx);

    if (isStatementCall) {
        try {// w w w .j  a  v  a 2 s .co  m
            debug("visitVariableIdentifier() for statementCallParameter");
            String varName = convertVariableName(ctx.getText());
            if (currentMethod != null) {
                currentMethod.visitInsn(Opcodes.DUP);
                currentMethod.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);

                if (currentMethod.isLocalVariableDefined(varName)) {
                    currentMethod.loadFromLocalVar(varName, false, 0);
                } else if (currentModule.isFieldDefined(varName)) {
                    currentMethod.loadFromField(currentModule, varName, false, 0);
                } else {
                    throw new CompileException("Variable " + varName + " used, but not defined.");
                }

                currentMethod.visitInsn(Opcodes.AASTORE);
            } else {
                currentConstructor.visitInsn(Opcodes.DUP);
                currentConstructor.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);

                if (currentConstructor.isLocalVariableDefined(varName)) {
                    currentConstructor.loadFromLocalVar(varName, false, 0);
                } else if (currentModule.isFieldDefined(varName)) {
                    currentConstructor.loadFromField(currentModule, varName, false, 0);
                } else {
                    throw new CompileException("Variable " + varName + " used, but not defined.");
                }

                currentConstructor.visitInsn(Opcodes.AASTORE);
            }

            statementCallCurrentParameterIndex++;
        } catch (CompileException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    return null;
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

private void saveLiteral(Object obj) {
    if (!isStatementCall) {
        if (currentMethod != null) {
            currentMethod.pushOnStack(obj);
        } else {//from  w  ww . ja  v  a  2 s  .  co  m
            currentConstructor.pushOnStack(obj);
        }
    } else {
        if (currentMethod != null) {
            currentMethod.visitInsn(Opcodes.DUP);
            currentMethod.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);
            currentMethod.pushOnStack(obj);
            currentMethod.visitInsn(Opcodes.AASTORE);
        } else {
            currentConstructor.visitInsn(Opcodes.DUP);
            currentConstructor.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);
            currentConstructor.pushOnStack(obj);
            currentConstructor.visitInsn(Opcodes.AASTORE);
        }
        statementCallCurrentParameterIndex++;
    }
}

From source file:com.axway.jmb.MethodBuilder.java

License:Open Source License

public void storeInLocalVar(String varName, boolean isStatementCall, int statementCallCurrentParameterIndex)
        throws CompileException {
    LocalVariable lv = localVars.get(varName);
    if (lv == null)
        throw new CompileException("Local variable " + varName + " used, but not defined.");

    if (isStatementCall) {
        visitInsn(Opcodes.DUP);/*from  w  w  w .j  ava2  s  . c  om*/
        visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);
        visitInsn(AALOAD);
    }

    if (lv.isArray()) {
        visitTypeInsn(CHECKCAST, lv.getType().getArrayJvmType(lv.getArrayDimension()).toString());
        storeLocal(lv.getArrayPosition(), lv.getType().getArrayJvmType(lv.getArrayDimension()));
    } else {
        if (lv.getType() == JMBVariableType.FIXED_STRING) {
            invokeVirtual(Type.getType(String.class), Method.getMethod("char[] toCharArray()"));
            push((int) lv.getFixedStringLength());
            invokeStatic(Type.getType(Arrays.class), Method.getMethod("char[] copyOf(char[], int)"));
        } else if (lv.getType() == JMBVariableType.STRING) {
            invokeVirtual(Type.getType(Object.class), Method.getMethod("String toString()"));
        } else if (lv.getType() == JMBVariableType.INTEGER) {
            visitTypeInsn(CHECKCAST, "java/lang/Long");
        } else if (lv.getType() == JMBVariableType.FLOAT) {
            visitTypeInsn(CHECKCAST, "java/lang/Double");
        } else if (lv.getType() == JMBVariableType.DATE) {
            visitTypeInsn(CHECKCAST, "java/util/Date");
        }

        storeLocal(lv.getArrayPosition(), lv.getType().getJvmType());
    }

    debug("## store _" + lv.getArrayPosition());
}