001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import org.apache.log4j.*;
039:
040: public class ConstantPool extends ArrayList<ConstantPoolEntry>
041: implements Visitable {
042: private Classfile classfile;
043:
044: public ConstantPool(Classfile classfile, DataInputStream in)
045: throws IOException {
046: this .classfile = classfile;
047:
048: int count = in.readUnsignedShort();
049:
050: ensureCapacity(count);
051:
052: // Entry 0 is null
053: add(null);
054:
055: for (int i = 1; i < count; i++) {
056: byte tag = in.readByte();
057:
058: switch (tag) {
059: case ConstantPoolEntry.CONSTANT_Class:
060: add(new Class_info(this , in));
061: break;
062: case ConstantPoolEntry.CONSTANT_Fieldref:
063: add(new FieldRef_info(this , in));
064: break;
065: case ConstantPoolEntry.CONSTANT_Methodref:
066: add(new MethodRef_info(this , in));
067: break;
068: case ConstantPoolEntry.CONSTANT_InterfaceMethodref:
069: add(new InterfaceMethodRef_info(this , in));
070: break;
071: case ConstantPoolEntry.CONSTANT_String:
072: add(new String_info(this , in));
073: break;
074: case ConstantPoolEntry.CONSTANT_Integer:
075: add(new Integer_info(this , in));
076: break;
077: case ConstantPoolEntry.CONSTANT_Float:
078: add(new Float_info(this , in));
079: break;
080: case ConstantPoolEntry.CONSTANT_Long:
081: add(new Long_info(this , in));
082: i++;
083: add(null);
084: break;
085: case ConstantPoolEntry.CONSTANT_Double:
086: add(new Double_info(this , in));
087: i++;
088: add(null);
089: break;
090: case ConstantPoolEntry.CONSTANT_NameAndType:
091: add(new NameAndType_info(this , in));
092: break;
093: case ConstantPoolEntry.CONSTANT_Utf8:
094: add(new UTF8_info(this , in));
095: break;
096: default:
097: Logger.getLogger(getClass()).info("Unknown Tag " + tag);
098: break;
099: }
100: }
101: }
102:
103: public Classfile getClassfile() {
104: return classfile;
105: }
106:
107: public void accept(Visitor visitor) {
108: visitor.visitConstantPool(this );
109: }
110:
111: public String toString() {
112: StringWriter out = new StringWriter();
113: PrintWriter writer = new PrintWriter(out);
114:
115: writer.println("Constant Pool:");
116:
117: Printer printer = new TextPrinter(writer);
118: accept(printer);
119:
120: writer.close();
121:
122: return out.toString();
123: }
124: }
|