コード例 #1
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  private boolean removeBeginNode() {
    List heads = getHeads();
    if (heads.size() != 1) {
      // System.out.println("heads: "+heads);
      // System.out.println("Error: the size of heads is not equal to 1!");
      return false;
      //	    System.exit(1);
    } else {
      JPegStmt head = (JPegStmt) heads.get(0);
      // System.out.println("test head: "+head);
      if (!head.getName().equals("begin")) {
        System.err.println("Error: the head is not begin node!");
        System.exit(1);
      }
      // remove begin node from heads list
      heads.remove(0);
      // set the preds list of the succs of head to a new list and put succs of head into heads
      Iterator succOfHeadIt = getSuccsOf(head).iterator();
      while (succOfHeadIt.hasNext()) {

        JPegStmt succOfHead = (JPegStmt) succOfHeadIt.next();

        unitToPreds.put(succOfHead, new ArrayList());
        // put succs of head into heads
        heads.add(succOfHead);
      }
      // remove begin node from inlinee Peg
      if (!mainPegChain.remove(head)) {
        System.err.println("fail to remove begin node in from mainPegChain!");
        System.exit(1);
      }
      if (!allNodes.contains(head)) {
        System.err.println("fail to find begin node in FlowSet allNodes!");
        System.exit(1);
      } else {
        allNodes.remove(head);
      }

      // remove begin node from unitToSuccs
      if (unitToSuccs.containsKey(head)) {
        unitToSuccs.remove(head);
      }
    }

    return true;
  }
コード例 #2
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  public boolean addPeg(PegGraph pg, Chain chain) {
    if (!pg.removeBeginNode()) return false;
    //		System.out.println("adding one peg into another");

    //		System.out.println("after removeBeginNode===");
    //	pg.testPegChain();
    // System.out.println(pg);

    // put every node of peg into this
    Iterator mainIt = pg.mainIterator();

    while (mainIt.hasNext()) {
      JPegStmt s = (JPegStmt) mainIt.next();
      //			System.out.println("add to mainPegChain: "+s);
      mainPegChain.addLast(s);
      //			if (chain.contains(s)){
      //				System.err.println("error! chain contains: "+s);
      //				System.exit(1);
      //			}
      //			else
      //				chain.addLast(s);

    }
    Iterator it = pg.iterator();
    while (it.hasNext()) {
      JPegStmt s = (JPegStmt) it.next();
      // System.out.println("add to allNodes: "+s);
      if (allNodes.contains(s)) {
        System.err.println("error! allNodes contains: " + s);
        System.exit(1);
      } else allNodes.add(s);
    }
    //	testPegChain();
    //	testIterator();
    unitToSuccs.putAll(pg.getUnitToSuccs());
    unitToPreds.putAll(pg.getUnitToPreds());
    //	testUnitToSucc();
    // testUnitToPred();
    //		buildMaps(pg); // RLH
    return true;
  }
コード例 #3
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  private void buildHeadsAndTails() {

    List tailList = new ArrayList();
    List headList = new ArrayList();

    // Build the sets
    {
      Iterator unitIt = mainPegChain.iterator();

      while (unitIt.hasNext()) {
        JPegStmt s = (JPegStmt) unitIt.next();

        List succs = unitToSuccs.get(s);
        if (succs.size() == 0) tailList.add(s);
        if (!unitToPreds.containsKey(s)) {
          System.err.println("unitToPreds does not contain key: " + s);
          System.exit(1);
        }
        List preds = unitToPreds.get(s);
        if (preds.size() == 0) headList.add(s);
        // System.out.println("head is:");
      }
    }
    tails = (List) tailList;
    heads = (List) headList;
    //	tails = Collections.unmodifiableList(tailList);
    // heads = Collections.unmodifiableList(headList);

    Iterator tmpIt = heads.iterator();

    while (tmpIt.hasNext()) {
      Object temp = tmpIt.next();
      // System.out.println(temp);
    }

    buildPredecessor(mainPegChain);
  }
