001: /*
002: *
003: *
004: * Copyright 1990-2007 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: package com.sun.satsa.jcrmic.classfile.constants;
028:
029: import java.io.*;
030:
031: /**
032: * This class represents constant pool.
033: * It defines a set of get and add methods to search or add a constant.
034: */
035:
036: public class JConstantPool {
037:
038: /**
039: * Constant ppol entries.
040: */
041: private JConstant[] constants;
042:
043: /**
044: * Constructor.
045: * @param size constant pool size.
046: */
047: public JConstantPool(int size) {
048: constants = new JConstant[size];
049: }
050:
051: /**
052: * Returns constant pool entry.
053: * @param index entry index
054: * @return the entry
055: */
056: public JConstant getAt(int index) {
057:
058: return constants[index];
059: }
060:
061: /**
062: * Returns constant.
063: * @param index entry index
064: * @return the entry
065: */
066: public JConstantUtf8 getConstantUtf8(int index) {
067: JConstant constant = getAt(index);
068:
069: if (constant instanceof JConstantUtf8)
070: return (JConstantUtf8) constant;
071: else
072: return null;
073: }
074:
075: /**
076: * Returns constant.
077: * @param index entry index
078: * @return the entry
079: */
080: public JConstantClass getConstantClass(int index) {
081: JConstant constant = getAt(index);
082:
083: if (constant instanceof JConstantClass)
084: return (JConstantClass) constant;
085: else
086: return null;
087: }
088:
089: /**
090: * Returns constant.
091: * @param index entry index
092: * @return the entry
093: */
094: public JConstantNameAndType getConstantNameAndType(int index) {
095: JConstant constant = getAt(index);
096:
097: if (constant instanceof JConstantNameAndType)
098: return (JConstantNameAndType) constant;
099: else
100: return null;
101: }
102:
103: /**
104: * Parses the constant pool.
105: * @param dis input stream
106: * @throws IOException if I/O exception occurs
107: */
108: public void parse(DataInputStream dis) throws IOException {
109: // note: constants start at index 1
110:
111: for (int i = 1; i < constants.length; i++) {
112: int tag = dis.readUnsignedByte();
113:
114: constants[i] = JConstant.create(tag, this );
115: constants[i].parse(dis);
116:
117: // If a Constant_Long_info or Constant_Double_info
118: // structure is the item in the constant pool table
119: // at index n, then the next index n+1 must be
120: // considered invalid and must not be used
121: if ((tag == JConstant.CONSTANT_DOUBLE)
122: || (tag == JConstant.CONSTANT_LONG)) {
123: i++;
124: }
125: }
126: }
127: }
|