Example usage for org.objectweb.asm Opcodes PUTSTATIC

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

Introduction

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

Prototype

int PUTSTATIC

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

Click Source Link

Usage

From source file:org.evosuite.instrumentation.RemoveFinalMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/* ww  w . j a va  2 s. c  o  m*/
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if ((opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) && owner.equals(className)) {

        if (!finalFields.contains(name)) {
            //System.out.println("Keeping non-final field " + name + " in class "
            //        + owner);
            super.visitFieldInsn(opcode, owner, name, desc);
        } else {
            //System.out.println("Omitting final field " + name + " in class " + owner);
            Type type = Type.getType(desc);
            if (type.getSize() == 1)
                super.visitInsn(Opcodes.POP);
            else if (type.getSize() == 2)
                super.visitInsn(Opcodes.POP2);
            if (opcode == Opcodes.PUTFIELD)
                super.visitInsn(Opcodes.POP);
        }
    } else {
        //if (!owner.equals(className))
        //   System.out.println("Mismatch: " + className + " / " + owner);
        super.visitFieldInsn(opcode, owner, name, desc);
    }
}

From source file:org.evosuite.instrumentation.StaticAccessMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  ww w .  j a  v a2  s  .  c om
public void visitFieldInsn(int opcode, String owner, String name, String desc) {

    if ((opcode == Opcodes.PUTSTATIC || opcode == Opcodes.GETSTATIC)
            && !(className.equals(owner) && methodName.equals("<clinit>"))
            && !(className.equals(owner) && methodName.equals(ClassResetter.STATIC_RESET))) {

        String classNameWithDots = owner.replace('/', '.');
        if (RuntimeInstrumentation.checkIfCanInstrument(classNameWithDots)) {

            String executionTracerClassName = ExecutionTracer.class.getName().replace('.', '/');
            String executionTracerDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE,
                    Type.getType(String.class), Type.getType(String.class));

            super.visitLdcInsn(classNameWithDots);
            super.visitLdcInsn(name);
            if (opcode == Opcodes.PUTSTATIC)
                super.visitMethodInsn(INVOKESTATIC, executionTracerClassName, PASSED_PUT_STATIC,
                        executionTracerDescriptor, false);
            else
                super.visitMethodInsn(INVOKESTATIC, executionTracerClassName, PASSED_GET_STATIC,
                        executionTracerDescriptor, false);
        }
    }
    super.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * This helper function determines whether the boolean on the stack at the
 * current position will be stored in a Boolean variable
 * /*  www .ja v a  2 s  . co  m*/
 * @param position
 * @param mn
 * @return
 */