コード例 #4
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  protected void buildSuccsForInlining(JPegStmt stmt, Chain chain, PegGraph inlinee) {
    // System.out.println("entering buildSuccsForInlining...");
    Tag tag = (Tag) stmt.getTags().get(0);
    // System.out.println("stmt is: "+tag+" "+stmt);
    /*connect heads of inlinee with the preds of invokeStmt and
     * delete stmt from the succs list from the preds
     */

    Iterator predIt = getPredsOf(stmt).iterator();
    // System.out.println("preds list: "+getPredsOf(stmt));
    // System.out.println("preds size: "+getPredsOf(stmt).size());
    Iterator headsIt = inlinee.getHeads().iterator();
    {
      // System.out.println("heads: "+inlinee.getHeads());
      while (predIt.hasNext()) {
        JPegStmt pred = (JPegStmt) predIt.next();
        // System.out.println("pred: "+pred);
        List succList = (List) getSuccsOf(pred);
        // System.out.println("succList of pred: "+succList);
        int pos = succList.indexOf(stmt);
        // System.out.println("remove : "+stmt + " from succList: \n"+succList+ "\n of pred" );
        // remove invokeStmt
        succList.remove(pos);

        while (headsIt.hasNext()) {
          succList.add(headsIt.next());
        }
        unitToSuccs.put(pred, succList);
      }

      {
        while (headsIt.hasNext()) {
          Object head = headsIt.next();
          List predsOfHeads = new ArrayList();
          predsOfHeads.addAll(getPredsOf(head));
          unitToPreds.put(head, predsOfHeads);
        }
      }
      /*
      {
      predIt = getPredsOf(stmt).iterator();
      while (predIt.hasNext()){
      JPegStmt s = (JPegStmt)predIt.next();
      if (unitToSuccs.containsKey(s)){
      Iterator succIt = ((List) unitToSuccs.get(s)).iterator();
      while(succIt.hasNext()){

      //Object successor =  succIt.next();
       JPegStmt successor = (JPegStmt)succIt.next();
       List predList = (List) unitToPreds.get(successor);
       if (predList != null) {
       try {
       predList.add(s);

       } catch(NullPointerException e) {
       System.out.println(s + "successor: " + successor);
       throw e;
       }
       }
       }
       }
       }




       }*/

    }

    /*connect tails of inlinee with the succ of invokeStmt and
     * delete stmt from the
     */

    Iterator tailsIt = inlinee.getTails().iterator();
    {
      // System.out.println("tails: "+inlinee.getTails());
      while (tailsIt.hasNext()) {
        Iterator succIt = getSuccsOf(stmt).iterator();
        JPegStmt tail = (JPegStmt) tailsIt.next();
        List succList = null;
        if (unitToSuccs.containsKey(tail)) {
          // System.out.println("error: unitToSucc containsKey: "+tail);
          succList = (List) getSuccsOf(tail);
          // System.out.println("succList: "+succList);
        } else {

          succList = new ArrayList();
        }
        while (succIt.hasNext()) {
          JPegStmt succ = (JPegStmt) succIt.next();
          succList.add(succ);
          // System.out.println("succ: "+succ);
          // remove stmt from the preds list of the succs of itself.
          List predListOfSucc = getPredsOf(succ);
          if (predListOfSucc == null) {
            System.err.println("Error: predListOfSucc is null!");
            System.exit(1);
          } else {
            if (predListOfSucc.size() != 0) {

              int pos = predListOfSucc.indexOf(stmt);
              if (pos > 0 || pos == 0) {

                //	System.out.println("remove stmt: "+stmt+" from the preds list"+predListOfSucc+"
                // of the succ");
                predListOfSucc.remove(pos);
              }

              //		System.out.println("remove(from PRED): ");
            }
          }
          unitToPreds.put(succ, predListOfSucc);
        }
        unitToSuccs.put(tail, succList);
        // System.out.println("put: "+tail);
        // System.out.println("succList: "+succList+ "into unitToSucc");

      }
    }

    // add Nov 1
    {
      tailsIt = inlinee.getTails().iterator();
      while (tailsIt.hasNext()) {
        JPegStmt s = (JPegStmt) tailsIt.next();
        if (unitToSuccs.containsKey(s)) {
          Iterator succIt = unitToSuccs.get(s).iterator();
          while (succIt.hasNext()) {

            // Object successor =  succIt.next();
            JPegStmt successor = (JPegStmt) succIt.next();
            List<JPegStmt> predList = unitToPreds.get(successor);
            if (predList != null && !predList.contains(s)) {
              try {
                predList.add(s);
                /*
                Tag tag = (Tag)successor.getTags().get(0);
                System.out.println("add "+s+" to predlist of "+tag+" "+successor);
                */
              } catch (NullPointerException e) {
                System.out.println(s + "successor: " + successor);
                throw e;
              }
            }
          }
        }
      }
    }
    // end add Nov 1

    // System.out.println("stmt: "+stmt);
    // remove stmt from allNodes and mainPegChain
    // System.out.println("mainPegChain contains stmt: "+mainPegChain.contains(stmt));
    //		testPegChain();

    if (!allNodes.contains(stmt)) {
      System.err.println("fail to find begin node in  allNodes!");
      System.exit(1);
    } else {
      allNodes.remove(stmt);
      // System.out.println("remove from allNode: "+stmt);
    }

    if (!chain.contains(stmt)) {
      System.err.println("Error! Chain does not contains stmt (extending point)!");
      System.exit(1);

    } else {
      if (!chain.remove(stmt)) {
        System.err.println("fail to remove invoke stmt in from Chain!");
        System.exit(1);
      }
    }
    /*
    if (!mainPegChain.contains(stmt)){
    boolean find = false;
    //System.out.println("main chain does not contain AFTER");
     Set maps = startToThread.entrySet();
     for(Iterator iter=maps.iterator(); iter.hasNext();){
     Map.Entry entry = (Map.Entry)iter.next();
     Object startNode = entry.getKey();
     Iterator runIt  = ((List)entry.getValue()).iterator();
     while (runIt.hasNext()){
     Chain chain=(Chain)runIt.next();
     if (chain.contains(stmt)) {
     find = true;
     if (!chain.remove(stmt)){
     System.err.println("fail to remove begin node in from mainPegChain!");
     System.exit(1);
     }
     break;
     }
     }
     if (find == false){
     System.err.println("fail to find stmt: "+stmt+" in chains!");
     System.exit(1);
     }
     }
     //this.toString();
      }
      else{
      if (!mainPegChain.remove(stmt)) {
      System.err.println("fail to remove begin node in from mainPegChain!");
      System.exit(1);
      }
      else{
      // System.out.println("remove(from mainchain): "+stmt);
       }
       }
       */
    // remove stmt from unitToSuccs and unitToPreds
    if (unitToSuccs.containsKey(stmt)) {
      unitToSuccs.remove(stmt);
    }
    if (unitToPreds.containsKey(stmt)) {
      unitToPreds.remove(stmt);
    }
  }
