Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Soot - a J*va Optimization Framework
   * Copyright (C) 2007 Eric Bodden
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 package soot.jimple.toolkits.annotation.logic;
 
 import java.util.List;
 
 import soot.Unit;
A (natural) loop in Jimple. A back-edge (t,h) is a control-flog edge for which h dominates t. In this case h is the header and the loop consists of all statements s which reach t without passing through h.

Author(s):
Eric Bodden
 
 public class Loop {
     
     protected final Stmt header;
     protected final Stmt backJump;
     protected final List<StmtloopStatements;
     protected final UnitGraph g;
     protected Collection<StmtloopExists;

    
Creates a new loop. Expects that the last statement in the list is the loop head and the second-last statement is the back-jump to the head. LoopFinder will normally guarantee this.

Parameters:
head the loop header
loopStatements an ordered list of loop statements, ending with the header
g the unit graph according to which the loop exists
 
     Loop(Stmt headList<StmtloopStatementsUnitGraph g) {
         this. = head;
         this. = g;
 
         //put header to the top
         loopStatements.remove(head);
         loopStatements.add(0, head);
         
         //last statement
         this. = loopStatements.get(loopStatements.size()-1);
         
         assert g.getSuccsOf(this.).contains(head); //must branch back to the head
 
         this. = loopStatements;
     }

    

Returns:
the loop head
 
     public Stmt getHead() {
         return ;
     }

    
Returns the statement that jumps back to the head, thereby constituing the loop.
 
     public Stmt getBackJumpStmt() {
         return ;
     }

    

Returns:
all statements of the loop, including the header; the header will be the first element returned and then the other statements follow in the natural ordering of the loop
 
     public List<StmtgetLoopStatements() {
         return ;
     }
    
    
Returns all loop exists. A loop exit is a statement which has a successor that is not contained in the loop.
 
     public Collection<StmtgetLoopExits() {
         if(==null) {
              = new HashSet<Stmt>();
             for (Stmt s : ) {
                 for (Unit succ : .getSuccsOf(s)) {
                    if(!.contains(succ)) {
                        .add(s);
                    }
                }
            }
        }
        return ;
    }
    
    
Computes all targets of the given loop exit, i.e. statements that the exit jumps to but which are not part of this loop.
    public Collection<StmttargetsOfLoopExit(Stmt loopExit) {
        assert getLoopExits().contains(loopExit);
        List<Unitsuccs = .getSuccsOf(loopExit);
        Collection<Stmtres = new HashSet<Stmt>();
        for (Unit u : succs) {
            Stmt s = (Stmt)u;
            res.add(s);            
        }
        res.removeAll();
        return res;
    }
    
    
Returns true if this loop certainly loops forever, i.e. if it has not exit.

See also:
getLoopExits()
    public boolean loopsForever() {
        return getLoopExits().isEmpty();
    }

    
Returns true if this loop has a single exit statement.

See also:
getLoopExits()
    public boolean hasSingleExit() {
        return getLoopExits().size()==1;
    }

    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (( == null) ? 0 : .hashCode());
        result = prime * result
                + (( == null) ? 0 : .hashCode());
        return result;
    }

    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Loop other = (Loopobj;
        if ( == null) {
            if (other.header != null)
                return false;
        } else if (!.equals(other.header))
            return false;
        if ( == null) {
            if (other.loopStatements != null)
                return false;
        } else if (!.equals(other.loopStatements))
            return false;
        return true;
    }
New to GrepCode? Check out our FAQ X