Search in sources :

Example 31 with ClassInfo

use of com.jopdesign.common.ClassInfo in project jop by jop-devel.

the class Config method parseMethodList.

/**
     * @param methodNames comma separated list of method names, either fully qualified with or without descriptor, or
     *        method names of methods in the main class.
     * @return the set of methods represented by these names
     * @throws BadConfigurationException if a name is not resolvable
     */
public static List<MethodInfo> parseMethodList(String methodNames) throws BadConfigurationException {
    List<String> names = splitStringList(methodNames);
    List<MethodInfo> methods = new ArrayList<MethodInfo>(names.size());
    for (String name : names) {
        MemberID id = MemberID.parse(name);
        if (!id.hasClassName()) {
            ClassInfo main = AppInfo.getSingleton().getMainMethod().getClassInfo();
            Set<MethodInfo> m = main.getMethodInfos(id);
            if (m.isEmpty()) {
                throw new BadConfigurationException("Cannot find method '" + name + "' in main class " + main);
            }
            methods.addAll(m);
        } else {
            try {
                Collection<MethodInfo> infos = AppInfo.getSingleton().getMethodInfos(id);
                if (infos.isEmpty()) {
                    throw new BadConfigurationException("Cannot find methods for " + name);
                }
                methods.addAll(infos);
            } catch (MethodNotFoundException e) {
                throw new BadConfigurationException("Cannot find class for " + name, e);
            }
        }
    }
    return methods;
}
Also used : MemberID(com.jopdesign.common.type.MemberID) ArrayList(java.util.ArrayList) MethodInfo(com.jopdesign.common.MethodInfo) MethodNotFoundException(com.jopdesign.common.misc.MethodNotFoundException) ClassInfo(com.jopdesign.common.ClassInfo)

Example 32 with ClassInfo

use of com.jopdesign.common.ClassInfo in project jop by jop-devel.

the class DFATool method load.

/**
     * Load the methods into the internal DFA structures and initialize the DFA tool for a new analysis.
     * You need to call this method before starting the first analysis and before starting an analysis after
     * the code has been modified.
     */
