Example usage for org.objectweb.asm Opcodes ACONST_NULL

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

Introduction

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

Prototype

int ACONST_NULL

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

Click Source Link

Usage

From source file:org.jboss.byteman.rule.expression.NullLiteral.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    // load null or zero

    mv.visitInsn(Opcodes.ACONST_NULL);

    compileContext.addStackCount(1);/*from   w  w w  .jav a  2s .c  om*/
}

From source file:org.jboss.byteman.rule.expression.ReturnExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    Type valueType = (returnValue == null ? Type.VOID : returnValue.getType());
    int currentStack = compileContext.getStackCount();
    int expected = 1;

    // ok, we need to create the EarlyReturnException instance and then
    // initialise it using the appropriate return value or null if no
    // return value is needed. strictly we should maybe delay the
    // new until after computing the return expression so we avoid a new
    // if the expression throws an error. but that means we end up doing
    // stack manipulations so lets do it the easy way.

    // create am EarlyReturnException -- adds 1 to stack
    String exceptionClassName = Type.internalName(EarlyReturnException.class);
    mv.visitTypeInsn(Opcodes.NEW, exceptionClassName);
    compileContext.addStackCount(1);/*from  w w  w.j a v  a  2  s .c  om*/
    // copy the exception so we can initialise it -- adds 1 to stack
    mv.visitInsn(Opcodes.DUP);
    compileContext.addStackCount(1);
    // stack a string constant to initialise the exception with -- adds 1 to stack
    mv.visitLdcInsn("return from " + rule.getName());
    compileContext.addStackCount(1);
    // stack any required return value or null -- adds 1 to stack but may use 2 slots
    if (returnValue != null) {
        returnValue.compile(mv, compileContext);
        // we may need to convert from the value type to the return type
        if (valueType != type) {
            compileTypeConversion(valueType, type, mv, compileContext);
        }
        if (type.isPrimitive()) {
            // we need an object not a primitive
            compileBox(Type.boxType(type), mv, compileContext);
        }
    } else {
        // just push null
        mv.visitInsn(Opcodes.ACONST_NULL);
        compileContext.addStackCount(1);
    }
    // construct the exception -- pops 3
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClassName, "<init>",
            "(Ljava/lang/String;Ljava/lang/Object;)V");
    compileContext.addStackCount(-3);

    // check current stack and increment max stack if necessary
    if (compileContext.getStackCount() != currentStack + expected) {
        throw new CompileException("ReturnExpression.compile : invalid stack height "
                + compileContext.getStackCount() + " expecting " + (currentStack + expected));
    }

    // now insert the throw instruction and decrement the stack height accordingly

    mv.visitInsn(Opcodes.ATHROW);
    compileContext.addStackCount(-1);
}

From source file:org.jboss.byteman.rule.expression.StaticExpression.java

License:Open Source License

public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int expected;

    // compile a field access

    if (isPublicField) {
        String ownerType = Type.internalName(field.getDeclaringClass());
        String fieldName = field.getName();
        String fieldType = Type.internalName(field.getType(), true);
        mv.visitFieldInsn(Opcodes.GETSTATIC, ownerType, fieldName, fieldType);
        expected = (type.getNBytes() > 4 ? 2 : 1);
        compileContext.addStackCount(expected);
    } else {/* w  w w .ja  va  2s  . com*/
        // since this is a private field we need to do the access using reflection
        // stack the helper, a null owner and the field index
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitLdcInsn(fieldIndex);
        compileContext.addStackCount(3);
        // use the HelperAdapter method getAccessibleField to get the field value
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class),
                "getAccessibleField", "(Ljava/lang/Object;I)Ljava/lang/Object;");
        // we popped three words and added one object as result
        compileContext.addStackCount(-2);
        // convert Object to primitive or cast to subtype if required
        compileTypeConversion(Type.OBJECT, type, mv, compileContext);
    }
}

From source file:org.jboss.byteman.rule.expression.StaticExpression.java

License:Open Source License

