Example usage for org.objectweb.asm Opcodes GETSTATIC

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

Introduction

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

Prototype

int GETSTATIC

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

Click Source Link

Usage

From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn,
        FieldInsnNode varNode, BytecodeInstruction parentLevel) {

    if (addedNodes.contains(dependency))
        return;//from w ww.j av a 2 s.  c  o m

    // Get the basic blocks reachable if the dependency would evaluate different
    Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
    addedNodes.add(dependency);

    Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
    //if (dependencies.size() == 1) {
    //   ControlDependency dep = dependencies.iterator().next();
    for (ControlDependency dep : dependencies) {
        if (!addedNodes.contains(dep) && dep != dependency)
            handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
    }

    // TODO: Need to check that there is an assignment in every alternative path through CDG

    boolean hasAssignment = false;
    for (BasicBlock block : blocks) {
        // If this block also assigns a value to the same variable
        for (BytecodeInstruction instruction : block) {
            if (instruction.getASMNode().getOpcode() == Opcodes.PUTFIELD
                    || instruction.getASMNode().getOpcode() == Opcodes.PUTSTATIC) {
                FieldInsnNode otherFieldNode = (FieldInsnNode) instruction.getASMNode();
                FieldInsnNode thisFieldNode = varNode;
                if (otherFieldNode.owner.equals(thisFieldNode.owner)
                        && otherFieldNode.name.equals(thisFieldNode.name)) {
                    hasAssignment = true;
                    break;
                }
            }
        }
        if (hasAssignment) {
            break;
        }
    }

    // The Flag assignment is is the dependency evaluates to the given value
    // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true)
    // or before the jump target (if the value is false)

    if (!hasAssignment) {
        if (dependency.getBranch().getInstruction().isSwitch()) {
            BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
            return;
        }

        TransformationStatistics.transformedImplicitElse();

        JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
        FieldInsnNode newLoad = new FieldInsnNode(
                varNode.getOpcode() == Opcodes.PUTSTATIC ? Opcodes.GETSTATIC : Opcodes.GETFIELD, varNode.owner,
                varNode.name, varNode.desc);
        FieldInsnNode newStore = new FieldInsnNode(varNode.getOpcode(), varNode.owner, varNode.name,
                varNode.desc);
        AbstractInsnNode newOwnerLoad1 = null;
        AbstractInsnNode newOwnerLoad2 = null;
        if (varNode.getOpcode() == Opcodes.PUTFIELD) {
            // Need to copy the bloody owner
            // Check for VarInsn
            //if (varNode.getPrevious().getOpcode() == Opcodes.ALOAD) {
            newOwnerLoad1 = new VarInsnNode(Opcodes.ALOAD, 0);
            newOwnerLoad2 = new VarInsnNode(Opcodes.ALOAD, 0);
            /*
            } else {
            // Else use helper function
            // Insert DUP and
            logger.info("Wargh");
            System.exit(0);
            fieldOwnerId++;
            InsnNode dupNode = new InsnNode(Opcodes.DUP);
            mn.instructions.insertBefore(varNode, new LdcInsnNode(
            fieldOwnerId));
            mn.instructions.insertBefore(varNode, dupNode);
            registerInstruction(mn, varNode, dupNode);
            MethodInsnNode storeOwner = new MethodInsnNode(
            Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "setFieldOwner", "(ILjava/lang/Object;)V");
            mn.instructions.insertBefore(varNode, storeOwner);
            registerInstruction(mn, varNode, storeOwner);
            newOwnerLoad1 = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "getFieldOwner", "(I)Ljava/lang/Object;");
            newOwnerLoad2 = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "getFieldOwner", "(I)Ljava/lang/Object;");
            }
            */
        }

        if (dependency.getBranchExpressionValue()) {
            BooleanTestabilityTransformation.logger.info("Inserting after if");
            // Insert directly after if
            mn.instructions.insert(jumpNode, newStore);
            mn.instructions.insert(jumpNode, newLoad);
            if (newOwnerLoad1 != null) {
                mn.instructions.insert(jumpNode, newOwnerLoad1);
                registerInstruction(mn, varNode, newOwnerLoad1);
            }
            if (newOwnerLoad2 != null) {
                mn.instructions.insert(jumpNode, newOwnerLoad2);
                registerInstruction(mn, varNode, newOwnerLoad2);
            }
            registerInstruction(mn, varNode, newStore);
            registerInstruction(mn, varNode, newLoad);

        } else {
            BooleanTestabilityTransformation.logger.info("Inserting as jump target");

            // Insert as jump target
            LabelNode target = jumpNode.label;
            LabelNode newTarget = new LabelNode(new Label());

            registerInstruction(mn, target, newStore);
            registerInstruction(mn, target, newLoad);

            InsnList assignment = new InsnList();
            assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
            assignment.add(newTarget);
            if (newOwnerLoad1 != null) {
                assignment.add(newOwnerLoad1);
                registerInstruction(mn, target, newOwnerLoad1);
            }
            if (newOwnerLoad2 != null) {
                assignment.add(newOwnerLoad2);
                registerInstruction(mn, target, newOwnerLoad2);
            }
            assignment.add(newLoad);
            assignment.add(newStore);
            jumpNode.label = newTarget;

            mn.instructions.insertBefore(target, assignment);
        }
        addedInsns.add(newStore);
        addedInsns.add(newLoad);
    }

}

