Search in sources :

Example 26 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.

the class PutStaticMethodCollector method collectMethods.

@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;
                    }
                    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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 27 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.

the class CallGraphGenerator method handleMethodNode.

/**
 * Add all possible calls for a given method
 *
 * @param callGraph
 * @param mn
 */
@SuppressWarnings("unchecked")
private static void handleMethodNode(CallGraph callGraph, ClassNode cn, MethodNode mn, int depth) {
    handlePublicMethodNode(callGraph, cn, mn);
    // TODO: Integrate this properly - it is currently an unexpected side-effect
    if (!ExceptionTransformationClassAdapter.methodExceptionMap.containsKey(cn.name))
        ExceptionTransformationClassAdapter.methodExceptionMap.put(cn.name, new LinkedHashMap<>());
    String methodNameDesc = mn.name + mn.desc;
    Set<Type> exceptionTypes = new LinkedHashSet<>();
    if (mn.exceptions != null) {
        for (String exceptionName : ((List<String>) mn.exceptions)) {
            exceptionTypes.add(Type.getType(exceptionName));
        }
    }
    ExceptionTransformationClassAdapter.methodExceptionMap.get(cn.name).put(methodNameDesc, exceptionTypes);
    InsnList instructions = mn.instructions;
    Iterator<AbstractInsnNode> iterator = instructions.iterator();
    // TODO: This really shouldn't be here but in its own class
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn instanceof MethodInsnNode) {
            handleMethodInsnNode(callGraph, cn, mn, (MethodInsnNode) insn, depth + 1);
        }
    }
}
Also used : Type(org.objectweb.asm.Type) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 28 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.

the class Instrumenter method transformWrapperCalls.

@SuppressWarnings("unchecked")
private void transformWrapperCalls(MethodNode mn) {
    Iterator<AbstractInsnNode> iterator = mn.instructions.iterator();
    List<Class<?>> wrapperClasses = getWrapperClasses();
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn instanceof MethodInsnNode) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) insn;
            if (methodInsnNode.name.equals("<init>")) {
                String ownerName = methodInsnNode.owner.replace('/', '.');
                for (Class<?> wrapperClass : wrapperClasses) {
                    if (wrapperClass.getName().equals(ownerName)) {
                        logger.debug("Replacing call " + methodInsnNode.name);
                        methodInsnNode.owner = "org/evosuite/testcarver/wrapper/" + methodInsnNode.owner;
                        break;
                    }
                }
            } else {
                String ownerName = methodInsnNode.owner.replace('/', '.');
                for (Class<?> wrapperClass : wrapperClasses) {
                    if (wrapperClass.getName().equals(ownerName)) {
                        if (methodInsnNode.getOpcode() == Opcodes.INVOKESTATIC) {
                            logger.debug("Replacing call " + methodInsnNode.name);
                            methodInsnNode.owner = PackageInfo.getEvoSuitePackageWithSlash() + "/testcarver/wrapper/" + methodInsnNode.owner;
                        }
                        Type[] parameterTypes = Type.getArgumentTypes(methodInsnNode.desc);
                        try {
                            Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
                            int pos = 0;
                            for (Type parameter : parameterTypes) {
                                switch(parameter.getSort()) {
                                    case Type.OBJECT:
                                        parameterClasses[pos++] = Class.forName(parameter.getClassName());
                                        break;
                                    case Type.BOOLEAN:
                                        parameterClasses[pos++] = boolean.class;
                                        break;
                                    case Type.BYTE:
                                        parameterClasses[pos++] = byte.class;
                                        break;
                                    case Type.CHAR:
                                        parameterClasses[pos++] = char.class;
                                        break;
                                    case Type.DOUBLE:
                                        parameterClasses[pos++] = double.class;
                                        break;
                                    case Type.FLOAT:
                                        parameterClasses[pos++] = float.class;
                                        break;
                                    case Type.INT:
                                        parameterClasses[pos++] = int.class;
                                        break;
                                    case Type.LONG:
                                        parameterClasses[pos++] = long.class;
                                        break;
                                    case Type.SHORT:
                                        parameterClasses[pos++] = short.class;
                                        break;
                                }
                            }
                            Method method = wrapperClass.getMethod(methodInsnNode.name, parameterClasses);
                            if (Modifier.isFinal(method.getModifiers())) {
                                if (methodInsnNode.getOpcode() != Opcodes.INVOKESTATIC) {
                                    methodInsnNode.setOpcode(Opcodes.INVOKESTATIC);
                                    Type[] args = Type.getArgumentTypes(methodInsnNode.desc);
                                    Type returnType = Type.getReturnType(methodInsnNode.desc);
                                    Type[] newargs = new Type[args.length + 1];
                                    newargs[0] = Type.getObjectType(methodInsnNode.owner);
                                    for (int i = 0; i < args.length; i++) newargs[i + 1] = args[i];
                                    methodInsnNode.desc = Type.getMethodDescriptor(returnType, newargs);
                                    methodInsnNode.owner = PackageInfo.getEvoSuitePackageWithSlash() + "/testcarver/wrapper/" + methodInsnNode.owner;
                                } else {
                                    methodInsnNode.name += "_final";
                                }
                                logger.debug("Method is final: " + methodInsnNode.owner + "." + methodInsnNode.name);
                            } else {
                                logger.debug("Method is not final: " + methodInsnNode.owner + "." + methodInsnNode.name);
                            }
                        } catch (Exception e) {
                            logger.warn("Error while instrumenting: " + e);
                        }
                        break;
                    }
                }
            // } else if(methodInsnNode.name.equals("getTime")) {
            // if(methodInsnNode.owner.equals("java/util/Calendar")) {
            // logger.debug("Replacing call "+methodInsnNode.name);
            // methodInsnNode.owner = "org/evosuite/testcarver/wrapper/java/util/Calendar";
            // methodInsnNode.name = "getTime";
            // methodInsnNode.desc = "(Ljava/util/Calendar;)Ljava/util/Date;";
            // methodInsnNode.setOpcode(Opcodes.INVOKESTATIC);
            // }
            // }
            }
        } else if (insn.getOpcode() == Opcodes.NEW || insn.getOpcode() == Opcodes.CHECKCAST) {
            TypeInsnNode typeInsnNode = (TypeInsnNode) insn;
            Type generatedType = Type.getType(typeInsnNode.desc);
            String name = generatedType.getInternalName().replace('/', '.');
            logger.debug("Checking for replacement of " + name);
            for (Class<?> wrapperClass : wrapperClasses) {
                if (wrapperClass.getName().equals(name)) {
                    logger.debug("Replacing new " + name);
                    typeInsnNode.desc = PackageInfo.getEvoSuitePackageWithSlash() + "/testcarver/wrapper/" + generatedType.getInternalName();
                    break;
                }
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) Type(org.objectweb.asm.Type) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode)

