KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > StaticDefinitionFinder


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 /*
21  * Maintained by Nomair A. Naeem
22  */

23
24 /*
25  * Class does a traversal of the AST and checks all definitions of fields.
26  * If any of the fields which are assigned is a final field the boolean
27  * finalFieldDefined is set to true
28  */

29 package soot.dava;
30
31 import soot.SootField;
32 import soot.SootMethod;
33 import soot.Value;
34 import soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter;
35 import soot.jimple.DefinitionStmt;
36 import soot.jimple.FieldRef;
37
38 public class StaticDefinitionFinder extends DepthFirstAdapter{
39
40     SootMethod method;
41     boolean finalFieldDefined;
42
43     public StaticDefinitionFinder(SootMethod method){
44     this.method = method;
45     finalFieldDefined=false;
46     }
47
48     public StaticDefinitionFinder(boolean verbose,SootMethod method){
49     super(verbose);
50     this.method= method;
51     finalFieldDefined=false;
52     }
53
54     public void inDefinitionStmt(DefinitionStmt s){
55     Value leftOp = s.getLeftOp();
56     if(leftOp instanceof FieldRef){
57         //System.out.println("leftOp is a fieldRef:"+s);
58
SootField field = ((FieldRef)leftOp).getField();
59         //check if this is a final field
60
if(field.isFinal()){
61         //System.out.println("the field is a final variable");
62
finalFieldDefined=true;
63         }
64     }
65     
66     }
67         
68     public boolean anyFinalFieldDefined(){
69     return finalFieldDefined;
70     }
71
72 }
Popular Tags