KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > cp_info


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33 import soot.*;
34
35 import java.io.*;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.NoSuchElementException JavaDoc;
38
39 /** Base abstract class for constant pool entries; includes some utility methods.
40  * @see ClassFile#constant_pool
41  * @author Clark Verbrugge
42  */

43 abstract class cp_info {
44
45    public static final byte CONSTANT_Utf8 = 1;
46    public static final byte CONSTANT_Integer = 3;
47    public static final byte CONSTANT_Float = 4;
48    public static final byte CONSTANT_Long = 5;
49    public static final byte CONSTANT_Double = 6;
50    public static final byte CONSTANT_Class = 7;
51    public static final byte CONSTANT_String = 8;
52    public static final byte CONSTANT_Fieldref = 9;
53    public static final byte CONSTANT_Methodref = 10;
54    public static final byte CONSTANT_InterfaceMethodref = 11;
55    public static final byte CONSTANT_NameAndType = 12;
56
57    /** One of the CONSTANT_* constants. */
58    public byte tag;
59
60    /** Returns the size of this entry.
61     * @return size (in bytes) of this entry.
62     */

63    public abstract int size();
64
65    /** Returns a String representation of this entry.
66     * @param constant_pool constant pool of ClassFile.
67     * @return String representation of this entry.
68     */

69    public abstract String JavaDoc toString(cp_info constant_pool[]);
70
71    /** Returns a String description of what kind of entry this is.
72     * @return String representation of this kind of entry.
73     */

74    public abstract String JavaDoc typeName();
75
76    /** Compares this entry with another cp_info object (which may reside
77     * in a different constant pool).
78     * @param constant_pool constant pool of ClassFile for this.
79     * @param cp constant pool entry to compare against.
80     * @param cp_constant_pool constant pool of ClassFile for cp.
81     * @return a value <0, 0, or >0 indicating whether this is smaller,
82     * the same or larger than cp.
83     */

84    public abstract int compareTo(cp_info constant_pool[],cp_info cp,
85                                  cp_info cp_constant_pool[]);
86
87
88    /** Utility method, converts two integers into a single long.
89     * @param high upper 32 bits of the long.
90     * @param low lower 32 bits of the long.
91     * @return a long value composed from the two ints.
92     */

93    public static long ints2long(long high,long low) {
94       long h,l;
95       h = high; l = low;
96       return ((h<<32) + l);
97    }
98
99    /** Utility method, returns a String binary representation of the
100     * given integer.
101     * @param i the integer in question.
102     * @return a String of 0's and 1's.
103     * @see cp_info#printBits(long)
104     */

105    public static String JavaDoc printBits(int i) {
106       String JavaDoc s = "";
107       int j,k;
108       k = 1;
109       for (j=0;j<32; j++) {
110          if ((i&k)!=0)
111             s = "1" + s;
112          else
113             s = "0" + s;
114          k = k<<1;
115       }
116       return s;
117    }
118
119    /** Utility method, returns a String binary representation of the
120     * given long.
121     * @param i the long in question.
122     * @return a String of 0's and 1's.
123     * @see cp_info#printBits(int)
124     */

125    public static String JavaDoc printBits(long i) {
126       String JavaDoc s = "";
127       long j,k;
128       k = 1;
129       for (j=0;j<64; j++) {
130          if ((i&k)!=0)
131             s = "1" + s;
132          else
133             s = "0" + s;
134          k = k<<1;
135       }
136       return s;
137    }
138
139    /** Locates the name of the corresponding class, given the constant
140     * pool index of either a CONSTANT_Class, _Fieldref, Methodref or
141     * InterfaceMethodref.
142     * @param constant_pool constant pool of ClassFile.
143     * @param i index of cp_info entry in question.
144     * @return name of the associated class.
145     */

146    public static String JavaDoc getClassname(cp_info constant_pool[],int i) {
147       cp_info c = constant_pool[i];
148       switch (c.tag) {
149       case cp_info.CONSTANT_Class:
150          return c.toString(constant_pool);
151       case cp_info.CONSTANT_Fieldref:
152          return getClassname(constant_pool,((CONSTANT_Fieldref_info)c).class_index);
153       case cp_info.CONSTANT_Methodref:
154          return getClassname(constant_pool,((CONSTANT_Methodref_info)c).class_index);
155       case cp_info.CONSTANT_InterfaceMethodref:
156          return getClassname(constant_pool,
157                              ((CONSTANT_InterfaceMethodref_info)c).class_index);
158       }
159       G.v().out.println("Request for classname for non-class object!");
160       return "Can't find classname. Sorry.";
161    }
162
163    /** Returns the name of the given constant pool object, assuming it is
164     * of type CONSTANT_NameAndType, _FieldRef, _Methodref or _InterfaceMethodref.
165     * @param constant_pool constant pool of ClassFile.
166     * @param i index of cp_info entry in question.
167     * @return name of the associated object.
168     * @see CONSTANT_Utf8_info
169     */

170    public static String JavaDoc getName(cp_info constant_pool[],int i) {
171       cp_info c = constant_pool[i];
172       switch (c.tag) {
173       case cp_info.CONSTANT_Utf8:
174          return c.toString(constant_pool);
175       case cp_info.CONSTANT_NameAndType:
176          return getName(constant_pool,((CONSTANT_NameAndType_info)c).name_index);
177       case cp_info.CONSTANT_Fieldref:
178          return getName(constant_pool,((CONSTANT_Fieldref_info)c).name_and_type_index);
179       case cp_info.CONSTANT_Methodref:
180          return getName(constant_pool,((CONSTANT_Methodref_info)c).name_and_type_index);
181       case cp_info.CONSTANT_InterfaceMethodref:
182          return getName(constant_pool,
183                         ((CONSTANT_InterfaceMethodref_info)c).name_and_type_index);
184       }
185       G.v().out.println("Request for name for non-named object!");
186       return "Can't find name of that object. Sorry.";
187    }
188
189    /** Counts the number of parameters of the given method.
190     * @param constant_pool constant pool of ClassFile.
191     * @param m a constant pool index as accepted by getTypeDescr.
192     * @return the number of parameters.
193     * @see cp_info#getTypeDescr
194     */

195    public static int countParams(cp_info constant_pool[],int m) {
196       StringTokenizer JavaDoc st;
197       String JavaDoc s = getTypeDescr(constant_pool,m);
198       s = ClassFile.parseMethodDesc_params(s);
199       st = new StringTokenizer JavaDoc(s,",",false);
200       return st.countTokens();
201    }
202
203    /** Returns the type descriptor for the given constant pool
204     * object, which must be a CONSTANT_Utf8, CONSTANT_NameAndType,
205     * CONSTANT_Fieldref, CONSTANT_MethodRef, or CONSTANT_InterfaceMethodRef.
206     * @param constant_pool constant pool of ClassFile.
207     * @param i a constant pool index for an entry of type CONSTANT_Utf8,
208     * CONSTANT_NameAndType, CONSTANT_MethodRef, or CONSTANT_InterfaceMethodRef.
209     * @return the type descriptor.
210     * @see CONSTANT_Utf8_info
211     */

212    public static String JavaDoc getTypeDescr(cp_info constant_pool[],int i) {
213       cp_info c = constant_pool[i];
214       if (c instanceof CONSTANT_Utf8_info)
215          return c.toString(constant_pool);
216       if (c instanceof CONSTANT_NameAndType_info)
217          return getTypeDescr(constant_pool,
218                              ((CONSTANT_NameAndType_info)c).descriptor_index);
219       if (c instanceof CONSTANT_Methodref_info)
220          return getTypeDescr(constant_pool,
221                              ((CONSTANT_Methodref_info)c).name_and_type_index);
222       if (c instanceof CONSTANT_InterfaceMethodref_info)
223          return getTypeDescr(constant_pool,
224                              ((CONSTANT_InterfaceMethodref_info)c).name_and_type_index);
225       if (c instanceof CONSTANT_Fieldref_info)
226          return getTypeDescr(constant_pool,
227                              ((CONSTANT_Fieldref_info)c).name_and_type_index);
228       G.v().out.println("Invalid request for type descr!");
229       return "Invalid type descriptor request.";
230    }
231
232    /** Returns the name of the field type of the given constant pool object.
233     * @param constant_pool constant pool of ClassFile.
234     * @param i a constant pool index for an entry of type CONSTANT_Utf8,
235     * CONSTANT_NameAndType, or CONSTANT_FieldRef.
236     * @return the type of the field.
237     * @see CONSTANT_Utf8_info
238     * @see cp_info#getTypeDescr
239     */

240    public static String JavaDoc fieldType(cp_info constant_pool[],int i) {
241       return ClassFile.parseDesc(getTypeDescr(constant_pool,i),"");
242    }
243 }
244
Popular Tags