コード例 #5
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  /*
  private void deleteExitToException(){
  Iterator it = iterator();
  while (it.hasNext()){
  JPegStmt stmt = (JPegStmt)it.next();
  Unit unit = stmt.getUnit();
  UnitGraph unitGraph = stmt.getUnitGraph();
  if (unit instanceof ExitMonitorStmt){
  Iterator succIt = unitGraph.getSuccsOf(unit).iterator();
  while(succIt.next
  && exceHandlers.contains(un) ){
  System.out.println("====find it! unit: "+unit+"\n un: "+un);
  continue;
  }
  }
  }
  */
  private void buildPredecessor(Chain pegChain) {
    // System.out.println("==building predcessor===");

    // initialize the pred sets to empty
    {
      JPegStmt s = null;
      Iterator unitIt = pegChain.iterator();
      while (unitIt.hasNext()) {

        s = (JPegStmt) unitIt.next();

        unitToPreds.put(s, new ArrayList());
      }
    }
    //	System.out.println("==finish init of unitToPred===");
    {
      Iterator unitIt = pegChain.iterator();

      while (unitIt.hasNext()) {

        Object s = unitIt.next();
        //		System.out.println("s is: "+s);

        // Modify preds set for each successor for this statement
        if (unitToSuccs.containsKey(s)) {
          List succList = unitToSuccs.get(s);
          Iterator succIt = succList.iterator();
          //		    System.out.println("unitToSuccs contains "+s);
          //		    System.out.println("succList is: "+succList);
          while (succIt.hasNext()) {

            // Object successor =  succIt.next();
            JPegStmt successor = (JPegStmt) succIt.next();
            //			System.out.println("successor is: "+successor);
            List<Object> predList = unitToPreds.get(successor);
            //			System.out.println("predList is: "+predList);
            if (predList != null && !predList.contains(s)) {
              try {
                predList.add(s);
                /*
                Tag tag1 = (Tag)((JPegStmt)s).getTags().get(0);
                System.out.println("add "+tag1+" "+s+" to predListof");
                Tag tag2 = (Tag)((JPegStmt)successor).getTags().get(0);
                System.out.println(tag2+" "+successor);
                */
              } catch (NullPointerException e) {
                System.out.println(s + "successor: " + successor);
                throw e;
              }
              // if (((JPegStmt)successor).getName().equals("start")){
              if (successor instanceof StartStmt) {
                List runMethodChainList = startToThread.get(successor);
                if (runMethodChainList == null) {
                  throw new RuntimeException("null runmehtodchain: \n" + successor.getUnit());
                }
                Iterator possibleMethodIt = runMethodChainList.iterator();
                while (possibleMethodIt.hasNext()) {

                  Chain subChain = (Chain) possibleMethodIt.next();

                  buildPredecessor(subChain);
                }
              }
            } else {
              System.err.println("predlist of " + s + " is null");
              //							System.exit(1);
            }
            //			unitToPreds.put(successor, predList);

          }

        } else {
          System.err.println("unitToSuccs does not contains key" + s);
          System.exit(1);
        }
      }
    }
  }