public void load() {
    // First clear everything ..
    statements.clear();
    flow.clear();
    receivers = null;
    prologue = createPrologue();
    // Now we need to process all classes (for DFA's internal flow graph)
    for (ClassInfo cls : appInfo.getClassInfos()) {
        for (MethodInfo mi : cls.getMethods()) {
            if (mi.hasCode()) {
                loadMethod(mi);
            }
        }
    }
    if (cacheDir != null && !appInfo.updateCheckSum(prologue)) {
        cacheDir = null;
    }
}
Also used : MethodInfo(com.jopdesign.common.MethodInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 33 with ClassInfo

use of com.jopdesign.common.ClassInfo in project jop by jop-devel.

the class CallStringReceiverTypes method doInvokeVirtual.

private void doInvokeVirtual(String receiver, MethodInfo method, InstructionHandle stmt, Context context, ContextMap<CallString, Set<TypeMapping>> input, Interpreter<CallString, Set<TypeMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> state, ContextMap<CallString, Set<TypeMapping>> result) {
    String methodImplName = method.getClassName() + "." + method.getMethodSignature();
    recordReceiver(stmt, context, methodImplName);
    //		LineNumberTable lines = p.getMethods().get(context.method).getLineNumberTable(context.constPool);
    //		int sourceLine = lines.getSourceLine(stmt.getPosition());
    //		String invokeId = context.method+"\t"+":"+sourceLine+"."+stmt.getPosition();
    // set up new context
    int varPtr = context.stackPtr - MethodHelper.getArgSize(method);
    Context c = new Context(context);
    c.stackPtr = method.getCode().getMaxLocals();
    if (method.isSynchronized()) {
        c.syncLevel = context.syncLevel + 1;
    }
    c.setMethodInfo(method);
    c.callString = c.callString.push(context.getMethodInfo(), stmt, callStringLength);
    boolean threaded = false;
    DFATool p = interpreter.getDFATool();
    if (p.getAppInfo().getClassInfo(receiver).isSubclassOf(p.getAppInfo().getClassInfo("joprt.RtThread")) && "run()V".equals(method.getMethodSignature())) {
        c.createThread();
        threaded = true;
    }
    // carry only minimal information with call
    Set<TypeMapping> in = input.get(context.callString);
    Set<TypeMapping> out = new LinkedHashSet<TypeMapping>();
    ContextMap<CallString, Set<TypeMapping>> tmpresult = new ContextMap<CallString, Set<TypeMapping>>(c, new LinkedHashMap<CallString, Set<TypeMapping>>());
    tmpresult.put(c.callString, out);
    for (TypeMapping m : in) {
        if (m.stackLoc < 0) {
            out.add(m);
        }
        if (m.stackLoc > varPtr) {
            out.add(new TypeMapping(m.stackLoc - varPtr, m.type));
        }
        if (m.stackLoc == varPtr) {
            // add "this"
            ClassInfo staticClass = p.getAppInfo().getClassInfo(receiver);
            ClassInfo dynamicClass = null;
            try {
                dynamicClass = p.getAppInfo().getClassInfo(m.type.split("@")[0]);
            } catch (MissingClassError ex) {
                logger.error("doInvokeVirtual - trying to improve type of " + staticClass + ": " + ex);
            // TODO: maybe we should throw an exception/error instead?
            }
            if (dynamicClass != null && dynamicClass.isSubclassOf(staticClass)) {
                out.add(new TypeMapping(0, m.type));
            }
        }
    }
    InstructionHandle entry = p.getEntryHandle(method);
    state.put(entry, join(state.get(entry), tmpresult));
    // interpret method
    Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> r = interpreter.interpret(c, entry, state, false);
    // pull out relevant information from call
    InstructionHandle exit = p.getExitHandle(method);
    if (r.get(exit) != null) {
        Set<TypeMapping> returned = r.get(exit).get(c.callString);
        if (returned != null) {
            filterReturnSet(returned, result.get(context.callString), varPtr);
        }
    }
    // update all threads
    if (threaded) {
        threads.put(methodImplName, new ContextMap<CallString, Set<TypeMapping>>(c, result));
        updateThreads(result, interpreter, state);
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) LinkedHashSet(java.util.LinkedHashSet) DFATool(com.jopdesign.dfa.DFATool) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CallString(com.jopdesign.common.code.CallString) ContextMap(com.jopdesign.dfa.framework.ContextMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CallString(com.jopdesign.common.code.CallString) ClassInfo(com.jopdesign.common.ClassInfo) MissingClassError(com.jopdesign.common.misc.MissingClassError)

Example 34 with ClassInfo

use of com.jopdesign.common.ClassInfo in project jop by jop-devel.

the class ConstantPoolReferenceFinder method findPoolReferences.

private static Set<Integer> findPoolReferences(FieldInfo fieldInfo, Field field) {
    ClassInfo classInfo = fieldInfo.getClassInfo();
    // we do not need to handle invoke sites here specially, since we do not visit code here
    IdFinderVisitor visitor = new IdFinderVisitor(classInfo);
    visitor.visitConstantUtf8(field.getNameIndex());
    visitor.visitConstantUtf8(field.getSignatureIndex());
    DescendingClassTraverser traverser = new DescendingClassTraverser(visitor);
    traverser.visitAttributes(fieldInfo, field.getAttributes());
    return visitor.getIds();
}
Also used : DescendingClassTraverser(com.jopdesign.common.graphutils.DescendingClassTraverser) ClassInfo(com.jopdesign.common.ClassInfo)

Example 35 with ClassInfo

use of com.jopdesign.common.ClassInfo in project jop by jop-devel.

the class SourceLineStorage method storeSourceInfos.

/**
     * Store all source file and -line annotations of all classes to the storage file.
     */
public void storeSourceInfos() {
    PrintWriter writer;
    try {
        writer = new PrintWriter(new BufferedWriter(new FileWriter(storage, false)));
    } catch (IOException e) {
        logger.error("Error opening file " + storage + " for writing, not writing source line infos!", e);
        return;
    }
    for (ClassInfo cls : AppInfo.getSingleton().getClassInfos()) {
        for (MethodInfo method : cls.getMethods()) {
            if (!method.hasCode())
                continue;
            writeSourceInfos(method.getCode(), writer);
        }
    }
    writer.close();
}
Also used : FileWriter(java.io.FileWriter) MethodInfo(com.jopdesign.common.MethodInfo) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) ClassInfo(com.jopdesign.common.ClassInfo)

Aggregations

ClassInfo (com.jopdesign.common.ClassInfo)49 MethodInfo (com.jopdesign.common.MethodInfo)19 DefaultEdge (org.jgrapht.graph.DefaultEdge)8 AppInfo (com.jopdesign.common.AppInfo)7 HashMap (java.util.HashMap)7 InstructionHandle (org.apache.bcel.generic.InstructionHandle)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 IOException (java.io.IOException)5 LinkedList (java.util.LinkedList)5 FieldInfo (com.jopdesign.common.FieldInfo)4 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)4 MemberID (com.jopdesign.common.type.MemberID)4 BitSet (java.util.BitSet)3 AppSetup (com.jopdesign.common.AppSetup)2 MethodCode (com.jopdesign.common.MethodCode)2 BasicBlock (com.jopdesign.common.code.BasicBlock)2 CallString (com.jopdesign.common.code.CallString)2 CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)2 ExecutionContext (com.jopdesign.common.code.ExecutionContext)2