KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
23 import soot.*;
24 import soot.jimple.*;
25 import soot.dava.*;
26 import soot.dava.internal.asg.*;
27 import soot.dava.internal.AST.*;
28 import soot.dava.toolkits.base.AST.analysis.*;
29
30
31 public class VoidReturnRemover extends DepthFirstAdapter{
32     DavaBody davaBody;
33
34     public VoidReturnRemover(DavaBody davaBody){
35     this.davaBody=davaBody;
36     }
37
38     public VoidReturnRemover(DavaBody davaBody,boolean verbose){
39     super(verbose);
40     this.davaBody=davaBody;
41     }
42
43     public void caseASTMethodNode(ASTMethodNode node){
44     
45     // check if this is a void method
46
SootMethod method = davaBody.getMethod();
47     Type type = method.getReturnType();
48     if(type instanceof VoidType){
49         // see if there is a stmtseq node in the end
50

51         List subBodies = node.get_SubBodies();
52         if(subBodies.size()==1){
53         List subBody = (List)subBodies.get(0);
54         //see if the last of this is a stmtseq node
55
if(subBody.size()==0){
56             //nothing inside subBody
57
return;
58         }
59
60         ASTNode last = (ASTNode)subBody.get(subBody.size()-1);
61         if(last instanceof ASTStatementSequenceNode){
62             //get last statement
63

64             List stmts = ((ASTStatementSequenceNode)last).getStatements();
65             if(stmts.size()==0){
66             //no stmts inside statement sequence node
67
subBody.remove(subBody.size()-1);
68             return;
69             }
70             AugmentedStmt lastas = (AugmentedStmt)stmts.get(stmts.size()-1);
71             Stmt lastStmt = lastas.get_Stmt();
72             if(lastStmt instanceof ReturnVoidStmt){
73
74             //we can remove the lastStmt
75
stmts.remove(stmts.size()-1);
76             /*
77               we need to check if we have made the size 0 in
78               which case the stmtSeq Node should also be removed
79             */

80             if(stmts.size()==0){
81                 subBody.remove(subBody.size()-1);
82             }
83                   
84             }
85         }
86         }
87     }
88     }
89 }
Popular Tags