KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > SootField


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  * Copyright (C) 2004 Ondrej Lhotak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */

20
21 /*
22  * Modified by the Sable Research Group and others 1997-1999.
23  * See the 'credits' file distributed with Soot for the complete list of
24  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
25  */

26
27
28 package soot;
29
30 import soot.tagkit.*;
31 import soot.jimple.*;
32 import java.util.*;
33 import soot.util.*;
34 import soot.jimple.paddle.PaddleField;
35 import soot.jimple.spark.pag.SparkField;
36
37 /**
38     Soot representation of a Java field. Can be declared to belong to a SootClass.
39 */

40 public class SootField extends AbstractHost implements ClassMember, SparkField, Numberable, PaddleField
41 {
42     String JavaDoc name;
43     Type type;
44     int modifiers;
45
46     boolean isDeclared = false;
47     SootClass declaringClass;
48     boolean isPhantom = false;
49
50     /** Constructs a Soot field with the given name, type and modifiers. */
51     public SootField(String JavaDoc name, Type type, int modifiers)
52     {
53         this.name = name;
54         this.type = type;
55         this.modifiers = modifiers;
56         if( type instanceof RefLikeType ) Scene.v().getFieldNumberer().add(this);
57     }
58
59     /** Constructs a Soot field with the given name, type and no modifiers. */
60     public SootField(String JavaDoc name, Type type)
61     {
62         this.name = name;
63         this.type = type;
64         this.modifiers = 0;
65         if( type instanceof RefLikeType ) Scene.v().getFieldNumberer().add(this);
66     }
67
68     public int equivHashCode()
69     {
70         return type.hashCode() * 101 + modifiers * 17 + name.hashCode();
71     }
72
73     public String JavaDoc getName()
74     {
75         return name;
76     }
77
78     public String JavaDoc getSignature() {
79         return getSignature(declaringClass, getName(), getType());
80     }
81     public static String JavaDoc getSignature(SootClass cl, String JavaDoc name, Type type)
82     {
83         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84
85         buffer.append("<" + Scene.v().quotedNameOf(cl.getName()) + ": ");
86         buffer.append(type + " " + Scene.v().quotedNameOf(name) + ">");
87
88         return buffer.toString().intern();
89
90     }
91   
92     public String JavaDoc getSubSignature()
93     {
94         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
95         buffer.append(getType() + " " + Scene.v().quotedNameOf(getName()));
96         return buffer.toString().intern();
97     }
98     
99     public SootClass getDeclaringClass()
100     {
101         if(!isDeclared)
102             throw new RuntimeException JavaDoc("not declared: "+getName()+" "+getType());
103
104         return declaringClass;
105     }
106
107     public boolean isPhantom()
108     {
109         return isPhantom;
110     }
111     
112     public void setPhantom(boolean value)
113     {
114         if( value ) {
115             if( !Scene.v().allowsPhantomRefs() )
116                 throw new RuntimeException JavaDoc( "Phantom refs not allowed" );
117             if( declaringClass != null && !declaringClass.isPhantom() )
118                 throw new
119                     RuntimeException JavaDoc( "Declaring class would have to be phantom" );
120         }
121         isPhantom = value;
122     }
123
124     public boolean isDeclared()
125     {
126         return isDeclared;
127     }
128
129     public void setName(String JavaDoc name)
130     {
131         this.name = name;
132     }
133
134     public Type getType()
135     {
136         return type;
137     }
138
139     public void setType(Type t)
140     {
141         this.type = t;
142     }
143
144     /**
145      * Convenience method returning true if this field is public.
146      */

147     public boolean isPublic()
148     {
149         return Modifier.isPublic(this.getModifiers());
150     }
151
152     /**
153      * Convenience method returning true if this field is protected.
154      */

155     public boolean isProtected()
156     {
157         return Modifier.isProtected(this.getModifiers());
158     }
159
160     /**
161      * Convenience method returning true if this field is private.
162      */

163     public boolean isPrivate()
164     {
165         return Modifier.isPrivate(this.getModifiers());
166     }
167
168     /**
169      * Convenience method returning true if this field is static.
170      */

171     public boolean isStatic()
172     {
173         return Modifier.isStatic(this.getModifiers());
174     }
175
176     /**
177      * Convenience method returning true if this field is final.
178      */

179     public boolean isFinal()
180     {
181         return Modifier.isFinal(this.getModifiers());
182     }
183
184
185
186     public void setModifiers(int modifiers)
187     {
188         if (!declaringClass.isApplicationClass())
189             throw new RuntimeException JavaDoc("Cannot set modifiers of a field from a non-app class!");
190             
191         this.modifiers = modifiers;
192     }
193
194     public int getModifiers()
195     {
196         return modifiers;
197     }
198
199     public String JavaDoc toString()
200     {
201         return getSignature();
202     }
203
204
205     private String JavaDoc getOriginalStyleDeclaration()
206     {
207         String JavaDoc qualifiers = Modifier.toString(modifiers) + " " + type.toString();
208         qualifiers = qualifiers.trim();
209
210         if(qualifiers.equals(""))
211             return Scene.v().quotedNameOf(name);
212         else
213             return qualifiers + " " + Scene.v().quotedNameOf(name) + "";
214
215     }
216
217
218     public String JavaDoc getDeclaration()
219     {
220         return getOriginalStyleDeclaration();
221     }
222     public final int getNumber() {
223         return number;
224     }
225     public final void setNumber(int number) {
226         this.number = number;
227     }
228     private int number = 0;
229     public SootFieldRef makeRef() {
230         return Scene.v().makeFieldRef(declaringClass, name, type, isStatic());
231     }
232 }
233
234
235
236
237
238
239
Popular Tags