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.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation areturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opCode, final MethodVisitor mv) {

            // Strategy translated from jumble BCEL code
            // if result is non-null make it null, otherwise hard case
            // for moment throw runtime exception
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
            mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
            mv.visitInsn(Opcodes.DUP);//w  w  w  .  j a  va  2s. co m
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "()V", false);
            mv.visitInsn(Opcodes.ATHROW);
            mv.visitLabel(l1);
            mv.visitInsn(Opcodes.ACONST_NULL);
            mv.visitInsn(Opcodes.ARETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "mutated return of Object value for " + methodInfo.getDescription()
                    + " to ( if (x != null) null else throw new RuntimeException )";
        }

    };
}

From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java

License:Open Source License

@Test
public void test_aconst_null() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.ACONST_NULL));
    assertStack(programState, ObjectConstraint.NULL);
}

From source file:org.spongepowered.despector.emitter.bytecode.instruction.BytecodeNullConstantEmitter.java

License:Open Source License

@Override
public void emit(BytecodeEmitterContext ctx, NullConstant arg, TypeSignature type) {
    MethodVisitor mv = ctx.getMethodVisitor();
    mv.visitInsn(Opcodes.ACONST_NULL);
    ctx.updateStack(1);
}

From source file:org.wavescale.hotload.transformer.api.VarArgsHelperMethod.java

License:Open Source License

/**
 * Fills the method body with an empty content, usually a null return.
 *///from   w w w . j  a va2s. c  o m
private void addEmptyContent() {
    InsnList insnList = this.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String className = "L" + this.clazz.getCanonicalName();
    this.localVariables.add(new LocalVariableNode("this", "L" + className + ";", null, l0, l1, 0));
    this.localVariables.add(new LocalVariableNode("methodName", "Ljava/lang/String;", null, l0, l1, 1));
    this.localVariables.add(new LocalVariableNode("args", "[Ljava/lang/Object;", null, l0, l1, 2));
}

From source file:org.zoeey.ztpl.compiler.ByteCodeHelper.java

License:LGPL

/**
 * //from   ww w.j  a v  a2  s  . com
 * @param obj
 * @return
 */
public void visitObject(Object obj) {
    if (obj == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    } else {
        @SuppressWarnings("unchecked")
        Class<Object> clazz = (Class<Object>) obj.getClass();
        if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) {
            visitInt(new ZObject(obj).toInteger());
        } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) {
            visitDouble(new ZObject(obj).toDouble());
        } else if (Boolean.class.isAssignableFrom(clazz)) {
            visitBoolean(new ZObject(obj).toBoolean());
        } else {
            mv.visitLdcInsn(obj);
        }
    }
}

From source file:pl.clareo.coroutines.core.ClassTransformer.java

License:Apache License

@SuppressWarnings("unchecked")
private static InsnList createDebugFrame(MethodNode coroutine) {
    InsnList insn = new InsnList();
    int nLocals = coroutine.maxLocals;
    String[] names = new String[nLocals];
    List<LocalVariableNode> locals = coroutine.localVariables;
    fillVariableNames(names, locals);/*  w  ww.j a  va  2 s  .c om*/
    insn.add(new TypeInsnNode(Opcodes.NEW, FRAME_NAME));
    insn.add(new InsnNode(Opcodes.DUP));
    insn.add(makeInt(nLocals));
    insn.add(makeInt(nLocals));
    insn.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/String"));
    for (LocalVariableNode local : locals) {
        int i = local.index;
        String name = names[i];
        insn.add(new InsnNode(Opcodes.DUP));
        insn.add(makeInt(i));
        if (name != null) {
            insn.add(new LdcInsnNode(name));
        } else {
            insn.add(new InsnNode(Opcodes.ACONST_NULL));
        }
        insn.add(new InsnNode(Opcodes.AASTORE));
    }
    insn.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, FRAME_NAME, "<init>", "(I[Ljava/lang/String;)V"));
    return insn;
}

From source file:pl.clareo.coroutines.core.ClassTransformer.java

License:Apache License

