KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > PrivateFieldSetMethodSource


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