KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > annotation > fields > UnreachableFieldsTagger


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer Lhotak
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.jimple.toolkits.annotation.fields;
21 import soot.*;
22 import java.util.*;
23 import soot.toolkits.graph.*;
24 import soot.toolkits.scalar.*;
25 import soot.tagkit.*;
26 import soot.jimple.*;
27 import soot.util.queue.*;
28
29 /** A scene transformer that adds tags to unused fields. */
30 public class UnreachableFieldsTagger extends SceneTransformer
31 {
32     public UnreachableFieldsTagger (Singletons.Global g) {}
33     public static UnreachableFieldsTagger v() { return G.v().soot_jimple_toolkits_annotation_fields_UnreachableFieldsTagger();}
34
35     protected void internalTransform(String JavaDoc phaseName, Map options){
36
37         // make list of all fields
38
ArrayList fieldList = new ArrayList();
39         
40         Iterator getClassesIt = Scene.v().getApplicationClasses().iterator();
41         while (getClassesIt.hasNext()) {
42             SootClass appClass = (SootClass)getClassesIt.next();
43             //System.out.println("class to check: "+appClass);
44
Iterator getFieldsIt = appClass.getFields().iterator();
45             while (getFieldsIt.hasNext()) {
46                 SootField field = (SootField)getFieldsIt.next();
47                 //System.out.println("adding field: "+field);
48
fieldList.add(field);
49             }
50         }
51         
52         // from all bodies get all use boxes and eliminate used fields
53
getClassesIt = Scene.v().getApplicationClasses().iterator();
54         while (getClassesIt.hasNext()) {
55             SootClass appClass = (SootClass)getClassesIt.next();
56             Iterator mIt = appClass.getMethods().iterator();
57             while (mIt.hasNext()) {
58                 SootMethod sm = (SootMethod)mIt.next();
59                 //System.out.println("checking method: "+sm.getName());
60
if (!sm.hasActiveBody()) continue;
61                 if (!Scene.v().getReachableMethods().contains(sm)) continue;
62                 Body b = sm.getActiveBody();
63
64                 Iterator usesIt = b.getUseBoxes().iterator();
65                 while (usesIt.hasNext()) {
66                     ValueBox vBox = (ValueBox)usesIt.next();
67                     Value v = vBox.getValue();
68                     if (v instanceof FieldRef) {
69                         FieldRef fieldRef = (FieldRef)v;
70                         SootField f = fieldRef.getField();
71
72                         if (fieldList.contains(f)) {
73                             int index = fieldList.indexOf(f);
74                             fieldList.remove(index);
75                             //System.out.println("removed field: "+f);
76
}
77                     
78                     }
79                 }
80             
81             }
82         }
83         
84         // tag unused fields
85
Iterator unusedIt = fieldList.iterator();
86         while (unusedIt.hasNext()) {
87             SootField unusedField = (SootField)unusedIt.next();
88             unusedField.addTag(new StringTag("Field "+unusedField.getName()+" is not used!", "Unreachable Fields"));
89             unusedField.addTag(new ColorTag(ColorTag.RED, true, "Unreachable Fields"));
90             //System.out.println("tagged field: "+unusedField);
91

92         }
93     }
94
95 }
96
97
98
Popular Tags