From source file:org.evosuite.setup.GetStaticGraphGenerator.java

License:Open Source License

/**
 * Descend into a static field read//ww  w .  j av a2  s .c  o m
 * 
 */
private static void handleFieldInsnNode(GetStaticGraph staticUsageTree, ClassNode cn, MethodNode mn,
        FieldInsnNode insn, int depth) {

    // Skip field instructions that are not reads to static fields
    if (insn.getOpcode() != Opcodes.GETSTATIC) {
        return;
    }

    // Only collect relations for instrumentable classes
    String calleeClassName = insn.owner.replaceAll("/", ".");
    if (BytecodeInstrumentation.checkIfCanInstrument(calleeClassName)) {
        logger.debug("Handling field read: " + insn.name);
        if (!staticUsageTree.hasStaticFieldRead(cn.name, mn.name + mn.desc, insn.owner, insn.name)) {

            handleClassInitializer(staticUsageTree, cn, mn, insn.owner, depth);

            // Add static read from mn to insn to static usage graph
            staticUsageTree.addStaticFieldRead(cn.name, mn.name + mn.desc, insn.owner, insn.name);

            handle(staticUsageTree, insn.owner, insn.name + insn.desc, depth);
        }
    }

}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*from   ww w  .j av a2s.c  o  m*/
    FieldInsnNode fieldIns = null;

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();

                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        // -> Call
                        // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class),
                        "notifyReadAccess",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void addCaptureEnableStatement(final String className, final MethodNode mn, final InsnList il,
        final int returnValueVar) {
    il.add(new LdcInsnNode(this.captureId));

    if (TransformerUtil.isStatic(mn.access)) {
        // static method

        il.add(new LdcInsnNode(className));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class),
                "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
    } else {//from ww  w  .jav a 2s  . c  o  m
        // non-static method

        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
    }

    final Type returnType = Type.getReturnType(mn.desc);
    if (returnType.equals(Type.VOID_TYPE)) {
        // load return value for VOID methods
        il.add(new FieldInsnNode(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(CaptureLog.class),
                "RETURN_TYPE_VOID", Type.getDescriptor(Object.class)));
    } else {
        // load return value as object
        il.add(new VarInsnNode(Opcodes.ALOAD, returnValueVar));
    }

    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "enable",
            "(ILjava/lang/Object;Ljava/lang/Object;)V"));
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompilerForPrecisionBigDecimals_Base.java

License:Open Source License

