KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > ConstantPool


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39 import org.apache.oro.text.perl.*;
40
41 public class ConstantPool extends ArrayList implements Visitable {
42     private Classfile classfile;
43
44     public ConstantPool(Classfile classfile, DataInputStream in) throws IOException {
45         this.classfile = classfile;
46
47         int count = in.readUnsignedShort();
48
49         ensureCapacity(count);
50
51         // Entry 0 is null
52
add(null);
53
54         for (int i=1; i<count; i++) {
55             byte tag = in.readByte();
56
57             switch(tag) {
58                 case ConstantPoolEntry.CONSTANT_Class:
59                     add(new Class_info(this, in));
60                     break;
61                 case ConstantPoolEntry.CONSTANT_Fieldref:
62                     add(new FieldRef_info(this, in));
63                     break;
64                 case ConstantPoolEntry.CONSTANT_Methodref:
65                     add(new MethodRef_info(this, in));
66                     break;
67                 case ConstantPoolEntry.CONSTANT_InterfaceMethodref:
68                     add(new InterfaceMethodRef_info(this, in));
69                     break;
70                 case ConstantPoolEntry.CONSTANT_String:
71                     add(new String_info(this, in));
72                     break;
73                 case ConstantPoolEntry.CONSTANT_Integer:
74                     add(new Integer_info(this, in));
75                     break;
76                 case ConstantPoolEntry.CONSTANT_Float:
77                     add(new Float_info(this, in));
78                     break;
79                 case ConstantPoolEntry.CONSTANT_Long:
80                     add(new Long_info(this, in));
81                     i++;
82                     add(null);
83                     break;
84                 case ConstantPoolEntry.CONSTANT_Double:
85                     add(new Double_info(this, in));
86                     i++;
87                     add(null);
88                     break;
89                 case ConstantPoolEntry.CONSTANT_NameAndType:
90                     add(new NameAndType_info(this, in));
91                     break;
92                 case ConstantPoolEntry.CONSTANT_Utf8:
93                     add(new UTF8_info(this, in));
94                     break;
95                 default:
96                     Logger.getLogger(getClass()).info("Unknown Tag " + tag);
97                     break;
98             }
99         }
100     }
101
102     public Classfile getClassfile() {
103         return classfile;
104     }
105
106     public void accept(Visitor visitor) {
107         visitor.visitConstantPool(this);
108     }
109
110     public String JavaDoc toString() {
111         StringWriter out = new StringWriter();
112         PrintWriter writer = new PrintWriter(out);
113
114         writer.println("Constant Pool:");
115
116         Printer printer = new TextPrinter(writer);
117         accept(printer);
118
119         writer.close();
120         
121         return out.toString();
122     }
123 }
124
Popular Tags