@Override
public void compileAssign(MethodVisitor mv, CompileContext compileContext) throws CompileException {
    // make sure we are at the right source line
    compileContext.notifySourceLine(line);

    int currentStack = compileContext.getStackCount();
    int size = (type.getNBytes() > 4 ? 2 : 1);

    // copy the value so we leave a result
    // increases stack height by size words
    if (size == 1) {
        mv.visitInsn(Opcodes.DUP);//from   ww  w. j av a  2 s . com
    } else {
        mv.visitInsn(Opcodes.DUP2);
    }
    compileContext.addStackCount(size);
    // compile a static field update

    if (isPublicField) {
        String ownerType = Type.internalName(field.getDeclaringClass());
        String fieldName = field.getName();
        String fieldType = Type.internalName(field.getType(), true);
        compileContext.addStackCount(-size);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, ownerType, fieldName, fieldType);
    } else {
        // since this is a private field we need to do the update using reflection
        // box the value to an object if necessary
        // [.. val(s) val(s) ==> val(s) valObj]
        if (type.isPrimitive()) {
            compileBox(Type.boxType(type), mv, compileContext);
        }
        // stack the helper and then swap it so it goes under the value
        // [.. val(s) valObj ==> val(s) valObj helper ==> val(s) helper valObj]
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.SWAP);
        // stack a null owner then swap it so it goes under the value
        // [val(s) helper valObj ==> val(s) helper valObj null ==> val(s) helper null valObj]
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitInsn(Opcodes.SWAP);
        // now stack the field index
        // [.. val(s) helper null valObj ==> val(s) helper null valObj index ]
        mv.visitLdcInsn(fieldIndex);
        // we added three more words
        compileContext.addStackCount(3);
        // use the HelperAdapter method setAccessibleField to set the field value
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.internalName(HelperAdapter.class),
                "setAccessibleField", "(Ljava/lang/Object;Ljava/lang/Object;I)V");
        // we popped four args
        compileContext.addStackCount(-4);
    }

    if (compileContext.getStackCount() != currentStack) {
        throw new CompileException("StaticExpression.compileAssign : invalid stack height "
                + compileContext.getStackCount() + " expecting " + currentStack);
    }
}

From source file:org.jboss.byteman.rule.RuleElement.java

License:Open Source License

protected void compileStringConversion(Type fromType, Type toType, MethodVisitor mv,
        CompileContext compileContext) throws CompileException {
    assert toType == Type.STRING;
    if (fromType.isObject() || fromType.isArray() || (fromType.isNumeric() && !fromType.isPrimitive())) {
        // use the toString method if the object is non null otherwise just replace it with null
        Label elseLabel = new Label();
        Label endLabel = new Label();
        // if (object == null)
        mv.visitInsn(Opcodes.DUP);//  w  ww. ja v a2s  .c  o  m
        // the above dup bumps the stack height
        compileContext.addStackCount(1);
        mv.visitJumpInsn(Opcodes.IFNONNULL, elseLabel);
        compileContext.addStackCount(-1);
        // then string = "null"
        mv.visitInsn(Opcodes.POP);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitJumpInsn(Opcodes.GOTO, endLabel);
        // else string = object.toString()
        mv.visitLabel(elseLabel);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        mv.visitLabel(endLabel);
    } else if (fromType == Type.Z) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;");
    } else if (fromType == Type.B) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "toString", "(B)Ljava/lang/String;");
    } else if (fromType == Type.S) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "toString", "(S)Ljava/lang/String;");
    } else if (fromType == Type.C) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "toString", "(C)Ljava/lang/String;");
    } else if (fromType == Type.I) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;");
    } else if (fromType == Type.J) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "toString", "(J)Ljava/lang/String;");
        compileContext.addStackCount(-1);
    } else if (fromType == Type.F) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "toString", "(F)Ljava/lang/String;");
    } else if (fromType == Type.D) {
        // use the toString method
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;");
        compileContext.addStackCount(-1);
    }
}

From source file:org.jooby.internal.apitool.BytecodeRouteParser.java

License:Apache License

private Object paramValue(final ClassLoader loader, final ClassNode owner, final MethodNode method,
        final AbstractInsnNode n) {
    if (n instanceof LdcInsnNode) {

        Object cst = ((LdcInsnNode) n).cst;
        if (cst instanceof Type) {
            boolean typePresent = new Insn<>(method, n).next().filter(is(MethodInsnNode.class)).findFirst()
                    .map(MethodInsnNode.class::cast).filter(mutantToSomething().or(getOrCreateKotlinClass()))
                    .isPresent();//  www  .j  ava 2  s .c o m
            if (typePresent) {
                return null;
            }
            return loadType(loader, ((Type) cst).getClassName());
        }
        return cst;
    } else if (n instanceof InsnNode) {
        InsnNode insn = (InsnNode) n;
        switch (insn.getOpcode()) {
        case Opcodes.ICONST_0:
            return 0;
        case Opcodes.ICONST_1:
            return 1;
        case Opcodes.ICONST_2:
            return 2;
        case Opcodes.ICONST_3:
            return 3;
        case Opcodes.ICONST_4:
            return 4;
        case Opcodes.ICONST_5:
            return 5;
        case Opcodes.LCONST_0:
            return 0L;
        case Opcodes.LCONST_1:
            return 1L;
        case Opcodes.FCONST_0:
            return 0f;
        case Opcodes.FCONST_1:
            return 1f;
        case Opcodes.FCONST_2:
            return 2f;
        case Opcodes.DCONST_0:
            return 0d;
        case Opcodes.DCONST_1:
            return 1d;
        case Opcodes.ICONST_M1:
            return -1;
        case Opcodes.ACONST_NULL:
            return null;
        }
    } else if (n instanceof IntInsnNode) {
        IntInsnNode intinsn = (IntInsnNode) n;
        return intinsn.operand;
    } else if (n instanceof FieldInsnNode) {
        // toEnum
        FieldInsnNode finsn = (FieldInsnNode) n;
        if (finsn.getOpcode() == GETSTATIC) {
            java.lang.reflect.Type possibleEnum = loadType(loader, finsn.owner);
            if (MoreTypes.getRawType(possibleEnum).isEnum()) {
                return finsn.name;
            }
        }
    }
    return n;
}