コード例 #6
0
ファイル: PegGraph.java プロジェクト: Hounge/apkinspector
  // This method adds the monitorenter/exit statements into whichever pegChain contains the
  // corresponding node statement
  protected void addMonitorStmt() {
    // System.out.println("====entering addMonitorStmt");
    if (synch.size() > 0) {
      // System.out.println("synch: "+synch);
      Iterator<List> it = synch.iterator();
      while (it.hasNext()) {
        List list = it.next();

        JPegStmt node = (JPegStmt) list.get(0);
        JPegStmt enter = (JPegStmt) list.get(1);
        JPegStmt exit = (JPegStmt) list.get(2);
        //		System.out.println("monitor node: "+node);
        // System.out.println("monitor enter: "+enter);
        // System.out.println("monitor exit: "+exit);
        // add for test
        // System.out.println("allNodes contains node: "+allNodes.contains(node));
        // end add for test

        {
          if (!mainPegChain.contains(node)) {

            boolean find = false;
            // System.out.println("main chain does not contain node");
            Set maps = startToThread.entrySet();
            // System.out.println("size of startToThread: "+startToThread.size());
            for (Iterator iter = maps.iterator(); iter.hasNext(); ) {
              Map.Entry entry = (Map.Entry) iter.next();
              Object startNode = entry.getKey();
              Iterator runIt = ((List) entry.getValue()).iterator();
              while (runIt.hasNext()) {
                Chain chain = (Chain) runIt.next();
                //	testPegChain(chain);
                if (chain.contains(node)) {
                  find = true;
                  // System.out.println("---find it---");
                  chain.add(enter);
                  chain.add(exit);
                  break;
                }
              }
            }
            if (find == false) {
              System.err.println("fail to find stmt: " + node + " in chains!");
              System.exit(1);
            }

            // this.toString();
          } else {
            mainPegChain.add(enter);
            mainPegChain.add(exit);
          }
        }

        allNodes.add(enter);
        allNodes.add(exit);

        insertBefore(node, enter);
        insertAfter(node, exit);
      }
    }
    // add for test
    /*
    {
    // System.out.println("===main peg chain===");
     //testPegChain(mainPegChain);
      //System.out.println("===end main peg chain===");
       Set maps = startToThread.entrySet();
       for(Iterator iter=maps.iterator(); iter.hasNext();){
       Map.Entry entry = (Map.Entry)iter.next();
       Object startNode = entry.getKey();
       Iterator runIt  = ((List)entry.getValue()).iterator();
       while (runIt.hasNext()){
       Chain chain=(Chain)runIt.next();
       testPegChain(chain);
       }
       }
       }
       */
    //	System.out.println(this.toString());
    // end add for test
  }