Example 29 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.

the class Instrumenter method addFieldRegistryRegisterCall.

@SuppressWarnings("unchecked")
private void addFieldRegistryRegisterCall(final MethodNode methodNode) {
    AbstractInsnNode ins = null;
    ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();
    // number of invokespecial calls before actual constructor call
    int numInvokeSpecials = 0;
    while (iter.hasNext()) {
        ins = iter.next();
        if (ins instanceof MethodInsnNode) {
            MethodInsnNode mins = (MethodInsnNode) ins;
            if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                if (mins.name.startsWith("<init>")) {
                    if (numInvokeSpecials == 0) {
                        break;
                    } else {
                        numInvokeSpecials--;
                    }
                }
            }
        } else if (ins instanceof TypeInsnNode) {
            TypeInsnNode typeIns = (TypeInsnNode) ins;
            if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                numInvokeSpecials++;
            }
        }
    }
    final InsnList instructions = new InsnList();
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class), "register", "(Ljava/lang/Object;)V"));
    methodNode.instructions.insert(ins, instructions);
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 30 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.

the class ImplicitElseTransformer method isDefinedBefore.

@SuppressWarnings("unchecked")
private boolean isDefinedBefore(MethodNode mn, VarInsnNode var, AbstractInsnNode position) {
    // TODO: Iterate over local variables and check if local is defined here
    List<LocalVariableNode> localVar = mn.localVariables;
    if (localVar.isEmpty()) {
        // If we have no debug information, try to guess
        AbstractInsnNode pos = position.getPrevious();
        while (pos != mn.instructions.getFirst()) {
            if (pos instanceof VarInsnNode) {
                VarInsnNode vn = (VarInsnNode) pos;
                if (var.var == vn.var) {
                    return true;
                }
            }
            pos = pos.getPrevious();
        }
    } else {
        int current = mn.instructions.indexOf(position);
        for (LocalVariableNode local : localVar) {
            if (local.index == var.var) {
                int start = mn.instructions.indexOf(local.start);
                int end = mn.instructions.indexOf(local.end);
                if (current >= start && current <= end)
                    return true;
            }
        }
    }
    return false;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11