KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > fieldrw > FieldTagger


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.jimple.spark.fieldrw;
21 import soot.tagkit.*;
22 import soot.*;
23 import soot.util.*;
24 import java.util.*;
25 import soot.toolkits.graph.*;
26 import soot.jimple.toolkits.callgraph.*;
27 import soot.jimple.*;
28 import java.io.*;
29
30 public class FieldTagger extends BodyTransformer
31 {
32     public FieldTagger( Singletons.Global g ) {}
33     public static FieldTagger v() { return G.v().soot_jimple_spark_fieldrw_FieldTagger(); }
34
35     private HashSet processedMethods = new HashSet();
36     private HashMultiMap methodToWrite = new HashMultiMap();
37     private HashMultiMap methodToRead = new HashMultiMap();
38
39     protected void ensureProcessed( SootMethod m ) {
40         if( processedMethods.contains(m) ) return;
41         processedMethods.add(m);
42         if( !m.isConcrete() ) return;
43         if( m.isPhantom() ) return;
44         for( Iterator sIt = m.retrieveActiveBody().getUnits().iterator(); sIt.hasNext(); ) {
45             final Stmt s = (Stmt) sIt.next();
46             if( s instanceof AssignStmt ) {
47                 AssignStmt as = (AssignStmt) s;
48                 Value l = as.getLeftOp();
49                 if( l instanceof FieldRef ) {
50                     methodToWrite.put( m, ((FieldRef) l).getField() );
51                 }
52                 Value r = as.getRightOp();
53                 if( r instanceof FieldRef ) {
54                     methodToRead.put( m, ((FieldRef) r).getField() );
55                 }
56             }
57         }
58     }
59     protected void internalTransform(Body body, String JavaDoc phaseName, Map options)
60     {
61         int threshold = PhaseOptions.getInt( options, "threshold" );
62
63         ensureProcessed( body.getMethod() );
64
65         CallGraph cg = Scene.v().getCallGraph();
66         TransitiveTargets tt = new TransitiveTargets( cg );
67 statement: for( Iterator sIt = body.getUnits().iterator(); sIt.hasNext(); ) { final Stmt s = (Stmt) sIt.next();
68             HashSet read = new HashSet();
69             HashSet write = new HashSet();
70             Iterator it = tt.iterator( s );
71             while( it.hasNext() ) {
72                 SootMethod target = (SootMethod) it.next();
73                 ensureProcessed( target );
74                 if( target.isNative() ) continue statement;
75                 if( target.isPhantom() ) continue statement;
76                 read.addAll( methodToRead.get( target ) );
77                 write.addAll( methodToWrite.get( target ) );
78                 if( read.size() + write.size() > threshold ) {
79                     continue statement;
80                 }
81             }
82             s.addTag( new FieldReadTag( read ) );
83             s.addTag( new FieldWriteTag( write ) );
84         }
85     }
86 }
87
88
89
Popular Tags