KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of an attribute.
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 InnerClassesAttrInfo extends AttrInfo {
37   /** Holds the number of inner classes. */
38   private int numberOfClasses;
39   /** Holds the inner classes. */
40   private InnerClassesInfo[] innerClasses;
41   
42   
43   
44   
45   /** Default constructor that creates an InnerClassesAttrInfo object.
46    * @param cf the class file the object belongs to
47    * @param attrNameIndex index into the constant pool
48    * @param attrLength the length of the additional info data in the class file
49    */

50   protected InnerClassesAttrInfo(ClassFile cf, int attrNameIndex, int attrLength) {
51     super(cf, attrNameIndex, attrLength);
52   }
53   
54   
55   
56   
57   /** Return the string name of the attribute.
58    * @return string name of the attribute
59    */

60   protected String JavaDoc getAttrName() {
61     return ATTR_InnerClasses;
62   }
63   
64   
65   
66   
67   /** Sets the number of inner classes.
68    * @param num the number of inner classes
69    * @see #getNumberOfClasses
70    */

71   protected void setNumberOfClasses(int num) {
72     this.numberOfClasses = num;
73   }
74   
75   
76   /** Returns the number of inner classes.
77    * @return number of inner classes
78    * @see #setNumberOfClasses
79    */

80   protected int getNumberOfClasses() {
81     return numberOfClasses;
82   }
83   
84   
85   
86   
87   /** Sets the array of inner classes.
88    * @param classes array of inner classes
89    * @see #getInnerClasses
90    */

91   protected void setInnerClasses(InnerClassesInfo[] classes) {
92     innerClasses = classes;
93   }
94   
95   
96   /** Returns the array of inner classes.
97    * @return array of inner classes
98    * @see #getInnerClasses
99    * @see #getInnerClass
100    */

101   protected InnerClassesInfo[] getInnerClasses() {
102     return innerClasses;
103   }
104   
105   
106   /** Returns the specified inner class.
107    * @param index the index of the desired class in the internal array
108    * @return inner class
109    * @see #getInnerClasses
110    */

111   protected InnerClassesInfo getInnerClass(int index) {
112     return innerClasses[index];
113   }
114   
115   
116   
117   
118   /** Check for Utf8 references in the 'info' data to the constant pool and
119    * mark them.
120    * @param pool the constant pool the element belongs to
121    */

122   protected void markUtf8RefsInInfo(ConstantPool pool) {
123     for (int i = 0; i < getNumberOfClasses(); i++) {
124       getInnerClass(i).markUtf8Refs(pool);
125     }
126   }
127   
128   
129   
130   
131   /** Read the data following the header.
132    * @param din the input stream
133    * @throws IOException if an I/O error occurs
134    */

135   protected void readInfo(DataInput din)
136   throws IOException {
137     setNumberOfClasses(din.readUnsignedShort());
138     InnerClassesInfo[] classes = new InnerClassesInfo[getNumberOfClasses()];
139     for (int i = 0; i < getNumberOfClasses(); i++) {
140       classes[i] = InnerClassesInfo.create(din);
141     }
142     setInnerClasses(classes);
143   }
144   
145   
146   /** Export data following the header to a DataOutput stream. Should be
147    * overwritten in subclasses.
148    * @param dout the output stream
149    * @throws IOException if an I/O error occurs
150    */

151   public void writeInfo(DataOutput dout)
152   throws IOException {
153     dout.writeShort(getNumberOfClasses());
154     for (int i = 0; i < getNumberOfClasses(); i++) {
155       getInnerClass(i).write(dout);
156     }
157   }
158   
159   
160   
161   
162   /** Dump the content of the entry to the specified file (used for debugging).
163    * @param pw the print writer
164    * @param cf the class file the element belongs to
165    */

166   public void dump(PrintWriter pw, ClassFile cf) {
167     pw.println(getAttrName());
168     pw.print("Number of entries: ");
169     pw.println(getNumberOfClasses());
170     for (int i=0; i<getNumberOfClasses(); i++) {
171       getInnerClass(i).dump(pw, cf);
172     }
173   }
174 }
175
Popular Tags