KickJava   Java API By Example, From Geeks To Geeks.

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


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: StringCpInfo.java,v 1.4 2002/05/11 23:21:48 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of a 'string' 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 StringCpInfo extends CpInfo {
37   /** Holds the name index, stored in a 2-byte array in the class file. */
38   private int nameIndex;
39   
40   
41   
42   
43   /** Default constructor that creates a new ClassCpInfo object.
44    */

45   protected StringCpInfo() {
46     super(CONSTANT_String);
47   }
48   
49   
50   
51   
52   /** Sets the name index for the entry.
53    * @param index the name index for the entry
54    * @see #getNameIndex
55    */

56   protected void setNameIndex(int index) {
57     this.nameIndex = index;
58   }
59   
60   
61   /** Returns the name index for the entry.
62    * @return name index for the entry.
63    * @see #setNameIndex
64    */

65   protected int getNameIndex() {
66     return nameIndex;
67   }
68   
69   
70   
71   
72   /** Check for Utf8 references to constant pool and mark them.
73    * @param pool the constant pool
74    */

75   protected void markUtf8Refs(ConstantPool pool) {
76     pool.incRefCount(getNameIndex());
77   }
78   
79   
80   
81   
82   /** Read the 'info' data (the short value) following the u1tag byte.
83    * @param din the input stream
84    * @throws IOException if an I/O error occurs
85    */

86   protected void readInfo(DataInput din)
87   throws IOException {
88     setNameIndex(din.readUnsignedShort());
89   }
90   
91   
92   /** Write the 'info' data (the short value) following the u1tag byte.
93    * @param dout the output stream
94    * @throws IOException if an I/O error occurs
95    */

96   protected void writeInfo(DataOutput dout)
97   throws IOException {
98     dout.writeShort(getNameIndex());
99   }
100   
101   
102   
103   
104   /** Dump the content of the entry to the specified file (used for debugging).
105    * @param pw the print writer
106    * @param cf the class file the element belongs to
107    * @param index the index in the constant pool
108    */

109   public void dump(PrintWriter pw, ClassFile cf, int index) {
110     pw.print('['); pw.print(index); pw.println("]: StringCpInfo");
111     pw.print(" -> string name index: ");
112     pw.println(getNameIndex());
113     pw.print(" string: '");
114     pw.print( ((Utf8CpInfo) cf.getCpEntry(getNameIndex())).getString());
115     pw.println('\'');
116   }
117 }
118
Popular Tags