public boolean isBooleanAssignment(AbstractInsnNode position, MethodNode mn) {
    AbstractInsnNode node = position.getNext();
    logger.info("Checking for ISTORE after boolean");
    boolean done = false;
    while (!done) {

        if (node.getOpcode() == Opcodes.PUTFIELD || node.getOpcode() == Opcodes.PUTSTATIC) {
            // TODO: Check whether field is static
            logger.info("Checking field assignment");
            FieldInsnNode fn = (FieldInsnNode) node;
            if (Type.getType(DescriptorMapping.getInstance().getFieldDesc(fn.owner, fn.name,
                    fn.desc)) == Type.BOOLEAN_TYPE) {
                return true;
            } else {
                return false;
            }
        } else if (node.getOpcode() == Opcodes.ISTORE) {
            logger.info("Found ISTORE after boolean");

            VarInsnNode vn = (VarInsnNode) node;
            // TODO: Check whether variable at this position is a boolean
            if (isBooleanVariable(vn.var, mn)) {
                logger.info("Assigning boolean to variable ");
                return true;
            } else {
                logger.info("Variable is not a bool");
                return false;
            }
        } else if (node.getOpcode() == Opcodes.IRETURN) {
            logger.info("Checking return value of method " + cn.name + "." + mn.name);
            if (DescriptorMapping.getInstance().isTransformedOrBooleanMethod(cn.name, mn.name, mn.desc)) {
                logger.info("Method returns a bool");
                return true;
            } else {
                logger.info("Method does not return a bool");
                return false;
            }
        } else if (node.getOpcode() == Opcodes.BASTORE) {
            // We remove all bytes, so BASTORE is only used for booleans
            AbstractInsnNode start = position.getNext();
            boolean reassignment = false;
            while (start != node) {
                if (node instanceof InsnNode) {
                    reassignment = true;
                }
                start = start.getNext();
            }
            logger.info("Possible assignment to array?");
            if (reassignment)
                return false;
            else
                return true;

        } else if (node instanceof MethodInsnNode) {
            // if it is a boolean parameter of a converted method, then it needs to be converted
            // Problem: How do we know which parameter it represents?
            MethodInsnNode methodNode = (MethodInsnNode) node;
            String desc = DescriptorMapping.getInstance().getMethodDesc(methodNode.owner, methodNode.name,
                    methodNode.desc);
            Type[] types = Type.getArgumentTypes(desc);
            if (types.length > 0 && types[types.length - 1] == Type.BOOLEAN_TYPE) {
                return true;
            } else {
                return false;
            }

        } else if (node.getOpcode() == Opcodes.GOTO || node.getOpcode() == Opcodes.ICONST_0
                || node.getOpcode() == Opcodes.ICONST_1 || node.getOpcode() == -1) {
            logger.info("Continuing search");

            // continue search
        } else if (!(node instanceof LineNumberNode || node instanceof FrameNode)) {
            logger.info("Search ended with opcode " + node.getOpcode());

            return false;
        }
        if (node != mn.instructions.getLast())
            node = node.getNext();
        else
            done = true;
    }

    return false;
}

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

License:Open Source License

@Override
protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) {

    // TODO: If the field owner is not transformed, then convert this to a proper Boolean
    fieldNode.desc = this.booleanTestabilityTransformation.transformFieldDescriptor(fieldNode.owner,
            fieldNode.name, fieldNode.desc);

    // If after transformation the field is still Boolean, we need to convert 
    if (Type.getType(fieldNode.desc).equals(Type.BOOLEAN_TYPE)) {
        if (fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC) {
            MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC,
                    Type.getInternalName(BooleanHelper.class), "intToBoolean",
                    Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));
            TransformationStatistics.transformBackToBooleanField();
            mn.instructions.insertBefore(fieldNode, n);
        } else {//  ww w.  ja va2 s.  co m
            MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC,
                    Type.getInternalName(BooleanHelper.class), "booleanToInt",
                    Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.BOOLEAN_TYPE }));
            mn.instructions.insert(fieldNode, n);
            TransformationStatistics.transformBackToBooleanField();
            return n;
        }
    }
    return fieldNode;
}

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

License:Open Source License

