KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of a 'nameandtype' 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 public class NameAndTypeCpInfo extends CpInfo implements Cloneable JavaDoc {
37   /** Holds the name index, stored in a 2-byte array in the class file. */
38   private int nameIndex;
39   /** Holds the descriptor name index, stored in a 2-byte array in the class
40    * file. */

41   private int descriptorIndex;
42   
43   
44   
45   
46   /** Default constructor that creates a new NameAndTypeCpInfo object.
47    */

48   protected NameAndTypeCpInfo() {
49     super(CONSTANT_NameAndType);
50   }
51   
52   
53   
54   
55   /** Clone the entry.
56    * @return a duplicate of the entry
57    */

58   public Object JavaDoc clone() {
59     NameAndTypeCpInfo cloneInfo = new NameAndTypeCpInfo();
60     cloneInfo.setNameIndex(getNameIndex());
61     cloneInfo.setDescriptorIndex(getDescriptorIndex());
62     cloneInfo.resetRefCount();
63     return cloneInfo;
64   }
65   
66   
67   
68   
69   /** Return the name index into the constant pool.
70    * @return index into the constant pool
71    * @see #setNameIndex
72    */

73   protected int getNameIndex() {
74     return nameIndex;
75   }
76   
77   
78   /** Set the name index into the constant pool.
79    * @param index the index into the constant pool
80    * @see #getNameIndex
81    */

82   protected void setNameIndex(int index) {
83     nameIndex = index;
84   }
85   
86   
87   
88   
89   /** Return the descriptor index.
90    * @return index into the constant pool
91    * @see #setDescriptorIndex
92    */

93   protected int getDescriptorIndex() {
94     return descriptorIndex;
95   }
96   
97   
98   /** Set the descriptor index.
99    * @param index the index into the constant pool
100    * @see #getDescriptorIndex
101    */

102   protected void setDescriptorIndex(int index) {
103     descriptorIndex = index;
104   }
105   
106   
107   
108   
109   /** Check for Utf8 references to constant pool and mark them.
110    * @param pool the constant pool the element belongs to
111    */

112   protected void markUtf8Refs(ConstantPool pool) {
113     pool.incRefCount(getNameIndex());
114     pool.incRefCount(getDescriptorIndex());
115   }
116   
117   
118   
119   
120   /** Read the 'info' data (the two short values) following the u1tag byte.
121    * @param din the input stream
122    * @throws IOException if an I/O error occurs
123    */

124   protected void readInfo(DataInput din)
125   throws IOException {
126     setNameIndex(din.readUnsignedShort());
127     setDescriptorIndex(din.readUnsignedShort());
128   }
129   
130   
131   /** Write the 'info' data (the two short values) following the u1tag byte.
132    * @param dout the output stream
133    * @throws IOException if an I/O error occurs
134    */

135   protected void writeInfo(DataOutput dout)
136   throws IOException {
137     dout.writeShort(getNameIndex());
138     dout.writeShort(getDescriptorIndex());
139   }
140   
141   
142   
143   
144   /** Dump the content of the entry to the specified file (used for debugging).
145    * @param pw the print writer
146    * @param cf the class file the element belongs to
147    * @param index the index in the constant pool
148    */

149   public void dump(PrintWriter pw, ClassFile cf, int index) {
150     pw.print('['); pw.print(index); pw.println("]: NameAndTypeCpInfo");
151     pw.print(" -> name index: ");
152     pw.println(getNameIndex());
153     pw.print(" name: '");
154     pw.print( ((Utf8CpInfo) cf.getCpEntry(getNameIndex())).getString());
155     pw.println('\'');
156     pw.print(" -> descriptor index: ");
157     pw.println(getDescriptorIndex());
158     pw.print(" descriptor: '");
159     pw.print( ((Utf8CpInfo) cf.getCpEntry(getDescriptorIndex())).getString());
160     pw.println('\'');
161   }
162 }
163
Popular Tags