protected final void compile_mathContext() {
    this.bigCompiler.buildStaticContext();
    mv().visitFieldInsn(Opcodes.GETSTATIC, typeCompiler().rootCompiler().classInternalName(),
            RUNTIME_CONTEXT_NAME, RUNTIME_CONTEXT_DESCRIPTOR);
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompilerForScaledLongs_Base.java

License:Open Source License

protected final void compile_context() {
    this.longCompiler.buildStaticContext();
    mv().visitFieldInsn(Opcodes.GETSTATIC, typeCompiler().rootCompiler().classInternalName(),
            RUNTIME_CONTEXT_NAME, RUNTIME_CONTEXT_DESCRIPTOR);
}

From source file:org.formulacompiler.compiler.internal.bytecode.TypeCompilerForBigDecimals.java

License:Open Source License

private final void compileStaticConstant(MethodVisitor _mv, String _value) {
    final String constName = defineOrReuseStaticConstant(_value);
    _mv.visitFieldInsn(Opcodes.GETSTATIC, rootCompiler().classInternalName(), constName, B);
}

From source file:org.glassfish.pfl.tf.tools.enhancer.StaticInitVisitor.java

License:Open Source License

private void generateTraceMsg(MethodVisitor mv, String msg, int num) {
    if (ENABLED && util.getDebug()) {
        final Label start = new Label();
        mv.visitLabel(start);/*from ww w .  jav a2 s. c o m*/
        mv.visitLineNumber(num, start);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn(msg);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    }
}

From source file:org.glassfish.pfl.tf.tools.enhancer.StaticInitVisitor.java

License:Open Source License

@Override
public void visitCode() {
    if (SHORT_FORM) {
        super.visitCode();
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);
    } else {// w ww .  ja v  a 2  s .  c om
        int line = 1;
        util.info(2, "StaticInitVisitor.visitCode");
        super.visitCode();

        Label start = new Label();
        Label end = new Label();

        mv.visitLabel(start);

        LocalVariableNode thisClass = defineLocal(mv, "thisClass", Class.class, start, end);
        LocalVariableNode mnameList = defineLocal(mv, "mnameList", List.class, start, end);
        LocalVariableNode holderMap = defineLocal(mv, "holderMap", Map.class, start, end);

        generateTraceMsg(mv, "initialize the holders", line++);
        for (String str : ecd.getAnnotationToHolderName().values()) {
            generateTraceMsg(mv, "Generating to initialize holder " + str, line++);
            util.info(2, "Generating code to initialize holder " + str);
            util.newWithSimpleConstructor(mv, SynchronizedHolder.class);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, ecd.getClassName(), str,
                    Type.getDescriptor(SynchronizedHolder.class));
        }

        generateTraceMsg(mv, "Store the Class of this class", line++);
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        mv.visitVarInsn(Opcodes.ASTORE, thisClass.index);

        generateTraceMsg(mv, "Create list of method names", line++);
        util.newWithSimpleConstructor(mv, ArrayList.class);
        mv.visitVarInsn(Opcodes.ASTORE, mnameList.index);

        for (String str : ecd.getMethodNames()) {
            util.info(2, "Generating code to add " + str + " to methodNames");
            mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
            mv.visitLdcInsn(str);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "create map from MM annotation class to Holder and init", line++);
        util.newWithSimpleConstructor(mv, HashMap.class);
        mv.visitVarInsn(Opcodes.ASTORE, holderMap.index);

        for (Map.Entry<String, String> entry : ecd.getAnnotationToHolderName().entrySet()) {

            util.info(2,
                    "Generating code to put " + entry.getKey() + "=>" + entry.getValue() + " into holderMap");

            mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

            Type annoType = Type.getType("L" + entry.getKey() + ";");
            mv.visitLdcInsn(annoType);

            mv.visitFieldInsn(Opcodes.GETSTATIC, ecd.getClassName(), entry.getValue(),
                    Type.getDescriptor(SynchronizedHolder.class));

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "register with MethodMonitorRegistry", line++);
        util.info(2, "Generating code call MethodMonitorRegistry.registerClass");
        mv.visitVarInsn(Opcodes.ALOAD, thisClass.index);
        mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
        mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;Ljava/util/List;Ljava/util/Map;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);

        mv.visitLabel(end);

        thisClass.accept(mv);
        mnameList.accept(mv);
        holderMap.accept(mv);
    }
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(WriteStatement node) throws ASTVisitorException {

    Type type = ASTUtils.getSafeType(node.getExpression());
    LocalIndexPool lip = ASTUtils.getSafeLocalIndexPool(node);
    int li = lip.getLocalIndex(type);
    mn.instructions//from   w ww. j  a  v a2  s. c  o m
            .add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    System.out.println(
            "***************************************************************-------------------------------------------------------------- "
                    + li);
    mn.instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 0));
    node.getExpression().accept(this);
    mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "print",
            Type.getMethodType(Type.VOID_TYPE, type).toString(), false));
    lip.freeLocalIndex(li, type);
}