private static ClassHierarchyProto.Node serializeNode(Node n) {
    List<ClassHierarchyProto.Node> children = new ArrayList<>();
    for (Node child : n.getChildren()) {
      children.add(serializeNode(child));
    }
    if (n instanceof ClassNode) {
      ClassNode<?> cn = (ClassNode<?>) n;
      ConstructorDef<?>[] injectable = cn.getInjectableConstructors();
      ConstructorDef<?>[] all = cn.getAllConstructors();
      List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all));
      others.removeAll(Arrays.asList(injectable));

      List<ClassHierarchyProto.ConstructorDef> injectableConstructors = new ArrayList<>();
      for (ConstructorDef<?> inj : injectable) {
        injectableConstructors.add(serializeConstructorDef(inj));
      }
      List<ClassHierarchyProto.ConstructorDef> otherConstructors = new ArrayList<>();
      for (ConstructorDef<?> other : others) {
        otherConstructors.add(serializeConstructorDef(other));
      }
      List<String> implFullNames = new ArrayList<>();
      for (ClassNode<?> impl : cn.getKnownImplementations()) {
        implFullNames.add(impl.getFullName());
      }
      return newClassNode(
          cn.getName(),
          cn.getFullName(),
          cn.isInjectionCandidate(),
          cn.isExternalConstructor(),
          cn.isUnit(),
          injectableConstructors,
          otherConstructors,
          implFullNames,
          children);
    } else if (n instanceof NamedParameterNode) {
      NamedParameterNode<?> np = (NamedParameterNode<?>) n;
      return newNamedParameterNode(
          np.getName(),
          np.getFullName(),
          np.getSimpleArgName(),
          np.getFullArgName(),
          np.isSet(),
          np.isList(),
          np.getDocumentation(),
          np.getShortName(),
          np.getDefaultInstanceAsStrings(),
          children);
    } else if (n instanceof PackageNode) {
      return newPackageNode(n.getName(), n.getFullName(), children);
    } else {
      throw new IllegalStateException("Encountered unknown type of Node: " + n);
    }
  }
  private void buildNameToNodeListTable(Node curNode) throws IOException {
    String fullName = curNode.getName();
    String parent = curNode.getParent();
    String separator = System.getProperty("file.separator");

    if (parent != null) {
      if (!fullName.startsWith(parent)) {
        throw new RuntimeException(
            "Internal error: parent of file name \""
                + fullName
                + "\" does not match file name \""
                + parent
                + "\"");
      }

      int len = parent.length();
      if (!parent.endsWith(separator)) {
        len += separator.length();
      }

      String fileName = fullName.substring(len);

      if (fileName == null) {
        throw new RuntimeException("Internal error: file name was empty");
      }

      List nodeList = (List) nameToNodeListTable.get(fileName);
      if (nodeList == null) {
        nodeList = new Vector();
        nameToNodeListTable.put(fileName, nodeList);
      }

      nodeList.add(curNode);
    } else {
      if (curNode != rootNode) {
        throw new RuntimeException(
            "Internal error: parent of file + \"" + fullName + "\"" + " was null");
      }
    }

    if (curNode.isDirectory()) {
      Iterator iter = curNode.getChildren();
      if (iter != null) {
        while (iter.hasNext()) {
          buildNameToNodeListTable((Node) iter.next());
        }
      }
    }
  }
Example #3
0
  /* Print the current type graph to the dotfile */
  public void printDot(String title, Call call) {
    boolean printUnifications = false;
    PrintStream ps = PointsToAnalysis.v().file;
    if (ps == null) return;

    ps.println("\ndigraph F {");
    ps.println("   size = \"7,7\"; rankdir = LR;");
    ps.println("   orientation = landscape;");

    ps.println("   subgraph cluster1 {");
    ps.println("   \"Method: " + method.getName() + "\" [color=white];");

    if (nodes.isEmpty()) {
      ps.println("   \"empty graph\" [color = white];");
      ps.println("   }");
      ps.println("}");
      return;
    }

    for (Node node : nodes) {
      if (!printUnifications && !node.isRep()) continue;
      String color = "style=filled,fillcolor=";
      if (node.isheap && node.hasallocs) color += "red,";
      else if (node.isheap) color += "orange,";
      else if (node.hasallocs) color += "grey,";
      else color += "white,";
      // if (node.istouched) color = "khaki";
      // if (node.hassync) color = "khaki";
      String shape = "shape=";
      if (node.istouched) shape += "box";
      else shape += "ellipse";

      ps.println("   o" + node.id + "[label = \"" + node.getName() + "\"," + color + shape + "];");
    }
    ps.println("   }");

    Map<Integer, Map<Integer, String>> labels = new HashMap<Integer, Map<Integer, String>>();
    for (Field f : fedges.keySet())
      for (FieldEdge e : fedges.get(f)) {
        if (labels.containsKey(e.src.id)) {
          if (labels.get(e.src.id).containsKey(e.dst.id)) {
            labels.get(e.src.id).put(e.dst.id, "*");
            //                            labels.get(e.src.id).get(e.dst.id) + ", " +
            //                            e.field.getName());
          } else labels.get(e.src.id).put(e.dst.id, e.field.getName());

        } else {
          Map<Integer, String> is = new HashMap<Integer, String>();
          is.put(e.dst.id, e.field.getName());
          labels.put(e.src.id, is);
        }
      }
    for (Integer i : labels.keySet())
      for (Integer j : labels.get(i).keySet())
        ps.print(
            "   o"
                + i
                + " -> o"
                + j
                + "[label=\""
                + labels.get(i).get(j)
                + "\",style=solid,color=black];");

    for (Call ce : cedges.keySet())
      for (CallEdge e : cedges.get(ce)) {
        if (!(e.call instanceof VirtualCallExpr)) continue;
        // if (!e.call.equals(call)) continue;
        ps.print(
            "   o"
                + e.src.id
                + " -> o"
                + e.dst.id
                + "[label=\""
                + e.call
                + "\",style=solid,color=red];");
      }

    if (printUnifications)
      for (Node node : nodes)
        if (node.parent != null)
          ps.println("   o" + node.id + " -> o" + node.parent.id + " [color = blue];");

    ps.println("}");
  }
Example #4
0
  /*
   * Finds the shortest route from a specified start node to a specified end node
   */
  private static void findShortestRoute(String inputString) {
    HashMap<Character, Node> nodeMapTemp = new HashMap<Character, Node>(nodeMap);
    HashMap<Character, Node> nodeMapDijkstra = new HashMap<Character, Node>();
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    Node current;
    Node currentTo;
    int shortestRoute = INFINITY;
    Set nodeSet;

    first = inputString.charAt(0);
    second = inputString.charAt(2);

    for (char key : nodeMap.keySet()) {
      nodeMap.get(key).setDistance(INFINITY);
    }

    if (first == second) {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (pq.size() != 0) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapDijkstra.put(pq.peek().getName(), pq.peek());
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          } else {
          }
        }
      }
      for (char key : nodeMapDijkstra.keySet()) {
        edgeList = nodeMapDijkstra.get(key).getEdges();
        for (i = 0; i < edgeList.size(); i++) {
          if (edgeList.get(i).getEdgeDestination() == second) {
            if (nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength()
                < shortestRoute) {
              shortestRoute =
                  nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength();
            }
          }
        }
      }
    } else {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (nodeMapTemp.get(second) != null) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          }
        }
      }
      shortestRoute = nodeMapTemp.get(second).getDistance();
    }
    try {
      bw.write(Integer.toString(shortestRoute));
      bw.newLine();
    } catch (IOException e) {
      System.out.println(e);
    }
  }