@SuppressWarnings("unchecked")
void transform() {
    for (MethodNode coroutine : coroutines) {
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generating method for coroutine " + coroutine.name + coroutine.desc);
        }/*from  w  w w  .  j  ava 2s  . c om*/
        String coroutineName = getCoroutineName(coroutine);
        MethodTransformer methodTransformer = new MethodTransformer(coroutine, thisType);
        MethodNode coroutineImpl = methodTransformer.transform(coroutineName, generateDebugCode);
        thisNode.methods.add(coroutineImpl);
        /*
         * generate co iterators and method stubs
         */
        log.finest("Generating CoIterator implementation and method stubs");
        String baseCoIteratorName;
        Map<String, Object> annotation = getCoroutineAnnotationValues(coroutine);
        if (getBoolean(annotation, "threadLocal")) {
            baseCoIteratorName = Type.getInternalName(ThreadLocalCoIterator.class);
        } else {
            baseCoIteratorName = Type.getInternalName(SingleThreadedCoIterator.class);
        }
        String coIteratorClassName = "pl/clareo/coroutines/core/CoIterator" + num;
        ClassNode coIteratorClass = new ClassNode();
        coIteratorClass.version = Opcodes.V1_6;
        coIteratorClass.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER;
        coIteratorClass.name = coIteratorClassName;
        coIteratorClass.superName = baseCoIteratorName;
        if (generateDebugCode) {
            /*
             * If debugging code is emitted create field keeping JDK logger
             */
            FieldNode loggerField = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                    "logger", "Ljava/util/logging/Logger;", null, null);
            coIteratorClass.fields.add(loggerField);
            MethodNode clinit = new MethodNode();
            clinit.access = Opcodes.ACC_STATIC;
            clinit.name = "<clinit>";
            clinit.desc = "()V";
            clinit.exceptions = Collections.EMPTY_LIST;
            String loggerName = thisType.getClassName();
            InsnList clinitCode = clinit.instructions;
            clinitCode.add(new LdcInsnNode(loggerName));
            clinitCode.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger",
                    "(Ljava/lang/String;)Ljava/util/logging/Logger;"));
            clinitCode.add(new FieldInsnNode(Opcodes.PUTSTATIC, coIteratorClassName, "logger",
                    "Ljava/util/logging/Logger;"));
            clinitCode.add(new InsnNode(Opcodes.RETURN));
            clinit.maxStack = 1;
            clinit.maxLocals = 0;
            coIteratorClass.methods.add(clinit);
        }
        /*
         * Generate constructor
         */
        MethodNode init = new MethodNode();
        init.access = Opcodes.ACC_PUBLIC;
        init.name = "<init>";
        init.desc = CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR;
        init.exceptions = Collections.EMPTY_LIST;
        InsnList initCode = init.instructions;
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 0));
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        initCode.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, baseCoIteratorName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        initCode.add(new InsnNode(Opcodes.RETURN));
        init.maxStack = 2;
        init.maxLocals = 2;
        coIteratorClass.methods.add(init);
        /*
         * Generate overriden call to coroutine
         */
        MethodNode call = new MethodNode();
        call.access = Opcodes.ACC_PROTECTED;
        call.name = "call";
        call.desc = CALL_METHOD_DESCRIPTOR;
        call.exceptions = Collections.EMPTY_LIST;
        InsnList callCode = call.instructions;
        /*
         * if debug needed generate call details
         */
        if (generateDebugCode) {
            String coroutineId = "Coroutine " + coroutine.name;
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    coroutineId + " call. Caller sent: ", 2));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINEST,
                    coroutineId + " state ", 1));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
        }
        /*
         * push call arguments: this (if not static), frame, input, output
         */
        boolean isStatic = (coroutine.access & Opcodes.ACC_STATIC) != 0;
        if (!isStatic) {
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getThis", "()Ljava/lang/Object;"));
            callCode.add(new TypeInsnNode(Opcodes.CHECKCAST, thisType.getInternalName()));
        }
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        callCode.add(new InsnNode(Opcodes.ACONST_NULL));
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 2));
        callCode.add(new MethodInsnNode(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
                thisType.getInternalName(), coroutineName, COROUTINE_METHOD_DESCRIPTOR));
        // stack: *
        if (!generateDebugCode) {
            callCode.add(new InsnNode(Opcodes.ARETURN));
        } else {
            // save result display suspension point (two more locals
            // needed)
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 3));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLineOfCode", "()I"));
            callCode.add(box_int(Type.INT));
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 4));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    "Coroutine suspended at line ", 4, ". Yielded:", 3));
            callCode.add(new FrameNode(Opcodes.F_APPEND, 2,
                    new Object[] { "java/lang/Object", "java/lang/Integer" }, 0, EMPTY_STACK));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 3));
            callCode.add(new InsnNode(Opcodes.ARETURN));
        }
        coIteratorClass.methods.add(call);
        // if debugging code is emitted it needs space for two
        // additional locals and 5 stack operand
        if (generateDebugCode) {
            call.maxStack = 5;
            call.maxLocals = 5;
        } else {
            if (isStatic) {
                call.maxStack = 3;
            } else {
                call.maxStack = 4;
            }
            call.maxLocals = 3;
        }
        /*
         * CoIterator created - define it in the runtime and verify if
         * needed
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generated class " + coIteratorClassName);
        }
        ClassWriter cw = new ClassWriter(0);
        coIteratorClass.accept(cw);
        byte[] classBytes = cw.toByteArray();
        try {
            CoroutineInstrumentator.dumpClass(coIteratorClassName, classBytes);
        } catch (IOException e) {
            throw new CoroutineGenerationException("Unable to write class " + coIteratorClassName, e);
        }
        /*
         * start generating method - new method is named as the method in
         * user code, it: returns instance of appropriate CoIterator (see
         * above), saves arguments of call
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Instrumenting method " + coroutine.name);
        }
        InsnList code = coroutine.instructions;
        code.clear();
        /*
         * create new Frame
         */
        boolean isDebugFramePossible = generateDebugCode && coroutine.localVariables != null;
        if (isDebugFramePossible) {
            code.add(createDebugFrame(coroutine));
        } else {
            code.add(createFrame(coroutine));
        }
        /*
         * save frame in the first, and locals array in the second local
         * variable
         */
        int argsSize = Type.getArgumentsAndReturnSizes(coroutine.desc) >> 2;
        if (isStatic) {
            argsSize -= 1;
        }
        code.add(new VarInsnNode(Opcodes.ASTORE, argsSize));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLocals", "()[Ljava/lang/Object;"));
        int localsArrayIndex = argsSize + 1;
        code.add(new VarInsnNode(Opcodes.ASTORE, localsArrayIndex));
        /*
         * save all call arguments (along with this if this method is not
         * static) into locals array
         */
        Type[] argsTypes = Type.getArgumentTypes(coroutine.desc);
        if (!isStatic) {
            code.add(saveloc(localsArrayIndex, 0, 0, JAVA_LANG_OBJECT));
            code.add(savelocs(localsArrayIndex, 1, 1, argsTypes));
        } else {
            code.add(savelocs(localsArrayIndex, 0, 0, argsTypes));
        }
        /*
         * create CoIterator instance with saved frame, make initial call to
         * next if needed and return to caller
         */
        code.add(new TypeInsnNode(Opcodes.NEW, coIteratorClassName));
        code.add(new InsnNode(Opcodes.DUP));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, coIteratorClassName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        if (!getBoolean(annotation, "generator", true)) {
            code.add(new InsnNode(Opcodes.DUP));
            code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, coIteratorClassName, "next",
                    "()Ljava/lang/Object;"));
            code.add(new InsnNode(Opcodes.POP));
        }
        code.add(new InsnNode(Opcodes.ARETURN));
        /*
         * end method generation; maxs can be statically determined 3
         * operands on stack (call to frame setLocals and CoIterator
         * constructor) + 1 if any argument is long or double (debug frame
         * needs 7 operands for variable names creation); locals = argsSize
         * + 1 reference to frame + 1 array of locals
         */
        if (isDebugFramePossible) {
            coroutine.maxStack = 7;
        } else {
            boolean isCategory2ArgumentPresent = false;
            for (Type argType : argsTypes) {
                int sort = argType.getSort();
                if (sort == Type.LONG || sort == Type.DOUBLE) {
                    isCategory2ArgumentPresent = true;
                    break;
                }
            }
            coroutine.maxStack = isCategory2ArgumentPresent ? 4 : 3;
        }
        coroutine.maxLocals = localsArrayIndex + 1;
        coroutine.localVariables.clear();
        coroutine.tryCatchBlocks.clear();
        num++;
    }
}