From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java

License:Open Source License

/**
 * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null}
 * if the instruction is not a full statement (in that case, the instruction is stored in the
 * given Expression {@link Stack})./*from w ww.  j  av  a2s.c  o  m*/
 * 
 * @param insnNode the instruction to read
 * @param expressionStack the expression stack to put on or pop from.
 * @param localVariables the local variables
 * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created
 *         after reading the current instruction.
 * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode
 *      instruction listings on Wikipedia</a>
 */
private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack,
        final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) {
    final List<Statement> statements = new ArrayList<>();
    final AbstractInsnNode insnNode = insnCursor.getCurrent();
    switch (insnNode.getOpcode()) {
    // return a reference from a method
    case Opcodes.ARETURN:
        // return an integer from a method
    case Opcodes.IRETURN:
        statements.add(new ReturnStatement(expressionStack.pop()));
        break;
    // return void from method
    case Opcodes.RETURN:
        // wrap all pending expressions into ExpressionStatements
        while (!expressionStack.isEmpty()) {
            final Expression pendingExpression = expressionStack.pop();
            statements.add(new ExpressionStatement(pendingExpression));
        }
        break;
    // push a null reference onto the stack
    case Opcodes.ACONST_NULL:
        expressionStack.add(new NullLiteral());
        break;
    // load the int value 0 onto the stack
    case Opcodes.ICONST_0:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(0));
        break;
    // load the int value 1 onto the stack
    case Opcodes.ICONST_1:
        // applies for byte, short, int and boolean
        expressionStack.add(new NumberLiteral(1));
        break;
    // load the int value 2 onto the stack
    case Opcodes.ICONST_2:
        expressionStack.add(new NumberLiteral(2));
        break;
    // load the int value 3 onto the stack
    case Opcodes.ICONST_3:
        expressionStack.add(new NumberLiteral(3));
        break;
    // load the int value 4 onto the stack
    case Opcodes.ICONST_4:
        expressionStack.add(new NumberLiteral(4));
        break;
    // load the int value 5 onto the stack
    case Opcodes.ICONST_5:
        expressionStack.add(new NumberLiteral(5));
        break;
    // push the long 0 onto the stack
    case Opcodes.LCONST_0:
        expressionStack.add(new NumberLiteral(0L));
        break;
    // push the long 1 onto the stack
    case Opcodes.LCONST_1:
        expressionStack.add(new NumberLiteral(1L));
        break;
    // push the 0.0f onto the stack
    case Opcodes.FCONST_0:
        expressionStack.add(new NumberLiteral(0f));
        break;
    // push the 1.0f onto the stack
    case Opcodes.FCONST_1:
        expressionStack.add(new NumberLiteral(1f));
        break;
    // push the 2.0f onto the stack
    case Opcodes.FCONST_2:
        expressionStack.add(new NumberLiteral(2f));
        break;
    // push the constant 0.0 onto the stack
    case Opcodes.DCONST_0:
        expressionStack.add(new NumberLiteral(0d));
        break;
    // push the constant 1.0 onto the stack
    case Opcodes.DCONST_1:
        expressionStack.add(new NumberLiteral(1d));
        break;
    // compare two longs values
    case Opcodes.LCMP:
        // compare two doubles
    case Opcodes.DCMPL:
        // compare two doubles
    case Opcodes.DCMPG:
        // compare two floats
    case Opcodes.FCMPL:
        // compare two floats
    case Opcodes.FCMPG:
        statements.addAll(
                readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables));
        break;
    // add 2 ints
    case Opcodes.IADD:
        expressionStack.add(readOperation(Operator.ADD, expressionStack));
        break;
    // int subtract
    case Opcodes.ISUB:
        expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack));
        break;
    // multiply 2 integers
    case Opcodes.IMUL:
        expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack));
        break;
    // divide 2 integers
    case Opcodes.IDIV:
        expressionStack.add(readOperation(Operator.DIVIDE, expressionStack));
        break;
    // negate int
    case Opcodes.INEG:
        expressionStack.add(inverseInteger(expressionStack));
        break;
    // discard the top value on the stack
    case Opcodes.POP:
        statements.add(new ExpressionStatement(expressionStack.pop()));
        break;
    // duplicate the value on top of the stack
    case Opcodes.DUP:
        expressionStack.push(expressionStack.peek());
        break;
    // insert a copy of the top value into the stack two values from the top.
    case Opcodes.DUP_X1:
        expressionStack.add(expressionStack.size() - 2, expressionStack.peek());
        break;
    // store into a reference in an array
    case Opcodes.AASTORE:
        readArrayStoreInstruction(insnNode, expressionStack);
        break;
    // converts Float to Double -> ignored.
    case Opcodes.F2D:
        break;
    default:
        throw new AnalyzeException(
                "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported.");
    }
    return statements;
}