コード例 #7
0
ファイル: Printer.java プロジェクト: safdariqbal/soot
/** Prints out a class and all its methods. */
public class Printer {
  public Printer(Singletons.Global g) {}

  public static Printer v() {
    return G.v().soot_Printer();
  }

  private static final char fileSeparator = System.getProperty("file.separator").charAt(0);

  public static final int USE_ABBREVIATIONS = 0x0001, ADD_JIMPLE_LN = 0x0010;

  public boolean useAbbreviations() {
    return (options & USE_ABBREVIATIONS) != 0;
  }

  public boolean addJimpleLn() {
    return (options & ADD_JIMPLE_LN) != 0;
  }

  int options = 0;

  public void setOption(int opt) {
    options |= opt;
  }

  public void clearOption(int opt) {
    options &= ~opt;
  }

  int jimpleLnNum = 0; // actual line number

  public int getJimpleLnNum() {
    return jimpleLnNum;
  }

  public void setJimpleLnNum(int newVal) {
    jimpleLnNum = newVal;
  }

  public void incJimpleLnNum() {
    jimpleLnNum++;
    // G.v().out.println("jimple Ln Num: "+jimpleLnNum);
  }

  public void printTo(SootClass cl, PrintWriter out) {
    // add jimple line number tags
    setJimpleLnNum(1);

    // Print class name + modifiers
    {
      StringTokenizer st = new StringTokenizer(Modifier.toString(cl.getModifiers()));
      while (st.hasMoreTokens()) {
        String tok = (String) st.nextToken();
        if (cl.isInterface() && tok.equals("abstract")) continue;
        out.print(tok + " ");
      }

      String classPrefix = "";

      if (!cl.isInterface()) {
        classPrefix = classPrefix + " class";
        classPrefix = classPrefix.trim();
      }

      out.print(classPrefix + " " + Scene.v().quotedNameOf(cl.getName()) + "");
    }

    // Print extension
    {
      if (cl.hasSuperclass())
        out.print(" extends " + Scene.v().quotedNameOf(cl.getSuperclass().getName()) + "");
    }

    // Print interfaces
    {
      Iterator interfaceIt = cl.getInterfaces().iterator();

      if (interfaceIt.hasNext()) {
        out.print(" implements ");

        out.print("" + Scene.v().quotedNameOf(((SootClass) interfaceIt.next()).getName()) + "");

        while (interfaceIt.hasNext()) {
          out.print(",");
          out.print(" " + Scene.v().quotedNameOf(((SootClass) interfaceIt.next()).getName()) + "");
        }
      }
    }

    out.println();
    incJimpleLnNum();
    /*        if (!addJimpleLn()) {
        Iterator clTagsIt = cl.getTags().iterator();
        while (clTagsIt.hasNext()) {
            final Tag t = (Tag)clTagsIt.next();
            out.println(t);
        }
    }*/
    out.println("{");
    incJimpleLnNum();
    if (Options.v().print_tags_in_output()) {
      Iterator cTagIterator = cl.getTags().iterator();
      while (cTagIterator.hasNext()) {
        Tag t = (Tag) cTagIterator.next();
        out.print("/*");
        out.print(t.toString());
        out.println("*/");
      }
    }

    // Print fields
    {
      Iterator fieldIt = cl.getFields().iterator();

      if (fieldIt.hasNext()) {
        while (fieldIt.hasNext()) {
          SootField f = (SootField) fieldIt.next();

          if (f.isPhantom()) continue;

          if (Options.v().print_tags_in_output()) {
            Iterator fTagIterator = f.getTags().iterator();
            while (fTagIterator.hasNext()) {
              Tag t = (Tag) fTagIterator.next();
              out.print("/*");
              out.print(t.toString());
              out.println("*/");
            }
          }
          out.println("    " + f.getDeclaration() + ";");
          if (addJimpleLn()) {
            setJimpleLnNum(addJimpleLnTags(getJimpleLnNum(), f));
          }

          // incJimpleLnNum();
        }
      }
    }

    // Print methods
    {
      Iterator methodIt = cl.methodIterator();

      if (methodIt.hasNext()) {
        if (cl.getMethodCount() != 0) {
          out.println();
          incJimpleLnNum();
        }

        while (methodIt.hasNext()) {
          SootMethod method = (SootMethod) methodIt.next();

          if (method.isPhantom()) continue;

          if (!Modifier.isAbstract(method.getModifiers())
              && !Modifier.isNative(method.getModifiers())) {
            if (!method.hasActiveBody())
              throw new RuntimeException("method " + method.getName() + " has no active body!");
            else if (Options.v().print_tags_in_output()) {
              Iterator mTagIterator = method.getTags().iterator();
              while (mTagIterator.hasNext()) {
                Tag t = (Tag) mTagIterator.next();
                out.print("/*");
                out.print(t.toString());
                out.println("*/");
              }
            }
            printTo(method.getActiveBody(), out);

            if (methodIt.hasNext()) {
              out.println();
              incJimpleLnNum();
            }
          } else {

            if (Options.v().print_tags_in_output()) {
              Iterator mTagIterator = method.getTags().iterator();
              while (mTagIterator.hasNext()) {
                Tag t = (Tag) mTagIterator.next();
                out.print("/*");
                out.print(t.toString());
                out.println("*/");
              }
            }

            out.print("    ");
            out.print(method.getDeclaration());
            out.println(";");
            incJimpleLnNum();
            if (methodIt.hasNext()) {
              out.println();
              incJimpleLnNum();
            }
          }
        }
      }
    }
    out.println("}");
    incJimpleLnNum();
  }