From source file:scouter.agent.asm.ApicallASM.java

License:Apache License

private void capReturn() {
    Type tp = returnType;/*from  w  ww.j a v  a  2  s  .  co  m*/
    if (tp == null || tp.equals(Type.VOID_TYPE)) {
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESUBCALL, END_METHOD, END_SIGNATURE, false);
        return;
    }
    int i = newLocal(tp);
    switch (tp.getSort()) {
    case Type.BOOLEAN:
        mv.visitVarInsn(Opcodes.ISTORE, i);
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;",
                false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.BYTE:
        mv.visitVarInsn(Opcodes.ISTORE, i);
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.CHAR:
        mv.visitVarInsn(Opcodes.ISTORE, i);
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
                false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.SHORT:
        mv.visitVarInsn(Opcodes.ISTORE, i);
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.INT:
        mv.visitVarInsn(Opcodes.ISTORE, i);
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ILOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;",
                false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.LONG:
        mv.visitVarInsn(Opcodes.LSTORE, i);
        mv.visitVarInsn(Opcodes.LLOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.LLOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.FLOAT:
        mv.visitVarInsn(Opcodes.FSTORE, i);
        mv.visitVarInsn(Opcodes.FLOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.FLOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    case Type.DOUBLE:
        mv.visitVarInsn(Opcodes.DSTORE, i);
        mv.visitVarInsn(Opcodes.DLOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.DLOAD, i);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
        break;
    default:
        mv.visitVarInsn(Opcodes.ASTORE, i);
        mv.visitVarInsn(Opcodes.ALOAD, i);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
        mv.visitVarInsn(Opcodes.ALOAD, i);// return
        mv.visitInsn(Opcodes.ACONST_NULL);// throwable
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESUBCALL, END_METHOD, END_SIGNATURE, false);
}

From source file:scouter.agent.asm.ApicallASM.java

License:Apache License

@Override
public void visitMaxs(int maxStack, int maxLocals) {
    Label endFinally = new Label();
    mv.visitTryCatchBlock(startFinally, endFinally, endFinally, null);
    mv.visitLabel(endFinally);//from   w  ww  .j  a va2  s. c o m
    mv.visitInsn(DUP);
    int errIdx = newLocal(Type.getType(Throwable.class));
    mv.visitVarInsn(Opcodes.ASTORE, errIdx);
    mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
    mv.visitInsn(Opcodes.ACONST_NULL);// return
    mv.visitVarInsn(Opcodes.ALOAD, errIdx);// throwable
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESUBCALL, END_METHOD, END_SIGNATURE, false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(maxStack + 8, maxLocals + 2);
}

From source file:scouter.agent.asm.asyncsupport.CallRunnableASM.java

License:Apache License

private void capReturn() {
    Type tp = returnType;// w  w w  .j  a  va 2s  . c om

    if (tp == null || tp.equals(Type.VOID_TYPE)) {
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitVarInsn(Opcodes.ALOAD, statIdx);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, TARGET, END_METHOD, END_METHOD_DESC, false);
        return;
    }

    switch (tp.getSort()) {
    case Type.DOUBLE:
    case Type.LONG:
        mv.visitInsn(Opcodes.DUP2);
        break;
    default:
        mv.visitInsn(Opcodes.DUP);
    }
    // TODO method return test dup and store
    //      int rtnIdx = newLocal(tp);
    //      mv.visitVarInsn(Opcodes.ASTORE, rtnIdx);
    //      mv.visitVarInsn(Opcodes.ALOAD, rtnIdx);

    mv.visitVarInsn(Opcodes.ALOAD, statIdx);// stat
    mv.visitInsn(Opcodes.ACONST_NULL);// throwable
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, TARGET, END_METHOD, END_METHOD_DESC, false);
}