From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java

License:Apache License

/**
 * load the constant on the operand stack. primitives auto-boxed.
 *///from  w ww.ja  va 2  s.  co m
void loadConstant(Object value, MethodVisitor mv) {
    if (value == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else if (value instanceof String) {
        mv.visitLdcInsn(value);
    } else if (value instanceof Character) {
        String className = "java/lang/Character";
        mv.visitTypeInsn(Opcodes.NEW, className);
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(value);
        String methodType = "(C)V";
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", methodType);
    } else if (value instanceof Number) {
        /** todo it would be more efficient to generate class constants */
        Number n = (Number) value;
        String className = BytecodeHelper.getClassInternalName(value.getClass().getName());
        mv.visitTypeInsn(Opcodes.NEW, className);
        mv.visitInsn(Opcodes.DUP);
        String methodType;
        if (n instanceof Integer) {
            //pushConstant(n.intValue());
            mv.visitLdcInsn(n);
            methodType = "(I)V";
        } else if (n instanceof Double) {
            mv.visitLdcInsn(n);
            methodType = "(D)V";
        } else if (n instanceof Float) {
            mv.visitLdcInsn(n);
            methodType = "(F)V";
        } else if (n instanceof Long) {
            mv.visitLdcInsn(n);
            methodType = "(J)V";
        } else if (n instanceof BigDecimal) {
            mv.visitLdcInsn(n.toString());
            methodType = "(Ljava/lang/String;)V";
        } else if (n instanceof BigInteger) {
            mv.visitLdcInsn(n.toString());
            methodType = "(Ljava/lang/String;)V";
        } else if (n instanceof Short) {
            mv.visitLdcInsn(n);
            methodType = "(S)V";
        } else if (n instanceof Byte) {
            mv.visitLdcInsn(n);
            methodType = "(B)V";
        } else {
            throw new ClassGeneratorException("Cannot generate bytecode for constant: " + value + " of type: "
                    + value.getClass().getName() + ".  Numeric constant type not supported.");
        }
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", methodType);
    } else if (value instanceof Boolean) {
        Boolean bool = (Boolean) value;
        String text = (bool.booleanValue()) ? "TRUE" : "FALSE";
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Boolean", text, "Ljava/lang/Boolean;");
    } else if (value instanceof Class) {
        Class vc = (Class) value;
        if (vc.getName().equals("java.lang.Void")) {
            // load nothing here for void
        } else {
            throw new ClassGeneratorException("Cannot generate bytecode for constant: " + value + " of type: "
                    + value.getClass().getName());
        }
    } else {
        throw new ClassGeneratorException(
                "Cannot generate bytecode for constant: " + value + " of type: " + value.getClass().getName());
    }
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Encodes instructions to push a null object reference onto the operand stack.
 * @param context /*from ww w . j  av  a2s  . c o  m*/
 * @return JavaTypeName the type of the result on the operand stack.
 */
private static JavaTypeName encodePushNullValue(GenerationContext context) {
    MethodVisitor mv = context.getMethodVisitor();
    mv.visitInsn(Opcodes.ACONST_NULL);

    return JavaTypeName.OBJECT;
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.MethodCallMethodVisitor.java

License:Apache License

private void putReturnValueOnStack(final String desc, final String name) {
    final Type returnType = Type.getReturnType(desc);
    if (!returnType.equals(Type.VOID_TYPE)) {
        final Integer opCode = RETURN_TYPE_MAP.get(returnType);
        if (opCode == null) {
            this.mv.visitInsn(Opcodes.ACONST_NULL);
        } else {//from  ww w .  j a v  a 2s  .  c  o m
            this.mv.visitInsn(opCode);
        }
    } else if (MethodInfo.isConstructor(name)) {
        this.mv.visitInsn(Opcodes.ACONST_NULL);
    }
}