001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: * Copyright (C) 2004 Ondrej Lhotak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
018: * Boston, MA 02111-1307, USA.
019: */
020:
021: /*
022: * Modified by the Sable Research Group and others 1997-1999.
023: * See the 'credits' file distributed with Soot for the complete list of
024: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
025: */
026:
027: package soot;
028:
029: import soot.tagkit.*;
030: import soot.util.*;
031: import soot.jimple.paddle.PaddleField;
032: import soot.jimple.spark.pag.SparkField;
033:
034: /**
035: Soot representation of a Java field. Can be declared to belong to a SootClass.
036: */
037: public class SootField extends AbstractHost implements ClassMember,
038: SparkField, Numberable, PaddleField {
039: String name;
040: Type type;
041: int modifiers;
042:
043: boolean isDeclared = false;
044: SootClass declaringClass;
045: boolean isPhantom = false;
046:
047: /** Constructs a Soot field with the given name, type and modifiers. */
048: public SootField(String name, Type type, int modifiers) {
049: this .name = name;
050: this .type = type;
051: this .modifiers = modifiers;
052: if (type instanceof RefLikeType)
053: Scene.v().getFieldNumberer().add(this );
054: }
055:
056: /** Constructs a Soot field with the given name, type and no modifiers. */
057: public SootField(String name, Type type) {
058: this .name = name;
059: this .type = type;
060: this .modifiers = 0;
061: if (type instanceof RefLikeType)
062: Scene.v().getFieldNumberer().add(this );
063: }
064:
065: public int equivHashCode() {
066: return type.hashCode() * 101 + modifiers * 17 + name.hashCode();
067: }
068:
069: public String getName() {
070: return name;
071: }
072:
073: public String getSignature() {
074: return getSignature(declaringClass, getName(), getType());
075: }
076:
077: public static String getSignature(SootClass cl, String name,
078: Type type) {
079: StringBuffer buffer = new StringBuffer();
080:
081: buffer
082: .append("<" + Scene.v().quotedNameOf(cl.getName())
083: + ": ");
084: buffer.append(type + " " + Scene.v().quotedNameOf(name) + ">");
085:
086: return buffer.toString().intern();
087:
088: }
089:
090: public String getSubSignature() {
091: StringBuffer buffer = new StringBuffer();
092: buffer.append(getType() + " "
093: + Scene.v().quotedNameOf(getName()));
094: return buffer.toString().intern();
095: }
096:
097: public SootClass getDeclaringClass() {
098: if (!isDeclared)
099: throw new RuntimeException("not declared: " + getName()
100: + " " + getType());
101:
102: return declaringClass;
103: }
104:
105: public boolean isPhantom() {
106: return isPhantom;
107: }
108:
109: public void setPhantom(boolean value) {
110: if (value) {
111: if (!Scene.v().allowsPhantomRefs())
112: throw new RuntimeException("Phantom refs not allowed");
113: if (declaringClass != null && !declaringClass.isPhantom())
114: throw new RuntimeException(
115: "Declaring class would have to be phantom");
116: }
117: isPhantom = value;
118: }
119:
120: public boolean isDeclared() {
121: return isDeclared;
122: }
123:
124: public void setName(String name) {
125: this .name = name;
126: }
127:
128: public Type getType() {
129: return type;
130: }
131:
132: public void setType(Type t) {
133: this .type = t;
134: }
135:
136: /**
137: * Convenience method returning true if this field is public.
138: */
139: public boolean isPublic() {
140: return Modifier.isPublic(this .getModifiers());
141: }
142:
143: /**
144: * Convenience method returning true if this field is protected.
145: */
146: public boolean isProtected() {
147: return Modifier.isProtected(this .getModifiers());
148: }
149:
150: /**
151: * Convenience method returning true if this field is private.
152: */
153: public boolean isPrivate() {
154: return Modifier.isPrivate(this .getModifiers());
155: }
156:
157: /**
158: * Convenience method returning true if this field is static.
159: */
160: public boolean isStatic() {
161: return Modifier.isStatic(this .getModifiers());
162: }
163:
164: /**
165: * Convenience method returning true if this field is final.
166: */
167: public boolean isFinal() {
168: return Modifier.isFinal(this .getModifiers());
169: }
170:
171: public void setModifiers(int modifiers) {
172: if (!declaringClass.isApplicationClass())
173: throw new RuntimeException(
174: "Cannot set modifiers of a field from a non-app class!");
175:
176: this .modifiers = modifiers;
177: }
178:
179: public int getModifiers() {
180: return modifiers;
181: }
182:
183: public String toString() {
184: return getSignature();
185: }
186:
187: private String getOriginalStyleDeclaration() {
188: String qualifiers = Modifier.toString(modifiers) + " "
189: + type.toString();
190: qualifiers = qualifiers.trim();
191:
192: if (qualifiers.equals(""))
193: return Scene.v().quotedNameOf(name);
194: else
195: return qualifiers + " " + Scene.v().quotedNameOf(name) + "";
196:
197: }
198:
199: public String getDeclaration() {
200: return getOriginalStyleDeclaration();
201: }
202:
203: public final int getNumber() {
204: return number;
205: }
206:
207: public final void setNumber(int number) {
208: this .number = number;
209: }
210:
211: private int number = 0;
212:
213: public SootFieldRef makeRef() {
214: return Scene.v().makeFieldRef(declaringClass, name, type,
215: isStatic());
216: }
217: }
|