  /** Writes the class out to a file. */
  // This method is deprecated. Use soot.util.JasminOutputStream instead.
  public void writeXXXDeprecated(SootClass cl, String outputDir) {
    String outputDirWithSep = "";

    if (!outputDir.equals("")) outputDirWithSep = outputDir + fileSeparator;

    try {
      File tempFile = new File(outputDirWithSep + cl.getName() + ".jasmin");

      FileOutputStream streamOut = new FileOutputStream(tempFile);

      PrintWriter writerOut = new PrintWriter(new EscapedWriter(new OutputStreamWriter(streamOut)));

      if (cl.containsBafBody()) new soot.baf.JasminClass(cl).print(writerOut);
      else new soot.jimple.JasminClass(cl).print(writerOut);

      writerOut.close();

      if (Options.v().time()) Timers.v().assembleJasminTimer.start();

      // Invoke jasmin
      {
        String[] args;

        if (outputDir.equals("")) {
          args = new String[1];

          args[0] = cl.getName() + ".jasmin";
        } else {
          args = new String[3];

          args[0] = "-d";
          args[1] = outputDir;
          args[2] = outputDirWithSep + cl.getName() + ".jasmin";
        }

        jasmin.Main.main(args);
      }

      tempFile.delete();

      if (Options.v().time()) Timers.v().assembleJasminTimer.end();

    } catch (IOException e) {
      throw new RuntimeException("Could not produce new classfile! (" + e + ")");
    }
  }

