KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > transformations > UselessLabelFinder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.toolkits.base.AST.transformations;
21
22 import soot.*;
23 import java.util.*;
24 import soot.jimple.*;
25 import soot.dava.internal.asg.*;
26 import soot.dava.internal.SET.*;
27 import soot.dava.internal.AST.*;
28 import soot.dava.internal.javaRep.*;
29
30 public class UselessLabelFinder{
31     public UselessLabelFinder( Singletons.Global g ) {}
32     public static UselessLabelFinder v() { return G.v().soot_dava_toolkits_base_AST_transformations_UselessLabelFinder(); }
33
34     
35     //check whether label on a node is useless
36
public void findAndKill(ASTNode node){
37     if(!(node instanceof ASTLabeledNode)){
38         return;
39     }
40     String JavaDoc label = ((ASTLabeledNode)node).get_Label().toString();
41     List subBodies = (List)node.get_SubBodies();
42     Iterator it = subBodies.iterator();
43     while(it.hasNext()){
44         if(node instanceof ASTTryNode){
45         ASTTryNode.container subBody = (ASTTryNode.container)it.next();
46         if(checkForBreak((List)subBody.o,label)){
47             //true means break was found so we cant remove
48
}
49         else{
50             ((ASTLabeledNode)node).set_Label(new SETNodeLabel());
51             //System.out.println("USELESS LABEL DETECTED");
52
break;
53         }
54         }
55         else{//not an astTryNode
56
if(checkForBreak((List)it.next(),label)){
57             //true means break was found so we cant remove
58
}
59         else{
60             ((ASTLabeledNode)node).set_Label(new SETNodeLabel());
61             //System.out.println("USELESS LABEL DETECTED");
62
break;
63         }
64         }
65     }
66     }
67
68     /*
69       Returns True if finds a break for this label
70     */

71     private boolean checkForBreak(List ASTNodeBody,String JavaDoc outerLabel){
72     Iterator it = ASTNodeBody.iterator();
73     while(it.hasNext()){
74         ASTNode temp = (ASTNode)it.next();
75         //check if this is ASTStatementSequenceNode
76
if(temp instanceof ASTStatementSequenceNode){
77         ASTStatementSequenceNode stmtSeq = (ASTStatementSequenceNode)temp;
78         List statements = stmtSeq.getStatements();
79         Iterator stmtIt = statements.iterator();
80         while(stmtIt.hasNext()){
81             AugmentedStmt as = (AugmentedStmt)stmtIt.next();
82             Stmt s = as.get_Stmt();
83             String JavaDoc labelBroken = breaksLabel(s);
84             if(labelBroken != null && outerLabel!=null){//stmt breaks some label
85
if(labelBroken.compareTo(outerLabel)==0){
86                 //we have found a break breaking this label
87
return true;
88             }
89             }
90         }
91         }//if it was a StmtSeq node
92
else{
93         //otherwise recursion
94
//getSubBodies
95
List subBodies=(List)temp.get_SubBodies();
96         Iterator subIt = subBodies.iterator();
97         while(subIt.hasNext()){
98             if(temp instanceof ASTTryNode){
99             ASTTryNode.container subBody = (ASTTryNode.container) subIt.next();
100             if(checkForBreak((List)subBody.o,outerLabel)){
101                 //if this is true there was a break found
102
return true;
103             }
104             }
105             else{
106             if(checkForBreak((List)subIt.next(),outerLabel)){
107                 //if this is true there was a break found
108
return true;
109             }
110             }
111         }
112         }
113     }
114     
115     return false;
116     }
117
118
119     /*
120       If the stmt is a break/continue stmt then this method
121       returns the labels name
122       else returns null
123     */

124     private String JavaDoc breaksLabel(Stmt stmt){
125     if(!(stmt instanceof DAbruptStmt)){
126         //this is not a break stmt
127
return null;
128     }
129     DAbruptStmt abStmt = (DAbruptStmt)stmt;
130     if(abStmt.is_Break() || abStmt.is_Continue()){
131         SETNodeLabel label = abStmt.getLabel();
132         return label.toString();
133     }
134     else
135         return null;
136     }
137 }
Popular Tags