@Override
protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) {
    // This handles the else branch for field assignments
    if (DescriptorMapping.getInstance().isTransformedOrBooleanField(
            this.booleanTestabilityTransformation.className, fieldNode.name, fieldNode.desc)) {
        if (fieldNode.getNext() instanceof FieldInsnNode) {
            FieldInsnNode other = (FieldInsnNode) fieldNode.getNext();
            if (fieldNode.owner.equals(other.owner) && fieldNode.name.equals(other.name)
                    && fieldNode.desc.equals(other.desc)) {
                if (fieldNode.getOpcode() == Opcodes.GETFIELD && other.getOpcode() == Opcodes.PUTFIELD) {
                    this.booleanTestabilityTransformation.insertGetBefore(other, mn.instructions);
                } else if (fieldNode.getOpcode() == Opcodes.GETSTATIC
                        && other.getOpcode() == Opcodes.PUTSTATIC) {
                    this.booleanTestabilityTransformation.insertGetBefore(other, mn.instructions);
                }//from  w  ww  .  jav  a2 s  .c  om
            }
        }
    }
    return fieldNode;
}

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 w  w. jav  a2  s.co 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.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w w  w. j  a  v a2 s  .c o  m
protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) {

    if ((fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC)
            && DescriptorMapping.getInstance().isTransformedOrBooleanField(fieldNode.owner, fieldNode.name,
                    fieldNode.desc)) {

        if (addedInsns.contains(fieldNode))
            return fieldNode;

        // Can only handle cases where the field owner is loaded directly before the field
        // TODO: We could pop the top of the stack and DUP the owner, but would need to take care
        // whether we need to pop one or two words
        if (fieldNode.getOpcode() == Opcodes.PUTFIELD) {
            AbstractInsnNode previous = fieldNode.getPrevious();
            while (previous instanceof LineNumberNode || previous instanceof FrameNode
                    || previous.getOpcode() == Opcodes.ICONST_0 || previous.getOpcode() == Opcodes.ICONST_1)
                previous = previous.getPrevious();
            if (previous.getOpcode() != Opcodes.ALOAD) {
                BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous);
                return fieldNode;
            }
            VarInsnNode varNode = (VarInsnNode) previous;
            if (varNode.var != 0) {
                BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous);
                return fieldNode;
            }
        }
        BooleanTestabilityTransformation.logger.info("Handling PUTFIELD case!");

        // Check if ICONST_0 or ICONST_1 are on the stack
        ControlDependenceGraph cdg = GraphPool.getInstance(this.booleanTestabilityTransformation.classLoader)
                .getCDG(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc);
        int index = mn.instructions.indexOf(fieldNode);
        BooleanTestabilityTransformation.logger.info("Getting bytecode instruction for " + fieldNode.name + "/"
                + ((FieldInsnNode) mn.instructions.get(index)).name);
        InsnList nodes = mn.instructions;
        ListIterator<AbstractInsnNode> it = nodes.iterator();
        while (it.hasNext()) {
            BytecodeInstruction in = new BytecodeInstruction(this.booleanTestabilityTransformation.classLoader,
                    this.booleanTestabilityTransformation.className, mn.name, 0, 0, it.next());
            BooleanTestabilityTransformation.logger.info(in.toString());
        }
        BytecodeInstruction insn = BytecodeInstructionPool
                .getInstance(this.booleanTestabilityTransformation.classLoader)
                .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."),
                        mn.name + mn.desc, index);
        if (insn == null)
            insn = BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader)
                    .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."),
                            mn.name + mn.desc, fieldNode);
        //varNode);
        if (insn == null) {
            // TODO: Find out why
            BooleanTestabilityTransformation.logger.info("ERROR: Could not find node");
            return fieldNode;
        }
        if (insn.getASMNode().getOpcode() != fieldNode.getOpcode()) {
            BooleanTestabilityTransformation.logger.info("Found wrong bytecode instruction at this index!");
            BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader)
                    .getInstruction(this.booleanTestabilityTransformation.className, mn.name + mn.desc,
                            fieldNode);
        }
        if (insn.getBasicBlock() == null) {
            BooleanTestabilityTransformation.logger.info("ERROR: Problematic node found");
            return fieldNode;
        }
        Set<ControlDependency> dependencies = insn.getControlDependencies();
        BooleanTestabilityTransformation.logger.info("Found flag assignment: " + insn + ", checking "
                + dependencies.size() + " control dependencies");

        for (ControlDependency dep : dependencies) {
            if (!addedNodes.contains(dep))
                handleDependency(dep, cdg, mn, fieldNode, insn);
        }
    }
    return fieldNode;
}

From source file:org.evosuite.runtime.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

/**
 * Creates an empty __STATIC_RESET method where no <clinit> was found.
 *///from  w  w  w . ja  v  a2  s .  c  o  m
