KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > classfile > CpInfo


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at theit@gmx.de.
22  *
23  *
24  * $Id: CpInfo.java,v 1.2 2002/05/08 11:53:42 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31
32 /** Representation of an entry in the ConstantPool. Specific types of entry
33  * have their representations sub-classed from this.
34  *
35  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
36  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
37  */

38 abstract public class CpInfo implements ClassConstants {
39   /** Holds a tag that describes the type of the entry, stored in a single byte
40    * in the class file.
41    */

42   private int tag;
43   /** Used for reference counting in the constant pool. */
44   private int refCount = 0;
45   
46   
47   
48   
49   /** Create a new CpInfo from the data passed.
50    * @param din the input stream to read from
51    * @return the new CpInfo class created from the input stream
52    * @throws IOException if class file is corrupt or incomplete
53    */

54   public static CpInfo create(DataInput din)
55   throws IOException {
56     if (din == null) throw new IOException("No input stream was provided.");
57     
58     // Instantiate based on tag byte
59
CpInfo ci = null;
60     switch (din.readUnsignedByte()) {
61       case CONSTANT_Utf8:
62         ci = new Utf8CpInfo();
63         break;
64         
65       case CONSTANT_Integer:
66         ci = new IntegerCpInfo();
67         break;
68       case CONSTANT_Float:
69         ci = new FloatCpInfo();
70         break;
71       case CONSTANT_Long:
72         ci = new LongCpInfo();
73         break;
74         
75       case CONSTANT_Double:
76         ci = new DoubleCpInfo();
77         break;
78         
79       case CONSTANT_Class:
80         ci = new ClassCpInfo();
81         break;
82         
83       case CONSTANT_String:
84         ci = new StringCpInfo();
85         break;
86         
87       case CONSTANT_Fieldref:
88         ci = new FieldrefCpInfo();
89         break;
90         
91       case CONSTANT_Methodref:
92         ci = new MethodrefCpInfo();
93         break;
94         
95       case CONSTANT_InterfaceMethodref:
96         ci = new InterfaceMethodrefCpInfo();
97         break;
98         
99       case CONSTANT_NameAndType:
100         ci = new NameAndTypeCpInfo();
101         break;
102         
103       default:
104         throw new IOException("Unknown tag type in constant pool.");
105     }
106     ci.readInfo(din);
107     return ci;
108   }
109   
110   
111   
112   
113   /** Default constructor for the CpInfo class.
114    * @param tag an integer tag describes the type of the class
115    */

116   protected CpInfo(int tag) {
117     setTag(tag);
118   }
119   
120   
121   /** Sets the tag that describes the type of the constant pool entry.
122    * @param tag type of the contant pool entry
123    * @see #getTag
124    */

125   private void setTag(int tag) {
126     this.tag = tag;
127   }
128   
129   
130   /** Returns the tag that describes the type of the constant pool entry.
131    * @return type of the constant pool entry
132    * @see #setTag
133    */

134   private int getTag() {
135     return tag;
136   }
137   
138   
139   /** Read the 'info' data following the u1tag byte; over-ride this in
140    * sub-classes.
141    * @param din the input stream
142    * @throws IOException if an I/O error occurs
143    */

144   abstract protected void readInfo(DataInput din)
145   throws IOException;
146   
147   
148   /** Check for Utf8 references to constant pool and mark them; override this
149    * in sub-classes.
150    * @param pool the constant pool to check
151    */

152   protected void markUtf8Refs(ConstantPool pool) {
153   }
154   
155   
156   /** Check for NameAndType references to constant pool and mark them; override
157    * this in sub-classes.
158    * @param pool the constant pool
159    */

160   protected void markNTRefs(ConstantPool pool) {
161   }
162   
163   
164   /** Export the representation to a DataOutput stream.
165    * @param dout the output stream
166    * @throws IOException of an I/O error occurs
167    */

168   public void write(DataOutput dout)
169   throws IOException {
170     if (dout == null) throw new IOException("No output stream was provided.");
171     dout.writeByte(getTag());
172     writeInfo(dout);
173   }
174   
175   
176   /** Write the 'info' data following the u1tag byte; override this in
177    * sub-classes.
178    * @param dout the output stream
179    * @throws IOException if an I/O error occurs
180    */

181   abstract protected void writeInfo(DataOutput dout)
182   throws IOException;
183   
184   
185   
186   
187   /** Return the reference count.
188    * @return the reference count
189    */

190   public int getRefCount() {
191     return refCount;
192   }
193   
194   
195   /** Increment the reference count.
196    */

197   public void incRefCount() {
198     refCount++;
199   }
200   
201   
202   /** Decrement the reference count.
203    * @throws IllegalStateException if the reference count is already zero
204    */

205   public void decRefCount()
206   throws IllegalStateException JavaDoc {
207     if (refCount == 0) throw new IllegalStateException JavaDoc("Illegal to decrement ref count that is already zero.");
208     refCount--;
209   }
210   
211   
212   /** Reset the reference count to zero.
213    */

214   public void resetRefCount() {
215     refCount = 0;
216   }
217   
218   
219   
220   
221   /** Dump the content of the entry to the specified file (used for debugging).
222    * @param pw the print writer
223    * @param cf the class file the element belongs to
224    * @param index the index in the constant pool
225    */

226   abstract public void dump(PrintWriter pw, ClassFile cf, int index);
227 }
228
Popular Tags