KickJava   Java API By Example, From Geeks To Geeks.

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


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: RefCpInfo.java,v 1.3 2002/05/11 22:50:22 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of a 'ref'-type entry in the ConstantPool.
32  *
33  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
34  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
35  */

36 abstract public class RefCpInfo extends CpInfo {
37   /** Holds the class index, stored in a 2-byte array in the class file. */
38   private int classIndex;
39   /** Holds the name-type index, stored in a 2-byte array in the class file. */
40   private int nameAndTypeIndex;
41   
42   
43   
44   
45   /** Default constructor that creates a new RefCpInfo object.
46    * @param tag the tag that describes the type of the object
47    */

48   protected RefCpInfo(int tag) {
49     super(tag);
50   }
51   
52   
53   
54   
55   /** Return the class index.
56    * @return index into the constant pool
57    * @see #setClassIndex
58    */

59   protected int getClassIndex() {
60     return classIndex;
61   }
62   
63   
64   /** Set the class index.
65    * @param index index into the constant pool
66    * @see #getClassIndex
67    */

68   protected void setClassIndex(int index) {
69     classIndex = index;
70   }
71   
72   
73   
74   
75   /** Return the name-and-type index.
76    * @return index into the constant pool
77    * @see #setNameAndTypeIndex
78    */

79   protected int getNameAndTypeIndex() {
80     return nameAndTypeIndex;
81   }
82   
83   
84   /** Set the name-and-type index.
85    * @param index index into the constnat pool
86    * @see #getNameAndTypeIndex
87    */

88   protected void setNameAndTypeIndex(int index) {
89     nameAndTypeIndex = index;
90   }
91   
92   
93   
94   
95   /** Check for name-and-type references to constant pool and mark them.
96    * @param pool the constant pool the element belongs to
97    */

98   protected void markNTRefs(ConstantPool pool) {
99     pool.incRefCount(getNameAndTypeIndex());
100   }
101   
102   
103   
104   
105   /** Read the 'info' data (the float value) following the u1tag byte.
106    * @param din the input stream
107    * @throws IOException if an I/O error occurs
108    */

109   protected void readInfo(DataInput din)
110   throws IOException {
111     setClassIndex(din.readUnsignedShort());
112     setNameAndTypeIndex(din.readUnsignedShort());
113   }
114   
115   
116   /** Write the 'info' data (the float value) following the u1tag byte.
117    * @param dout the output stream
118    * @throws IOException if an I/O error occurs
119    */

120   protected void writeInfo(DataOutput dout)
121   throws IOException {
122     dout.writeShort(getClassIndex());
123     dout.writeShort(getNameAndTypeIndex());
124   }
125   
126   
127   
128   
129   /** Dump the content of the entry to the specified file (used for debugging).
130    * @param pw the print writer
131    * @param cf the class file the element belongs to
132    * @param index the index in the constant pool
133    */

134   public void dump(PrintWriter pw, ClassFile cf, int index) {
135     pw.print('['); pw.print(index); pw.print("]: ");
136     String JavaDoc name = getClass().getName();
137     pw.println(name.substring(name.lastIndexOf('.')+1));
138     pw.print(" -> class index: ");
139     pw.println(getClassIndex());
140     pw.print(" class name: ");
141     pw.println( ((Utf8CpInfo) cf.getCpEntry(((ClassCpInfo) cf.getCpEntry(getClassIndex())).getClassNameIndex())).getString());
142     pw.print(" -> name and type index: ");
143     pw.println(getNameAndTypeIndex());
144     NameAndTypeCpInfo cpInfo = (NameAndTypeCpInfo) cf.getCpEntry(getNameAndTypeIndex());
145     pw.print(" name: '");
146     pw.print( ((Utf8CpInfo) cf.getCpEntry(cpInfo.getNameIndex())).getString());
147     pw.println('\'');
148     pw.print(" descriptor: '");
149     pw.print( ((Utf8CpInfo) cf.getCpEntry(cpInfo.getDescriptorIndex())).getString());
150     pw.println('\'');
151   }
152 }
153
Popular Tags