KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > PrivateMethodAccMethodSource


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.javaToJimple;
21
22 import java.util.*;
23 public class PrivateMethodAccMethodSource implements soot.MethodSource {
24
25     public PrivateMethodAccMethodSource(polyglot.types.MethodInstance methInst){
26         this.methodInst = methInst;
27     }
28     private polyglot.types.MethodInstance methodInst;
29
30     public void setMethodInst(polyglot.types.MethodInstance mi) {
31         methodInst = mi;
32     }
33     
34     private boolean isCallParamType(soot.Type sootType) {
35         Iterator it = methodInst.formalTypes().iterator();
36         while (it.hasNext()) {
37             soot.Type compareType = Util.getSootType((polyglot.types.Type)it.next());
38             if (compareType.equals(sootType)) return true;
39         }
40         return false;
41     }
42     
43     public soot.Body getBody(soot.SootMethod sootMethod, String JavaDoc phaseName){
44            
45         soot.Body body = soot.jimple.Jimple.v().newBody(sootMethod);
46         LocalGenerator lg = new LocalGenerator(body);
47         
48         soot.Local base = null;
49         ArrayList methParams = new ArrayList();
50         ArrayList methParamsTypes = new ArrayList();
51         // create parameters
52
Iterator paramIt = sootMethod.getParameterTypes().iterator();
53         int paramCounter = 0;
54         while (paramIt.hasNext()) {
55             soot.Type sootType = (soot.Type)paramIt.next();
56             soot.Local paramLocal = lg.generateLocal(sootType);
57             //body.getLocals().add(paramLocal);
58
soot.jimple.ParameterRef paramRef = soot.jimple.Jimple.v().newParameterRef(sootType, paramCounter);
59             soot.jimple.Stmt stmt = soot.jimple.Jimple.v().newIdentityStmt(paramLocal, paramRef);
60             body.getUnits().add(stmt);
61             if (!isCallParamType(sootType)){
62                 base = paramLocal;
63             }
64             else {
65                 methParams.add(paramLocal);
66                 methParamsTypes.add(paramLocal.getType());
67             }
68             paramCounter++;
69         }
70         
71         // create return type local
72
soot.Type type = Util.getSootType(methodInst.returnType());
73         
74         soot.Local returnLocal = null;
75         if (!(type instanceof soot.VoidType)){
76             returnLocal = lg.generateLocal(type);
77             //body.getLocals().add(returnLocal);
78
}
79
80         // assign local to meth
81
soot.SootMethodRef meth = soot.Scene.v().makeMethodRef(((soot.RefType)Util.getSootType(methodInst.container())).getSootClass(), methodInst.name(), methParamsTypes, Util.getSootType(methodInst.returnType()), methodInst.flags().isStatic());
82
83         soot.jimple.InvokeExpr invoke = null;
84         if (methodInst.flags().isStatic()) {
85             invoke = soot.jimple.Jimple.v().newStaticInvokeExpr(meth, methParams);
86         }
87         else {
88             invoke = soot.jimple.Jimple.v().newSpecialInvokeExpr(base, meth, methParams);
89         }
90
91         soot.jimple.Stmt stmt = null;
92         if (!(type instanceof soot.VoidType)){
93             stmt = soot.jimple.Jimple.v().newAssignStmt(returnLocal, invoke);
94         }
95         else{
96             stmt = soot.jimple.Jimple.v().newInvokeStmt(invoke);
97         }
98         body.getUnits().add(stmt);
99
100         //return local
101
soot.jimple.Stmt retStmt = null;
102         if (!(type instanceof soot.VoidType)) {
103             retStmt = soot.jimple.Jimple.v().newReturnStmt(returnLocal);
104         }
105         else {
106             retStmt = soot.jimple.Jimple.v().newReturnVoidStmt();
107         }
108         body.getUnits().add(retStmt);
109         
110         return body;
111      
112     }
113     
114
115 }
116
Popular Tags