KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > UselessTryRemover


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
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;
21 import soot.*;
22
23 import java.util.*;
24 import soot.dava.internal.AST.*;
25
26 public class UselessTryRemover extends ASTAnalysis
27 {
28     public UselessTryRemover( Singletons.Global g ) {}
29     public static UselessTryRemover v() { return G.v().soot_dava_toolkits_base_AST_UselessTryRemover(); }
30
31     public int getAnalysisDepth()
32     {
33     return ANALYSE_AST;
34     }
35
36     public void analyseASTNode( ASTNode n)
37     {
38     Iterator sbit = n.get_SubBodies().iterator();
39
40     while (sbit.hasNext()) {
41         
42         List
43         subBody = null,
44         toRemove = new ArrayList();
45
46         if (n instanceof ASTTryNode)
47         subBody = (List) ((ASTTryNode.container) sbit.next()).o;
48         else
49         subBody = (List) sbit.next();
50
51
52         Iterator cit = subBody.iterator();
53         while (cit.hasNext()) {
54         Object JavaDoc child = cit.next();
55         
56         if (child instanceof ASTTryNode) {
57             ASTTryNode tryNode = (ASTTryNode) child;
58
59             tryNode.perform_Analysis( TryContentsFinder.v());
60
61             if ((tryNode.get_CatchList().isEmpty()) || (tryNode.isEmpty()))
62             toRemove.add( tryNode);
63         }
64         }
65
66         Iterator trit = toRemove.iterator();
67         while (trit.hasNext()) {
68         ASTTryNode tryNode = (ASTTryNode) trit.next();
69
70         subBody.addAll( subBody.indexOf( tryNode), tryNode.get_TryBody());
71         subBody.remove( tryNode);
72         }
73
74         if (toRemove.isEmpty() == false)
75         G.v().ASTAnalysis_modified = true;
76     }
77     }
78 }
79
Popular Tags