KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > callgraph > UnreachableMethodTransformer


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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.callgraph;
21
22 import soot.*;
23 import soot.jimple.*;
24 import soot.jimple.internal.*;
25 import soot.util.*;
26 import java.util.*;
27
28 public class UnreachableMethodTransformer extends BodyTransformer
29 {
30     protected void internalTransform(Body b, String JavaDoc phaseName, Map options) {
31         //System.out.println( "Performing UnreachableMethodTransformer" );
32
ReachableMethods reachableMethods = Scene.v().getReachableMethods();
33         SootMethod method = b.getMethod();
34         //System.out.println( "Method: " + method.getName() );
35
if( reachableMethods.contains( method ) ) return;
36
37         JimpleBody body = (JimpleBody) method.getActiveBody();
38
39         PatchingChain units = (PatchingChain) body.getUnits();
40         List list = new Vector();
41
42         Local tmpRef = Jimple.v().newLocal( "tmpRef", RefType.v( "java.io.PrintStream" ) );
43         body.getLocals().add(tmpRef);
44         list.add( Jimple.v().newAssignStmt( tmpRef, Jimple.v().newStaticFieldRef(
45             Scene.v().getField( "<java.lang.System: java.io.PrintStream out>" ).makeRef() ) ) );
46
47         SootMethod toCall = Scene.v().getMethod( "<java.lang.Thread: void dumpStack()>" );
48         list.add( Jimple.v().newInvokeStmt( Jimple.v().newStaticInvokeExpr( toCall.makeRef() ) ) );
49
50         toCall = Scene.v().getMethod( "<java.io.PrintStream: void println(java.lang.String)>" );
51         list.add( Jimple.v().newInvokeStmt( Jimple.v().newVirtualInvokeExpr(
52             tmpRef, toCall.makeRef(), StringConstant.v( "Executing supposedly unreachable method:" ) ) ) );
53         list.add( Jimple.v().newInvokeStmt( Jimple.v().newVirtualInvokeExpr(
54             tmpRef, toCall.makeRef(), StringConstant.v( "\t" + method.getDeclaringClass().getName() + "." + method.getName() ) ) ) );
55
56         toCall = Scene.v().getMethod( "<java.lang.System: void exit(int)>" );
57         list.add( Jimple.v().newInvokeStmt( Jimple.v().newStaticInvokeExpr( toCall.makeRef(), IntConstant.v( 1 ) ) ) );
58
59         /*
60         Stmt r;
61         if( method.getReturnType() instanceof VoidType ) {
62             list.add( r=Jimple.v().newReturnVoidStmt() );
63         } else if( method.getReturnType() instanceof RefLikeType ) {
64             list.add( r=Jimple.v().newReturnStmt( NullConstant.v() ) );
65         } else if( method.getReturnType() instanceof PrimType ) {
66             if( method.getReturnType() instanceof DoubleType ) {
67                 list.add( r=Jimple.v().newReturnStmt( DoubleConstant.v( 0 ) ) );
68             } else if( method.getReturnType() instanceof LongType ) {
69                 list.add( r=Jimple.v().newReturnStmt( LongConstant.v( 0 ) ) );
70             } else if( method.getReturnType() instanceof FloatType ) {
71                 list.add( r=Jimple.v().newReturnStmt( FloatConstant.v( 0 ) ) );
72             } else {
73                 list.add( r=Jimple.v().newReturnStmt( IntConstant.v( 0 ) ) );
74             }
75         } else {
76             throw new RuntimeException( "Wrong return method type: " + method.getReturnType() );
77         }
78         */

79
80         /*
81         if( method.getName().equals( "<init>" ) || method.getName().equals( "<clinit>" ) ) {
82
83             Object o = units.getFirst();
84             boolean insertFirst = false;
85             while( true ) {
86                 //System.out.println( "Unit: " + o );
87                 //System.out.println( "\tClass: " + o.getClass() );
88                 if( o == null ) {
89                     insertFirst = true;
90                     break;
91                 }
92                 if( o instanceof JInvokeStmt ) {
93                     JInvokeStmt stmt = (JInvokeStmt) o;
94                     if( (stmt.getInvokeExpr() instanceof SpecialInvokeExpr) ) {
95                         SootMethodRef
96                         break;
97                     }
98                 }
99                 o = units.getSuccOf( o );
100             }
101             if( insertFirst ) {
102                 units.insertBefore( list, units.getFirst() );
103             } else {
104                 units.insertAfter( list, o ) ;
105             }
106         } else {
107             */

108         {
109             units.insertBefore( list, units.getFirst() );
110         }
111         /*
112         ArrayList toRemove = new ArrayList();
113         for( Iterator sIt = units.iterator(r); sIt.hasNext(); ) {
114             final Stmt s = (Stmt) sIt.next();
115             if(s == r) continue;
116             toRemove.add(s);
117         }
118         for( Iterator sIt = toRemove.iterator(); sIt.hasNext(); ) {
119             final Stmt s = (Stmt) sIt.next();
120             units.getNonPatchingChain().remove(s);
121         }
122         body.getTraps().clear();
123         */

124     }
125 }
126
Popular Tags