private void createEmptyStaticReset() {
    logger.info("Creating brand-new static initializer in class " + className);
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC,
            ClassResetter.STATIC_RESET, "()V", null, null);
    mv.visitCode();
    for (StaticField staticField : static_fields) {

        if (!finalFields.contains(staticField.name) && !staticField.name.startsWith("__cobertura")
                && !staticField.name.startsWith("$jacoco") && !staticField.name.startsWith("$VRc") // Old
        // Emma
        ) {

            logger.info("Adding bytecode for initializing field " + staticField.name);

            if (staticField.value != null) {
                mv.visitLdcInsn(staticField.value);
            } else {
                Type type = Type.getType(staticField.desc);
                switch (type.getSort()) {
                case Type.BOOLEAN:
                case Type.BYTE:
                case Type.CHAR:
                case Type.SHORT:
                case Type.INT:
                    mv.visitInsn(Opcodes.ICONST_0);
                    break;
                case Type.FLOAT:
                    mv.visitInsn(Opcodes.FCONST_0);
                    break;
                case Type.LONG:
                    mv.visitInsn(Opcodes.LCONST_0);
                    break;
                case Type.DOUBLE:
                    mv.visitInsn(Opcodes.DCONST_0);
                    break;
                case Type.ARRAY:
                case Type.OBJECT:
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    break;
                }
            }
            mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc);

        }
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:org.evosuite.runtime.instrumentation.CreateClassResetMethodAdapter.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();
    for (StaticField staticField : staticFields) {

        if (!finalFields.contains(staticField.name) && !staticField.name.startsWith("__cobertura")
                && !staticField.name.startsWith("$jacoco") && !staticField.name.startsWith("$VRc") // Old Emma
        ) {/*from w  w w.  j  a va  2 s.c o  m*/

            if (staticField.value != null) {
                mv.visitLdcInsn(staticField.value);
            } else {
                Type type = Type.getType(staticField.desc);
                switch (type.getSort()) {
                case Type.BOOLEAN:
                case Type.BYTE:
                case Type.CHAR:
                case Type.SHORT:
                case Type.INT:
                    mv.visitInsn(Opcodes.ICONST_0);
                    break;
                case Type.FLOAT:
                    mv.visitInsn(Opcodes.FCONST_0);
                    break;
                case Type.LONG:
                    mv.visitInsn(Opcodes.LCONST_0);
                    break;
                case Type.DOUBLE:
                    mv.visitInsn(Opcodes.DCONST_0);
                    break;
                case Type.ARRAY:
                case Type.OBJECT:
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    break;
                }
            }
            mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc);
        }
    }

}

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

License:Open Source License

@SuppressWarnings("unchecked")
public Set<MethodIdentifier> collectMethods() {

    Set<MethodIdentifier> methods = new LinkedHashSet<MethodIdentifier>();

    for (String calledClassName : getStaticFields.keySet()) {
        ClassNode classNode = DependencyAnalysis.getClassNode(calledClassName);
        List<MethodNode> classMethods = classNode.methods;
        for (MethodNode mn : classMethods) {
            if (mn.name.equals(CLINIT))
                continue;

            InsnList instructions = mn.instructions;
            Iterator<AbstractInsnNode> it = instructions.iterator();
            while (it.hasNext()) {
                AbstractInsnNode insn = it.next();
                if (insn instanceof FieldInsnNode) {
                    FieldInsnNode fieldInsn = (FieldInsnNode) insn;
                    if (fieldInsn.getOpcode() != Opcodes.PUTSTATIC) {
                        continue;
                    }/*from w w w. ja  v a 2s . co m*/
                    String calleeClassName = fieldInsn.owner.replaceAll("/", ".");
                    String calleeFieldName = fieldInsn.name;

                    if (contains(getStaticFields, calleeClassName, calleeFieldName)) {

                        MethodIdentifier methodIdentifier = new MethodIdentifier(calledClassName, mn.name,
                                mn.desc);
                        methods.add(methodIdentifier);

                    }
                }
            }

        }

    }
    return methods;
}