KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > PrivateFieldAccMethodSource


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
24 import soot.SootFieldRef;
25 public class PrivateFieldAccMethodSource implements soot.MethodSource {
26
27     private final soot.Type fieldType;
28     private final String JavaDoc fieldName;
29     private final boolean isStatic;
30     private final soot.SootClass classToInvoke;
31     
32     public PrivateFieldAccMethodSource(soot.Type fieldType, String JavaDoc fieldName, boolean isStatic, soot.SootClass classToInvoke ) {
33         this.fieldType = fieldType;
34         this.fieldName = fieldName;
35         this.isStatic = isStatic;
36         this.classToInvoke = classToInvoke;
37     }
38     
39  
40     public soot.Body getBody(soot.SootMethod sootMethod, String JavaDoc phaseName){
41         
42         soot.Body body = soot.jimple.Jimple.v().newBody(sootMethod);
43         LocalGenerator lg = new LocalGenerator(body);
44         
45         soot.Local fieldBase = null;
46         // create parameters
47
Iterator paramIt = sootMethod.getParameterTypes().iterator();
48         while (paramIt.hasNext()) {
49             soot.Type sootType = (soot.Type)paramIt.next();
50             soot.Local paramLocal = lg.generateLocal(sootType);
51             
52             soot.jimple.ParameterRef paramRef = soot.jimple.Jimple.v().newParameterRef(sootType, 0);
53             soot.jimple.Stmt stmt = soot.jimple.Jimple.v().newIdentityStmt(paramLocal, paramRef);
54             body.getUnits().add(stmt);
55             fieldBase = paramLocal;
56         }
57         
58         // create field type local
59
soot.Local fieldLocal = lg.generateLocal(fieldType);
60         // assign local to fieldRef
61
soot.SootFieldRef field = soot.Scene.v().makeFieldRef( classToInvoke, fieldName, fieldType, isStatic);
62
63         soot.jimple.FieldRef fieldRef = null;
64         if (isStatic) {
65             fieldRef = soot.jimple.Jimple.v().newStaticFieldRef(field);
66         }
67         else {
68             fieldRef = soot.jimple.Jimple.v().newInstanceFieldRef(fieldBase, field);
69         }
70         soot.jimple.AssignStmt assign = soot.jimple.Jimple.v().newAssignStmt(fieldLocal, fieldRef);
71         body.getUnits().add(assign);
72
73         //return local
74
soot.jimple.Stmt retStmt = soot.jimple.Jimple.v().newReturnStmt(fieldLocal);
75         body.getUnits().add(retStmt);
76         
77         return body;
78      
79     }
80 }
81
Popular Tags