Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/chaoswg/xtc4y/classdesc/ConstantPool.java


1   //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/ConstantPool.java,v 1.2 2003/08/25 10:50:14 toggm Exp $
2   /******************************************************************************
3    * XTC4y - eXtreme Testing Collection 4 you                                   *
4    * -------------------------------------------------------------------------- *
5    * URL: http://www.chaoswg.com/xtc4y                                          *
6    * Author: Mike Toggweiler (2.dog@gmx.ch)                                     *
7    *                                                                            *
8    * Last Updated: $Date: 2003/08/25 10:50:14 $, by $Author: toggm $            *
9    * Version: $Revision: 1.2 $                                                  *
10   * -------------------------------------------------------------------------- *
11   * COPYRIGHT:   (c) 2003 by Mike Toggweiler                                   *
12   *                                                                            *
13   * This program is free software; you can redistribute it and/or modify       *
14   * it under the terms of the GNU General Public License as published by       *
15   * the Free Software Foundation; either version 2 of the License, or          *
16   * (at your option) any later version.                                        *
17   *****************************************************************************/
18  package com.chaoswg.xtc4y.classdesc;
19  
20  import java.util.Vector;
21  import java.util.Iterator;
22  
23  import java.io.DataInputStream;
24  import java.io.DataOutputStream;
25  import java.io.IOException;
26  
27  /**
28   * This class describes the constant pool in a class file. It is mainly 
29   * used internally
30   * The constant pool begins at index 1, therefore tries for storing data 
31   * at this position wan't succeed, a ArrayIndexOutofBoundException will be 
32   * thrown.
33   * It begins at index 1 so a reference to a cp entry at position 0 means no 
34   * reference.
35   * @author Mike Toggweiler
36   **/
37  public class ConstantPool {
38      private Vector poolEntries;
39      private ClassCPEntry thisClass;
40  
41      /**
42       * Create a new empty constant pool
43       **/
44      public ConstantPool() {
45    poolEntries = new Vector();
46    //add a default element at the position 0
47      }
48  
49      /**
50       * Creates and Initializes a constant pool from a DataInputStream
51       * @param dis the DataInputStream to read from
52       **/
53      protected ConstantPool(DataInputStream dis) throws IOException {
54    poolEntries = new Vector();
55    //constant_pool_count
56    int cpCount = dis.readUnsignedShort();  
57    
58    //cpinfos[constant_pool_count-1]
59    for (int i=0; i<cpCount-1;) {
60        ConstantPoolEntry cp = null;
61        byte tag = (byte)dis.readUnsignedByte();
62        switch (tag) {
63        case UTF8CPEntry.TAG:
64      cp = new UTF8CPEntry(dis);
65      break;
66        case IntegerCPEntry.TAG:
67      cp = new IntegerCPEntry(dis);
68      break;
69        case FloatCPEntry.TAG:
70      cp = new FloatCPEntry(dis);
71      break;
72        case LongCPEntry.TAG:
73      cp = new LongCPEntry(dis);
74      break;
75        case DoubleCPEntry.TAG:
76      cp = new DoubleCPEntry(dis);
77      break;    
78        case StringCPEntry.TAG:
79      cp = new StringCPEntry(dis);
80      break;    
81        case ClassCPEntry.TAG:
82      cp = new ClassCPEntry(dis);
83      break;
84        case FieldrefCPEntry.TAG:
85      cp = new FieldrefCPEntry(dis);
86      break;
87        case MethodrefCPEntry.TAG:
88      cp = new MethodrefCPEntry(dis);
89      break;
90        case InterfaceMethodrefCPEntry.TAG:
91      cp = new InterfaceMethodrefCPEntry(dis);
92      break;
93        case NameAndTypeCPEntry.TAG:
94      cp = new NameAndTypeCPEntry(dis);
95      break;
96        default:
97      throw new ClassFormatError("Constant pool tag unknown:"+tag);
98        }
99        int step = cp.getUsedCPSpace();            
100 
101       poolEntries.add(cp);
102       
103       for (int i2=1; i2<step; ++i2) {
104     //add dummy objects
105     poolEntries.add(new Object());
106       }      
107       i += step;      
108   }
109   int j=0;
110   //resolve references in cp entries
111   for (int i=0; i<poolEntries.size(); ++i) {
112       Object next = poolEntries.elementAt(i);
113       if (next instanceof ConstantPoolEntry) {    
114     ++j;
115     ((ConstantPoolEntry)next).resolve(this);
116       }
117   }
118     }
119 
120     /**
121      * Write the constant pool information into a DataOutputStream
122      * @param dos the DataOutputStream to write on     
123      **/
124     protected void write(DataOutputStream dos) throws IOException {
125   Object obj;
126   for (int i=0; i<poolEntries.size(); ++i) {
127       obj = poolEntries.elementAt(i);
128       if (obj instanceof ConstantPoolEntry) {
129     //used to remove dummy entries
130     ((ConstantPoolEntry)obj).write(dos, this);
131       }
132   }
133     }
134 
135     /**
136      * add a constant pool entry, if it already exists, just return an index
137      * on it
138      * @param entry cp entry to add
139      * @return the index of the current cp entry
140      **/
141     public short addCPEntry(ConstantPoolEntry entry) {  
142   if (poolEntries.contains(entry)) {
143       return (short)(poolEntries.indexOf(entry)+1);
144   }  
145   poolEntries.add(entry);
146   int index = poolEntries.size();
147 
148   int step = entry.getUsedCPSpace();            
149       
150   for (int i=1; i<step; ++i) {
151       //add dummy objects
152       poolEntries.add(new Object());
153   }        
154   return (short)index;
155     }
156 
157     /**
158      * @return an iterator of all cpentires
159      **/
160     public Iterator getCPEntries() {
161   Iterator it = poolEntries.iterator();
162   //move an element forward
163   it.next();
164   return it;
165     }
166 
167     /**
168      * @return a CPEntry at a given position
169      **/
170     public ConstantPoolEntry getCPEntryAt(int index) {
171   if (index <= 0 || index > poolEntries.size()) {
172       throw new ArrayIndexOutOfBoundsException(poolEntries.size()+":"+
173                  index);
174   }
175   return (ConstantPoolEntry)poolEntries.elementAt(index-1);
176     }
177 
178     /**
179      * @return the size of the constant pool
180      **/
181     public int getSize() {
182   return poolEntries.size();
183     }
184 
185     /**
186      * Get this class
187      **/
188     protected ClassCPEntry getThisClass() {
189   return thisClass;
190     }
191 
192     /**
193      * set this class
194      **/
195     protected void setThisClass(ClassCPEntry cl) {
196   thisClass = cl;
197     }
198 
199     /**
200      * Get a readable version of hte current constant pool
201      **/
202     public String toString() {
203   String ret = "ConstantPool:\nThisClass:"+thisClass+"\n";
204   for (int i=0; i<poolEntries.size(); ++i) {
205       Object obj = poolEntries.elementAt(i);
206       if (obj instanceof ConstantPoolEntry) {
207     ret += (i+1) + " " + obj + "\n";
208       }
209   }
210   return ret;
211     }
212 }