View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class represents the constant pool, i.e., a table of constants, of
28   * a parsed classfile. It may contain null references, due to the JVM
29   * specification that skips an entry after an 8-byte constant (double,
30   * long) entry.  Those interested in generating constant pools
31   * programatically should see <a href="../generic/ConstantPoolGen.html">
32   * ConstantPoolGen</a>.
33  
34   * @see     Constant
35   * @see     org.apache.bcel.generic.ConstantPoolGen
36   */
37  public class ConstantPool implements Cloneable, Node {
38  
39      private Constant[] constantPool;
40  
41      /**
42       * @param constantPool Array of constants
43       */
44      public ConstantPool(final Constant[] constantPool) {
45          this.constantPool = constantPool;
46      }
47  
48      /**
49       * Reads constants from given input stream.
50       *
51       * @param input Input stream
52       * @throws IOException
53       * @throws ClassFormatException
54       */
55      public ConstantPool(final DataInput input) throws IOException, ClassFormatException {
56          byte tag;
57          final int constant_pool_count = input.readUnsignedShort();
58          constantPool = new Constant[constant_pool_count];
59          /* constantPool[0] is unused by the compiler and may be used freely
60           * by the implementation.
61           */
62          for (int i = 1; i < constant_pool_count; i++) {
63              constantPool[i] = Constant.readConstant(input);
64              /* Quote from the JVM specification:
65               * "All eight byte constants take up two spots in the constant pool.
66               * If this is the n'th byte in the constant pool, then the next item
67               * will be numbered n+2"
68               *
69               * Thus we have to increment the index counter.
70               */
71              tag = constantPool[i].getTag();
72              if ((tag == Const.CONSTANT_Double) || (tag == Const.CONSTANT_Long)) {
73                  i++;
74              }
75          }
76      }
77  
78      /**
79       * Called by objects that are traversing the nodes of the tree implicitely
80       * defined by the contents of a Java class. I.e., the hierarchy of methods,
81       * fields, attributes, etc. spawns a tree of objects.
82       *
83       * @param v Visitor object
84       */
85      @Override
86      public void accept( final Visitor v ) {
87          v.visitConstantPool(this);
88      }
89  
90      /**
91       * Resolves constant to a string representation.
92       *
93       * @param  c Constant to be printed
94       * @return String representation
95       */
96      public String constantToString( Constant c ) throws ClassFormatException {
97          String str;
98          int i;
99          final byte tag = c.getTag();
100         switch (tag) {
101             case Const.CONSTANT_Class:
102                 i = ((ConstantClass) c).getNameIndex();
103                 c = getConstant(i, Const.CONSTANT_Utf8);
104                 str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
105                 break;
106             case Const.CONSTANT_String:
107                 i = ((ConstantString) c).getStringIndex();
108                 c = getConstant(i, Const.CONSTANT_Utf8);
109                 str = "\"" + escape(((ConstantUtf8) c).getBytes()) + "\"";
110                 break;
111             case Const.CONSTANT_Utf8:
112                 str = ((ConstantUtf8) c).getBytes();
113                 break;
114             case Const.CONSTANT_Double:
115                 str = String.valueOf(((ConstantDouble) c).getBytes());
116                 break;
117             case Const.CONSTANT_Float:
118                 str = String.valueOf(((ConstantFloat) c).getBytes());
119                 break;
120             case Const.CONSTANT_Long:
121                 str = String.valueOf(((ConstantLong) c).getBytes());
122                 break;
123             case Const.CONSTANT_Integer:
124                 str = String.valueOf(((ConstantInteger) c).getBytes());
125                 break;
126             case Const.CONSTANT_NameAndType:
127                 str = constantToString(((ConstantNameAndType) c).getNameIndex(),
128                         Const.CONSTANT_Utf8)
129                         + " " + constantToString(((ConstantNameAndType) c).getSignatureIndex(),
130                         Const.CONSTANT_Utf8);
131                 break;
132             case Const.CONSTANT_InterfaceMethodref:
133             case Const.CONSTANT_Methodref:
134             case Const.CONSTANT_Fieldref:
135                 str = constantToString(((ConstantCP) c).getClassIndex(), Const.CONSTANT_Class)
136                         + "." + constantToString(((ConstantCP) c).getNameAndTypeIndex(),
137                         Const.CONSTANT_NameAndType);
138                 break;
139             case Const.CONSTANT_MethodHandle:
140                 // Note that the ReferenceIndex may point to a Fieldref, Methodref or
141                 // InterfaceMethodref - so we need to peek ahead to get the actual type.
142                 final ConstantMethodHandle/org/apache/bcel/classfile/ConstantMethodHandle.html#ConstantMethodHandle">ConstantMethodHandle cmh = (ConstantMethodHandle) c;
143                 str = Const.getMethodHandleName(cmh.getReferenceKind())
144                         + " " + constantToString(cmh.getReferenceIndex(),
145                         getConstant(cmh.getReferenceIndex()).getTag());
146                 break;
147             case Const.CONSTANT_MethodType:
148                 final ConstantMethodType../org/apache/bcel/classfile/ConstantMethodType.html#ConstantMethodType">ConstantMethodType cmt = (ConstantMethodType) c;
149                 str = constantToString(cmt.getDescriptorIndex(), Const.CONSTANT_Utf8);
150                 break;
151             case Const.CONSTANT_InvokeDynamic:
152                 final ConstantInvokeDynamicorg/apache/bcel/classfile/ConstantInvokeDynamic.html#ConstantInvokeDynamic">ConstantInvokeDynamic cid = (ConstantInvokeDynamic) c;
153                 str = cid.getBootstrapMethodAttrIndex()
154                         + ":" + constantToString(cid.getNameAndTypeIndex(),
155                         Const.CONSTANT_NameAndType);
156                 break;
157             case Const.CONSTANT_Module:
158                 i = ((ConstantModule) c).getNameIndex();
159                 c = getConstant(i, Const.CONSTANT_Utf8);
160                 str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
161                 break;
162             case Const.CONSTANT_Package:
163                 i = ((ConstantPackage) c).getNameIndex();
164                 c = getConstant(i, Const.CONSTANT_Utf8);
165                 str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
166                 break;
167             default: // Never reached
168                 throw new IllegalArgumentException("Unknown constant type " + tag);
169         }
170         return str;
171     }
172 
173     private static String escape( final String str ) {
174         final int len = str.length();
175         final StringBuilder buf = new StringBuilder(len + 5);
176         final char[] ch = str.toCharArray();
177         for (int i = 0; i < len; i++) {
178             switch (ch[i]) {
179                 case '\n':
180                     buf.append("\\n");
181                     break;
182                 case '\r':
183                     buf.append("\\r");
184                     break;
185                 case '\t':
186                     buf.append("\\t");
187                     break;
188                 case '\b':
189                     buf.append("\\b");
190                     break;
191                 case '"':
192                     buf.append("\\\"");
193                     break;
194                 default:
195                     buf.append(ch[i]);
196             }
197         }
198         return buf.toString();
199     }
200 
201     /**
202      * Retrieves constant at `index' from constant pool and resolve it to
203      * a string representation.
204      *
205      * @param  index of constant in constant pool
206      * @param  tag expected type
207      * @return String representation
208      */
209     public String constantToString( final int index, final byte tag ) throws ClassFormatException {
210         final Constant c = getConstant(index, tag);
211         return constantToString(c);
212     }
213 
214     /**
215      * Dump constant pool to file stream in binary format.
216      *
217      * @param file Output file stream
218      * @throws IOException
219      */
220     public void dump( final DataOutputStream file ) throws IOException {
221         file.writeShort(constantPool.length);
222         for (int i = 1; i < constantPool.length; i++) {
223             if (constantPool[i] != null) {
224                 constantPool[i].dump(file);
225             }
226         }
227     }
228 
229     /**
230      * Gets constant from constant pool.
231      *
232      * @param  index Index in constant pool
233      * @return Constant value
234      * @see    Constant
235      */
236     public Constant getConstant( final int index ) {
237         if (index >= constantPool.length || index < 0) {
238             throw new ClassFormatException("Invalid constant pool reference: " + index
239                     + ". Constant pool size is: " + constantPool.length);
240         }
241         return constantPool[index];
242     }
243 
244     /**
245      * Gets constant from constant pool and check whether it has the
246      * expected type.
247      *
248      * @param  index Index in constant pool
249      * @param  tag Tag of expected constant, i.e., its type
250      * @return Constant value
251      * @see    Constant
252      * @throws  ClassFormatException
253      */
254     public Constant getConstant( final int index, final byte tag ) throws ClassFormatException {
255         Constant c;
256         c = getConstant(index);
257         if (c == null) {
258             throw new ClassFormatException("Constant pool at index " + index + " is null.");
259         }
260         if (c.getTag() != tag) {
261             throw new ClassFormatException("Expected class `" + Const.getConstantName(tag)
262                     + "' at index " + index + " and got " + c);
263         }
264         return c;
265     }
266 
267     /**
268      * @return Array of constants.
269      * @see    Constant
270      */
271     public Constant[] getConstantPool() {
272         return constantPool;
273     }
274 
275     /**
276      * Gets string from constant pool and bypass the indirection of
277      * `ConstantClass' and `ConstantString' objects. I.e. these classes have
278      * an index field that points to another entry of the constant pool of
279      * type `ConstantUtf8' which contains the real data.
280      *
281      * @param  index Index in constant pool
282      * @param  tag Tag of expected constant, either ConstantClass or ConstantString
283      * @return Contents of string reference
284      * @see    ConstantClass
285      * @see    ConstantString
286      * @throws  ClassFormatException
287      */
288     public String getConstantString( final int index, final byte tag ) throws ClassFormatException {
289         Constant c;
290         int i;
291         c = getConstant(index, tag);
292         /* This switch() is not that elegant, since the four classes have the
293          * same contents, they just differ in the name of the index
294          * field variable.
295          * But we want to stick to the JVM naming conventions closely though
296          * we could have solved these more elegantly by using the same
297          * variable name or by subclassing.
298          */
299         switch (tag) {
300             case Const.CONSTANT_Class:
301                 i = ((ConstantClass) c).getNameIndex();
302                 break;
303             case Const.CONSTANT_String:
304                 i = ((ConstantString) c).getStringIndex();
305                 break;
306             case Const.CONSTANT_Module:
307                 i = ((ConstantModule) c).getNameIndex();
308                 break;
309             case Const.CONSTANT_Package:
310                 i = ((ConstantPackage) c).getNameIndex();
311                 break;
312             default:
313                 throw new IllegalArgumentException("getConstantString called with illegal tag " + tag);
314         }
315         // Finally get the string from the constant pool
316         c = getConstant(i, Const.CONSTANT_Utf8);
317         return ((ConstantUtf8) c).getBytes();
318     }
319 
320 
321     /**
322      * @return Length of constant pool.
323      */
324     public int getLength() {
325         return constantPool == null ? 0 : constantPool.length;
326     }
327 
328 
329     /**
330      * @param constant Constant to set
331      */
332     public void setConstant( final int index, final Constant constant ) {
333         constantPool[index] = constant;
334     }
335 
336 
337     /**
338      * @param constantPool
339      */
340     public void setConstantPool( final Constant[] constantPool ) {
341         this.constantPool = constantPool;
342     }
343 
344 
345     /**
346      * @return String representation.
347      */
348     @Override
349     public String toString() {
350         final StringBuilder buf = new StringBuilder();
351         for (int i = 1; i < constantPool.length; i++) {
352             buf.append(i).append(")").append(constantPool[i]).append("\n");
353         }
354         return buf.toString();
355     }
356 
357 
358     /**
359      * @return deep copy of this constant pool
360      */
361     public ConstantPool copy() {
362         ConstantPool c = null;
363         try {
364             c = (ConstantPool) clone();
365             c.constantPool = new Constant[constantPool.length];
366             for (int i = 1; i < constantPool.length; i++) {
367                 if (constantPool[i] != null) {
368                     c.constantPool[i] = constantPool[i].copy();
369                 }
370             }
371         } catch (final CloneNotSupportedException e) {
372             // TODO should this throw?
373         }
374         return c;
375     }
376 }