KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > tools > BadFields


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej 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.tools;
21 import soot.*;
22 import java.io.*;
23 import java.util.*;
24 import soot.jimple.*;
25 import soot.jimple.toolkits.callgraph.*;
26
27 public class BadFields extends SceneTransformer {
28     public static void main(String JavaDoc[] args)
29     {
30     PackManager.v().getPack("cg").add(
31         new Transform("cg.badfields", new BadFields()));
32     soot.Main.main(args);
33     }
34
35     private SootClass lastClass;
36     private SootClass currentClass;
37
38     protected void internalTransform(String JavaDoc phaseName, Map options)
39     {
40         lastClass = null;
41
42         for( Iterator clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext(); ) {
43
44             final SootClass cl = (SootClass) clIt.next();
45             currentClass = cl;
46             handleClass( cl );
47             for( Iterator it = cl.methodIterator(); it.hasNext(); ) {
48                 handleMethod( (SootMethod) it.next() );
49             }
50         }
51         Scene.v().setCallGraph( new CallGraph() );
52     }
53
54     private void handleClass( SootClass cl ) {
55         for( Iterator fIt = cl.getFields().iterator(); fIt.hasNext(); ) {
56             final SootField f = (SootField) fIt.next();
57             if( !f.isStatic() ) continue;
58             String JavaDoc typeName = f.getType().toString();
59             if( typeName.equals( "java.lang.Class" ) ) continue;
60             if( f.isFinal() ) {
61                 if( f.getType() instanceof PrimType ) continue;
62                 if( typeName.equals( "java.io.PrintStream" ) ) continue;
63                 if( typeName.equals( "java.lang.String" ) ) continue;
64                 if( typeName.equals( "java.lang.Object" ) ) continue;
65                 if( typeName.equals( "java.lang.Integer" ) ) continue;
66                 if( typeName.equals( "java.lang.Boolean" ) ) continue;
67             }
68             warn( "Bad field "+f );
69         }
70     }
71
72     private void warn( String JavaDoc warning ) {
73         if( lastClass != currentClass )
74             G.v().out.println( "In class "+currentClass );
75         lastClass = currentClass;
76         G.v().out.println( " "+warning );
77     }
78
79     private void handleMethod( SootMethod m ) {
80         if( !m.isConcrete() ) return;
81         for( Iterator bIt = m.retrieveActiveBody().getUseAndDefBoxes().iterator(); bIt.hasNext(); ) {
82             final ValueBox b = (ValueBox) bIt.next();
83             Value v = b.getValue();
84             if( !(v instanceof StaticFieldRef) ) continue;
85             StaticFieldRef sfr = (StaticFieldRef) v;
86             SootField f = sfr.getField();
87             if( !f.getDeclaringClass().getName().equals( "java.lang.System" ) )
88                 continue;
89             if( f.getName().equals( "err" ) ) {
90                 G.v().out.println( "Use of System.err in "+m );
91             }
92             if( f.getName().equals( "out" ) ) {
93                 G.v().out.println( "Use of System.out in "+m );
94             }
95         }
96         for( Iterator sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
97             final Stmt s = (Stmt) sIt.next();
98             if( !s.containsInvokeExpr() ) continue;
99             InvokeExpr ie = s.getInvokeExpr();
100             SootMethod target = ie.getMethod();
101             if( target.getDeclaringClass().getName().equals( "java.lang.System" )
102                     && target.getName().equals( "exit" ) ) {
103                 warn( ""+m+" calls System.exit" );
104             }
105         }
106         if( m.getName().equals( "<clinit>" ) ) {
107             for( Iterator sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
108                 final Stmt s = (Stmt) sIt.next();
109                 for( Iterator bIt = s.getUseBoxes().iterator(); bIt.hasNext(); ) {
110                     final ValueBox b = (ValueBox) bIt.next();
111                     Value v = b.getValue();
112                     if( v instanceof FieldRef ) {
113                         warn( m.getName()+" reads field "+v );
114                     }
115                 }
116                 if( !s.containsInvokeExpr() ) continue;
117                 InvokeExpr ie = (InvokeExpr) s.getInvokeExpr();
118                 SootMethod target = ie.getMethod();
119                 calls( target );
120             }
121         }
122     }
123     private void calls( SootMethod target ) {
124         if( target.getName().equals("<init>") ) {
125             if( target.getDeclaringClass().getName().equals( "java.io.PrintStream" ) ) return;
126             if( target.getDeclaringClass().getName().equals( "java.lang.Boolean" ) ) return;
127             if( target.getDeclaringClass().getName().equals( "java.lang.Integer" ) ) return;
128             if( target.getDeclaringClass().getName().equals( "java.lang.String" ) ) return;
129             if( target.getDeclaringClass().getName().equals( "java.lang.Object" ) ) return;
130         }
131         if( target.getName().equals("getProperty") ) {
132             if( target.getDeclaringClass().getName().equals( "java.lang.System" ) ) return;
133         }
134         if( target.getName().equals("charAt") ) {
135             if( target.getDeclaringClass().getName().equals( "java.lang.String" ) ) return;
136         }
137         warn( "<clinit> invokes "+target );
138     }
139 }
140
141
142
Popular Tags