Search in sources :

Example 11 with PsiCFGClass

use of com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass in project android by JetBrains.

the class CFGUtil method outputCFGDotFile.

public static void outputCFGDotFile(MethodGraph graph) {
    String path = PathManager.getLogPath();
    //PsiMethod psiMethod = (PsiMethod) graph.getPsiCFGMethod().getPsiRef();
    String methodName = graph.getPsiCFGMethod().getName();
    String className = "dummyClass";
    //PsiElement parentElement = psiMethod.getParent();
    PsiCFGClass cfgClass = graph.getPsiCFGMethod().getDeclaringClass();
    //if (parentElement instanceof PsiClass) {
    //  PsiClass mClass = graph.getPsiCFGMethod().getDeclaringClass().getPsiClass();
    //  className = mClass.getQualifiedName().replace("." , "-");
    //}
    className = cfgClass.getQualifiedClassName().replace(".", "-");
    String fileName = className + "-" + methodName + ".dot";
    BufferedWriter bw = null;
    try {
        File dotFile = new File(path, fileName);
        if (dotFile.exists()) {
            dotFile.delete();
        }
        dotFile.createNewFile();
        PsiCFGDebugUtil.LOG.info("Log CFG to file: " + dotFile.getAbsolutePath());
        FileWriter fw = new FileWriter(dotFile);
        bw = new BufferedWriter(fw);
        Map<GraphNode, Integer> allNodes = getAllNodeFromGraph(graph);
        //Output digraph G
        bw.write("digraph G {\n");
        for (GraphNode node : allNodes.keySet()) {
            String label = getLabelFromGraphNode(node);
            String line = String.format("n%d[label=\"%s\"];\n", allNodes.get(node), label);
            bw.write(line);
        }
        bw.write("\n");
        bfsOutputEdges(bw, graph.getEntryNode(), allNodes);
        bw.write("}");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bw != null) {
                bw.close();
            }
        } catch (Exception ex) {
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 12 with PsiCFGClass

use of com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass in project android by JetBrains.

the class CHAUtil method dfsSetConeBits.

private void dfsSetConeBits(BitSet bitSet, PsiCFGClass clazz) {
    if (!mClassIndexMap.containsKey(clazz)) {
        throw new RuntimeException("class is not found in IndexMap: " + clazz.getQualifiedClassName());
    }
    int index = mClassIndexMap.get(clazz);
    bitSet.set(index);
    Set<PsiCFGClass> subClassSet = clazz.getSubClassSet();
    for (PsiCFGClass subClazz : subClassSet) {
        dfsSetConeBits(bitSet, subClazz);
    }
}
Also used : PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)

Example 13 with PsiCFGClass

use of com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass in project android by JetBrains.

the class CallgraphBuilder method addInvokeExprWithThisRef.

public void addInvokeExprWithThisRef(GraphNode node, PsiType thisBaseType, PsiCFGMethod method) {
    if (!method.isAbstract()) {
        addToCallGraph(node, method);
    } else {
        PsiClassType classType = null;
        PsiCFGClass cfgClass = null;
        if (thisBaseType instanceof PsiClassType) {
            classType = (PsiClassType) thisBaseType;
            cfgClass = mScene.getPsiCFGClass(classType.resolve());
        } else {
            PsiCFGDebugUtil.LOG.warning("PsiType of ThisRef is not a PsiClassType :" + thisBaseType.getClass().getSimpleName());
            return;
        }
        if (cfgClass == null) {
            PsiCFGDebugUtil.LOG.warning("PsiType of ThisRef cannot be resolved to cfgClass :" + thisBaseType.getClass().getSimpleName());
        }
        ArrayList<PsiCFGMethod> methodsList = Lists.newArrayList();
        recursivelyQueryConcreteMethodFromChildrenWithCache(methodsList, cfgClass, method.getSignature());
    }
}
Also used : PsiCFGMethod(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod) PsiClassType(com.intellij.psi.PsiClassType) PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)

Example 14 with PsiCFGClass

use of com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass in project android by JetBrains.

the class CallgraphBuilder method recursivelyQueryConcreteMethodFromChildrenWithOutCache.

public void recursivelyQueryConcreteMethodFromChildrenWithOutCache(ArrayList<PsiCFGMethod> methodList, PsiCFGClass receiverClass, PsiCFGPartialMethodSignature signature) {
    Pair<PsiCFGClass, PsiCFGPartialMethodSignature> keyPair = new Pair<>(receiverClass, signature);
    PsiCFGMethod method = receiverClass.getMethod(signature);
    if (method != null && (!method.isAbstract())) {
        methodList.add(method);
    }
    //Go through sub classes and interfaces
    for (PsiCFGClass subClass : receiverClass.getSubClassSet()) {
        if (mMethodOrderTreeMap.containsKey(keyPair)) {
            methodList.addAll(mMethodOrderTreeMap.get(keyPair));
        } else {
            recursivelyQueryConcreteMethodFromChildrenWithOutCache(methodList, subClass, signature);
        }
    }
}
Also used : PsiCFGMethod(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod) PsiCFGPartialMethodSignature(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGPartialMethodSignature) PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass) Pair(com.intellij.openapi.util.Pair)

Example 15 with PsiCFGClass

use of com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass in project android by JetBrains.

the class PsiCFGAnalysisUtil method performStage1.

/**
   * The purpose of the Stage1 is scan the project java files and
   * create wrapper objects for the Fields and Methods.
   */
public void performStage1() {
    PsiCFGClass[] appClasses = mScene.getAllApplicationClasses();
    for (PsiCFGClass curClass : appClasses) {
        parseFields(curClass);
        parseMethods(curClass);
    }
}
Also used : PsiCFGClass(com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)

Aggregations

PsiCFGClass (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGClass)28 PsiCFGMethod (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGMethod)8 PsiCFGPartialMethodSignature (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGPartialMethodSignature)3 PsiCFGField (com.android.tools.idea.experimental.codeanalysis.datastructs.PsiCFGField)2 PsiClassType (com.intellij.psi.PsiClassType)2 MethodGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.MethodGraph)1 PsiCFGAnalysisUtil (com.android.tools.idea.experimental.codeanalysis.utils.PsiCFGAnalysisUtil)1 Pair (com.intellij.openapi.util.Pair)1 PsiClass (com.intellij.psi.PsiClass)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1