KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > LocalGenerator


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 soot.*;
23 import java.util.*;
24
25 public class LocalGenerator{
26
27     private soot.Body body;
28     public LocalGenerator(Body b){
29           body = b;
30     }
31     
32     private boolean bodyContainsLocal(String JavaDoc name){
33         Iterator it = body.getLocals().iterator();
34         while (it.hasNext()){
35             if (((soot.Local)it.next()).getName().equals(name)) return true;
36         }
37         return false;
38     }
39     
40     /**
41      * generates a new soot local given the type
42      */

43     public soot.Local generateLocal(soot.Type type){
44         
45         String JavaDoc name = "v";
46         if (type instanceof soot.IntType) {
47             while (true){
48                 name = nextIntName();
49                 if (!bodyContainsLocal(name)) break;
50             }
51         }
52         else if (type instanceof soot.ByteType) {
53             while (true){
54                 name = nextByteName();
55                 if (!bodyContainsLocal(name)) break;
56             }
57         }
58         else if (type instanceof soot.ShortType) {
59             while (true){
60                 name = nextShortName();
61                 if (!bodyContainsLocal(name)) break;
62             }
63         }
64         else if (type instanceof soot.BooleanType) {
65             while (true){
66                 name = nextBooleanName();
67                 if (!bodyContainsLocal(name)) break;
68             }
69         }
70         else if (type instanceof soot.VoidType) {
71             while (true){
72                 name = nextVoidName();
73                 if (!bodyContainsLocal(name)) break;
74             }
75         }
76         else if (type instanceof soot.CharType) {
77             while (true){
78                 name = nextCharName();
79                 if (!bodyContainsLocal(name)) break;
80             }
81             type = soot.CharType.v();
82         }
83         else if (type instanceof soot.DoubleType) {
84             while (true){
85                 name = nextDoubleName();
86                 if (!bodyContainsLocal(name)) break;
87             }
88         }
89         else if (type instanceof soot.FloatType) {
90             while (true){
91                 name = nextFloatName();
92                 if (!bodyContainsLocal(name)) break;
93             }
94         }
95         else if (type instanceof soot.LongType) {
96             while (true){
97                 name = nextLongName();
98                 if (!bodyContainsLocal(name)) break;
99             }
100         }
101         else if (type instanceof soot.RefLikeType) {
102             while (true){
103                 name = nextRefLikeTypeName();
104                 if (!bodyContainsLocal(name)) break;
105             }
106         }
107         else {
108             throw new RuntimeException JavaDoc("Unhandled Type of Local variable to Generate - Not Implemented");
109         }
110         
111         return createLocal(name, type);
112         
113     }
114
115     private int tempInt = -1;
116     private int tempVoid = -1;
117     private int tempBoolean = -1;
118     private int tempLong = -1;
119     private int tempDouble = -1;
120     private int tempFloat = -1;
121     private int tempRefLikeType = -1;
122     private int tempByte = -1;
123     private int tempShort = -1;
124     private int tempChar = -1;
125     
126     private String JavaDoc nextIntName(){
127         tempInt++;
128         return "$i"+tempInt;
129     }
130
131     private String JavaDoc nextCharName(){
132         tempChar++;
133         return "$c"+tempChar;
134     }
135
136     private String JavaDoc nextVoidName(){
137         tempVoid++;
138         return "$v"+tempVoid;
139     }
140
141     private String JavaDoc nextByteName(){
142         tempByte++;
143         return "$b"+tempByte;
144     }
145
146     private String JavaDoc nextShortName(){
147         tempShort++;
148         return "$s"+tempShort;
149     }
150
151     private String JavaDoc nextBooleanName(){
152         tempBoolean++;
153         return "$z"+tempBoolean;
154     }
155
156     private String JavaDoc nextDoubleName(){
157         tempDouble++;
158         return "$d"+tempDouble;
159     }
160     
161     private String JavaDoc nextFloatName(){
162         tempFloat++;
163         return "$f"+tempFloat;
164     }
165
166     private String JavaDoc nextLongName(){
167         tempLong++;
168         return "$l"+tempLong;
169     }
170
171     private String JavaDoc nextRefLikeTypeName(){
172         tempRefLikeType++;
173         return "$r"+tempRefLikeType;
174     }
175     
176     // this should be used for generated locals only
177
private soot.Local createLocal(String JavaDoc name, soot.Type sootType) {
178         soot.Local sootLocal = soot.jimple.Jimple.v().newLocal(name, sootType);
179         body.getLocals().add(sootLocal);
180         return sootLocal;
181     }
182 }
183
Popular Tags