  /**
   * Prints out the method corresponding to b Body, (declaration and body), in the textual format
   * corresponding to the IR used to encode b body.
   *
   * @param out a PrintWriter instance to print to.
   */
  public void printTo(Body b, PrintWriter out) {
    b.validate();

    boolean isPrecise = !useAbbreviations();

    String decl = b.getMethod().getDeclaration();

    out.println("    " + decl);
    // incJimpleLnNum();

    // only print tags if not printing attributes in a file
    if (!addJimpleLn()) {
      /*for( Iterator tIt = b.getMethod().getTags().iterator(); tIt.hasNext(); ) {    final Tag t = (Tag) tIt.next();
          out.println(t);
          incJimpleLnNum();

      }*/
    }

    if (addJimpleLn()) {
      setJimpleLnNum(addJimpleLnTags(getJimpleLnNum(), b.getMethod()));
      // G.v().out.println("added jimple ln tag for method: "+b.getMethod().toString()+"
      // "+b.getMethod().getDeclaringClass().getName());
    }

    out.println("    {");
    incJimpleLnNum();

    UnitGraph unitGraph = new soot.toolkits.graph.BriefUnitGraph(b);

    LabeledUnitPrinter up;
    if (isPrecise) up = new NormalUnitPrinter(b);
    else up = new BriefUnitPrinter(b);

    if (addJimpleLn()) {
      up.setPositionTagger(new AttributesUnitPrinter(getJimpleLnNum()));
    }

    printLocalsInBody(b, up);

    printStatementsInBody(b, out, up, unitGraph);

    out.println("    }");
    incJimpleLnNum();
  }

