KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > ReachingTypeDumper


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 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;
21 import java.util.*;
22 import soot.*;
23 import java.io.*;
24
25 /** Dumps the reaching types of each local variable to a file in a format that
26  * can be easily compared with results of other analyses, such as VTA.
27  * @author Ondrej Lhotak
28  */

29 public class ReachingTypeDumper {
30     public ReachingTypeDumper( PointsToAnalysis pa, String JavaDoc output_dir ) {
31         this.pa = pa;
32         this.output_dir = output_dir;
33     }
34     public void dump() {
35         try {
36             PrintWriter file = new PrintWriter(
37                 new FileOutputStream( new File(output_dir, "types") ) );
38             for( Iterator it = Scene.v().getApplicationClasses().iterator();
39                     it.hasNext(); ) {
40                 handleClass( file, (SootClass) it.next() );
41             }
42             for( Iterator it = Scene.v().getLibraryClasses().iterator();
43                     it.hasNext(); ) {
44                 handleClass( file, (SootClass) it.next() );
45             }
46             file.close();
47         } catch( IOException e ) {
48             throw new RuntimeException JavaDoc( "Couldn't dump reaching types."+e );
49         }
50     }
51
52
53     /* End of public methods. */
54     /* End of package methods. */
55
56     protected PointsToAnalysis pa;
57     protected String JavaDoc output_dir;
58
59     protected void handleClass( PrintWriter out, SootClass c ) {
60         for( Iterator mIt = c.methodIterator(); mIt.hasNext(); ) {
61             final SootMethod m = (SootMethod) mIt.next();
62             if( !m.isConcrete() ) continue;
63             Body b = m.retrieveActiveBody();
64             TreeSet sortedLocals = new TreeSet( new StringComparator() );
65             sortedLocals.addAll( b.getLocals() );
66             for( Iterator lIt = sortedLocals.iterator(); lIt.hasNext(); ) {
67                 final Local l = (Local) lIt.next();
68                 out.println( "V "+m+l );
69                 if( l.getType() instanceof RefLikeType ) {
70                     Set types = pa.reachingObjects( l ).possibleTypes();
71                     TreeSet sortedTypes = new TreeSet( new StringComparator() );
72                     sortedTypes.addAll( types );
73                     for( Iterator tIt = sortedTypes.iterator(); tIt.hasNext(); ) {
74                         out.println( "T "+tIt.next() );
75                     }
76                 }
77             }
78         }
79     }
80     class StringComparator implements Comparator {
81         public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
82             return o1.toString().compareTo( o2.toString() );
83         }
84     }
85 }
86
87
Popular Tags