001: /*
002: * @(#)ConstantPool.java 1.9 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package com.sun.xlet.ixc;
029:
030: import java.util.Enumeration;
031: import java.util.Hashtable;
032: import java.util.Vector;
033: import java.io.DataOutputStream;
034: import java.io.IOException;
035:
036: class ConstantPool {
037: // We write the CP out in this order: classes, methods, fields,
038: // nameAndType, strings.
039: private Hashtable classes = new Hashtable(11);
040: // Hashtable<String, String>, becomes Hashtable<String, Integer>
041: // on write
042: private Vector fields = new Vector(); // Vector<String[3]>
043: private Vector methods = new Vector(); // Vector<String[3]>
044: private Vector ifMethods = new Vector(); // Interface methods
045: private Vector nameAndType = new Vector(); // Vector<String[2]>A
046: private Hashtable stringConstants = new Hashtable(11);
047: // This is the list of CONSTANT_String_info things
048: // Hashtable<String, String>, becomes Hashtable<String, Integer>
049: // on write
050: private Hashtable strings = new Hashtable(51);
051:
052: // Hashtable<String, String>, becomes Hashtable<String, Integer>
053: // on write
054:
055: void addStringConstant(String s) {
056: addString(s);
057: stringConstants.put(s, s);
058: }
059:
060: void addString(String s) {
061: strings.put(s, s);
062: }
063:
064: void addClass(String s) {
065: addString(s);
066: classes.put(s, s);
067: }
068:
069: boolean hasClass(String s) {
070: return classes.get(s) != null;
071: }
072:
073: private void addNameAndType(String name, String type) {
074: addString(name);
075: addString(type);
076: nameAndType.addElement(new String[] { name, type });
077: }
078:
079: int lookupString(Object s) {
080: Integer i = (Integer) strings.get(s);
081: if (i == null) {
082: throw new RuntimeException("" + s
083: + " not found in constant pool");
084: }
085: return i.intValue();
086: }
087:
088: int lookupStringConstant(Object s) {
089: Integer i = (Integer) stringConstants.get(s);
090: if (i == null) {
091: throw new RuntimeException("string constant " + s
092: + " not found in constant pool");
093: }
094: return i.intValue();
095: }
096:
097: int lookupClass(Object s) {
098: Integer i = (Integer) classes.get(s);
099: if (i == null) {
100: throw new RuntimeException("class " + s
101: + " not found in constant pool");
102: }
103: return i.intValue();
104: }
105:
106: int lookupMethod(String className, String name, String type) {
107: for (int i = 0; i < methods.size(); i++) {
108: String[] el = (String[]) methods.elementAt(i);
109: if (className.equals(el[0]) && name.equals(el[1])
110: && type.equals(el[2])) {
111: return 1 + classes.size() + fields.size() + i;
112: }
113: }
114: throw new RuntimeException("Method <" + className + ". " + name
115: + " : " + type + "> not found in constant pool");
116: }
117:
118: int lookupField(String className, String name, String type) {
119: for (int i = 0; i < fields.size(); i++) {
120: String[] el = (String[]) fields.elementAt(i);
121: if (className.equals(el[0]) && name.equals(el[1])
122: && type.equals(el[2])) {
123: return 1 + classes.size() + i;
124: }
125: }
126: throw new RuntimeException("Field <" + className + ". " + name
127: + " : " + type + "> not found in constant pool");
128: }
129:
130: int lookupNameAndType(String name, String type) {
131: for (int i = 0; i < nameAndType.size(); i++) {
132: String[] el = (String[]) nameAndType.elementAt(i);
133: if (name.equals(el[0]) && type.equals(el[1])) {
134: return 1 + classes.size() + fields.size()
135: + methods.size() + ifMethods.size() + i;
136: }
137: }
138: throw new RuntimeException("Name and type <" + name + ", "
139: + type + "> not found in constant pool");
140: }
141:
142: void addField(String className, String name, String type) {
143: addString(className);
144: addNameAndType(name, type);
145: fields.addElement(new String[] { className, name, type });
146: }
147:
148: void addMethodReference(String className, String name, String type) {
149: addString(className);
150: addNameAndType(name, type);
151: methods.addElement(new String[] { className, name, type });
152: }
153:
154: //
155: // Add an interface method reference
156: //
157: void addIfMethodReference(String className, String name, String type) {
158: addString(className);
159: addNameAndType(name, type);
160: ifMethods.addElement(new String[] { className, name, type });
161: }
162:
163: void write(DataOutputStream dos) throws IOException {
164: // Assign an entry number to each class, stringConstant and string
165: // CP entry. Remmeber, counting starts at 1!
166:
167: int num = 1;
168: String[] classesArr = new String[classes.size()];
169: {
170: int i = 0;
171: for (Enumeration e = classes.keys(); e.hasMoreElements();) {
172: String key = (String) e.nextElement();
173: classesArr[i++] = key;
174: classes.put(key, new Integer(num++));
175: }
176: }
177: num += fields.size() + methods.size() + ifMethods.size()
178: + nameAndType.size();
179: String[] stringConstantArr = new String[stringConstants.size()];
180: {
181: int i = 0;
182: for (Enumeration e = stringConstants.keys(); e
183: .hasMoreElements();) {
184: String key = (String) e.nextElement();
185: stringConstantArr[i++] = key;
186: stringConstants.put(key, new Integer(num++));
187: }
188: }
189: String[] stringArr = new String[strings.size()];
190: {
191: int i = 0;
192: for (Enumeration e = strings.keys(); e.hasMoreElements();) {
193: String key = (String) e.nextElement();
194: stringArr[i++] = key;
195: strings.put(key, new Integer(num++));
196: }
197: }
198: // Now num is the correct constant_pool_count, one greater than
199: // the number of entries. Yes, entires really are numbered
200: // 1..(constant_pool_count-1)!
201: dos.writeShort(num);
202: for (int i = 0; i < classesArr.length; i++) {
203: dos.writeByte(7); // CONSTANT_Class
204: dos.writeShort(lookupString(classesArr[i]));
205: }
206: for (int i = 0; i < fields.size(); i++) {
207: String[] sa = (String[]) fields.elementAt(i);
208: dos.writeByte(9); // CONSTANT_Fieldref
209: dos.writeShort(lookupClass(sa[0]));
210: dos.writeShort(lookupNameAndType(sa[1], sa[2]));
211: }
212: for (int i = 0; i < methods.size(); i++) {
213: String[] sa = (String[]) methods.elementAt(i);
214: dos.writeByte(10); // CONSTANT_Methodref
215: dos.writeShort(lookupClass(sa[0]));
216: dos.writeShort(lookupNameAndType(sa[1], sa[2]));
217: }
218: for (int i = 0; i < ifMethods.size(); i++) {
219: String[] sa = (String[]) ifMethods.elementAt(i);
220: dos.writeByte(11); // CONSTANT_InterfaceMethodref
221: dos.writeShort(lookupClass(sa[0]));
222: dos.writeShort(lookupNameAndType(sa[1], sa[2]));
223: }
224: for (int i = 0; i < nameAndType.size(); i++) {
225: String[] sa = (String[]) nameAndType.elementAt(i);
226: dos.writeByte(12); // CONSTANT_NameAndtype
227: dos.writeShort(lookupString(sa[0]));
228: dos.writeShort(lookupString(sa[1]));
229: }
230: for (int i = 0; i < stringConstantArr.length; i++) {
231: dos.writeByte(0x8); // CONSTANT_String_info
232: dos.writeShort(lookupString(stringConstantArr[i]));
233: }
234: for (int i = 0; i < stringArr.length; i++) {
235: dos.writeByte(1); // CONSTANT_Utf8
236: dos.writeUTF(stringArr[i]);
237: }
238: }
239: }
|