  /** Prints the given <code>JimpleBody</code> to the specified <code>PrintWriter</code>. */
  private void printStatementsInBody(
      Body body, java.io.PrintWriter out, LabeledUnitPrinter up, UnitGraph unitGraph) {
    Chain units = body.getUnits();
    Iterator unitIt = units.iterator();
    Unit currentStmt = null, previousStmt;

    while (unitIt.hasNext()) {

      previousStmt = currentStmt;
      currentStmt = (Unit) unitIt.next();

      // Print appropriate header.
      {
        // Put an empty line if the previous node was a branch node, the current node is a join node
        //   or the previous statement does not have body statement as a successor, or if
        //   body statement has a label on it

        if (currentStmt != units.getFirst()) {
          if (unitGraph.getSuccsOf(previousStmt).size() != 1
              || unitGraph.getPredsOf(currentStmt).size() != 1
              || up.labels().containsKey(currentStmt)) {
            up.newline();
          } else {
            // Or if the previous node does not have body statement as a successor.

            List succs = unitGraph.getSuccsOf(previousStmt);

            if (succs.get(0) != currentStmt) {
              up.newline();
            }
          }
        }

        if (up.labels().containsKey(currentStmt)) {
          up.unitRef(currentStmt, true);
          up.literal(":");
          up.newline();
        }

        if (up.references().containsKey(currentStmt)) {
          up.unitRef(currentStmt, false);
        }
      }

      up.startUnit(currentStmt);
      currentStmt.toString(up);
      up.endUnit(currentStmt);

      up.literal(";");
      up.newline();

      // only print them if not generating attributes files
      // because they mess up line number
      // if (!addJimpleLn()) {
      if (Options.v().print_tags_in_output()) {
        Iterator tagIterator = currentStmt.getTags().iterator();
        while (tagIterator.hasNext()) {
          Tag t = (Tag) tagIterator.next();
          up.noIndent();
          up.literal("/*");
          up.literal(t.toString());
          up.literal("*/");
          up.newline();
        }
        /*Iterator udIt = currentStmt.getUseAndDefBoxes().iterator();
        while (udIt.hasNext()) {
            ValueBox temp = (ValueBox)udIt.next();
            Iterator vbtags = temp.getTags().iterator();
            while (vbtags.hasNext()) {
                Tag t = (Tag) vbtags.next();
                up.noIndent();
                up.literal("VB Tag: "+t.toString());
                up.newline();
            }
        }*/
      }
    }

    out.print(up.toString());
    if (addJimpleLn()) {
      setJimpleLnNum(up.getPositionTagger().getEndLn());
    }

    // Print out exceptions
    {
      Iterator trapIt = body.getTraps().iterator();

      if (trapIt.hasNext()) {
        out.println();
        incJimpleLnNum();
      }

      while (trapIt.hasNext()) {
        Trap trap = (Trap) trapIt.next();

        out.println(
            "        catch "
                + Scene.v().quotedNameOf(trap.getException().getName())
                + " from "
                + up.labels().get(trap.getBeginUnit())
                + " to "
                + up.labels().get(trap.getEndUnit())
                + " with "
                + up.labels().get(trap.getHandlerUnit())
                + ";");

        incJimpleLnNum();
      }
    }
  }

  private int addJimpleLnTags(int lnNum, Unit stmt, int endLn) {

    if (endLn - lnNum <= 1) {
      stmt.addTag(new JimpleLineNumberTag(lnNum));
      // G.v().out.println(stmt.getClass().toString());
      lnNum++;
      return lnNum;
    } else {
      stmt.addTag(new JimpleLineNumberTag(lnNum, endLn));
      // G.v().out.println("multi-line: "+stmt.getClass().toString());
      endLn++;
      return endLn;
    }
  }

  private int addJimpleLnTags(int lnNum, SootMethod meth) {
    meth.addTag(new JimpleLineNumberTag(lnNum));
    lnNum++;
    return lnNum;
  }

  private int addJimpleLnTags(int lnNum, SootField f) {
    f.addTag(new JimpleLineNumberTag(lnNum));
    lnNum++;
    return lnNum;
  }

  /** Prints the given <code>JimpleBody</code> to the specified <code>PrintWriter</code>. */
  private void printLocalsInBody(Body body, UnitPrinter up) {
    // Print out local variables
    {
      Map typeToLocals = new DeterministicHashMap(body.getLocalCount() * 2 + 1, 0.7f);

      // Collect locals
      {
        Iterator localIt = body.getLocals().iterator();

        while (localIt.hasNext()) {
          Local local = (Local) localIt.next();

          List localList;

          Type t = local.getType();

          if (typeToLocals.containsKey(t)) localList = (List) typeToLocals.get(t);
          else {
            localList = new ArrayList();
            typeToLocals.put(t, localList);
          }

          localList.add(local);
        }
      }

      // Print locals
      {
        Iterator typeIt = typeToLocals.keySet().iterator();

        while (typeIt.hasNext()) {
          Type type = (Type) typeIt.next();

          List localList = (List) typeToLocals.get(type);
          Object[] locals = localList.toArray();
          up.type(type);
          up.literal(" ");

          for (int k = 0; k < locals.length; k++) {
            if (k != 0) up.literal(", ");

            up.local((Local) locals[k]);
          }

          up.literal(";");
          up.newline();
        }
      }

      if (!typeToLocals.isEmpty()) {
        up.newline();
      }
    }
  }
}