KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > InnerClassesAttr


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.util.*;
56 import java.io.*;
57
58 /******************************************************************************
59  * This class corresponds to the InnerClasses_attribute structure introduced in
60  * JDK1.1. It is not defined in the first edition of
61  * <i>The Java Virual Machine Specification</i>.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 19 <!-- $-->, <!--$$JustDate:--> 00/11/27 <!-- $-->
66  */

67 class InnerClassesAttr extends Attribute {
68     private List mInnerClasses = new ArrayList();
69
70     public InnerClassesAttr(ConstantPool cp) {
71         super(cp, INNER_CLASSES);
72     }
73
74     /**
75      * @param inner The full inner class name
76      * @param outer The full outer class name
77      * @param name The simple name of the inner class, or null if anonymous
78      * @param accessFlags Access flags for the inner class
79      */

80     public void addInnerClass(String JavaDoc inner,
81                               String JavaDoc outer,
82                               String JavaDoc name,
83                               AccessFlags accessFlags) {
84         
85         ConstantClassInfo innerInfo = ConstantClassInfo.make(mCp, inner);
86         ConstantClassInfo outerInfo;
87         if (outer == null) {
88             outerInfo = null;
89         }
90         else {
91             outerInfo = ConstantClassInfo.make(mCp, outer);
92         }
93
94         ConstantUTFInfo nameInfo;
95         if (name == null) {
96             nameInfo = null;
97         }
98         else {
99             nameInfo = ConstantUTFInfo.make(mCp, name);
100         }
101
102         mInnerClasses.add(new Info(innerInfo, outerInfo, nameInfo,
103                                    accessFlags.getModifier()));
104     }
105
106     public Info[] getInnerClassesInfo() {
107         Info[] infos = new Info[mInnerClasses.size()];
108         return (Info[])mInnerClasses.toArray(infos);
109     }
110
111     public int getLength() {
112         return 2 + 8 * mInnerClasses.size();
113     }
114
115     public void writeDataTo(DataOutput dout) throws IOException {
116         int size = mInnerClasses.size();
117         dout.writeShort(size);
118         for (int i=0; i<size; i++) {
119             ((Info)mInnerClasses.get(i)).writeTo(dout);
120         }
121     }
122
123     static Attribute define(ConstantPool cp,
124                             String JavaDoc name,
125                             int length,
126                             DataInput din) throws IOException {
127
128         InnerClassesAttr innerClasses = new InnerClassesAttr(cp);
129
130         int size = din.readUnsignedShort();
131         for (int i=0; i<size; i++) {
132             int inner_index = din.readUnsignedShort();
133             int outer_index = din.readUnsignedShort();
134             int name_index = din.readUnsignedShort();
135             int af = din.readUnsignedShort();
136             
137             ConstantClassInfo inner;
138             if (inner_index == 0) {
139                 inner = null;
140             }
141             else {
142                 inner = (ConstantClassInfo)cp.getConstant(inner_index);
143             }
144
145             ConstantClassInfo outer;
146             if (outer_index == 0) {
147                 outer = null;
148             }
149             else {
150                 outer = (ConstantClassInfo)cp.getConstant(outer_index);
151             }
152             
153             ConstantUTFInfo innerName;
154             if (name_index == 0) {
155                 innerName = null;
156             }
157             else {
158                 innerName = (ConstantUTFInfo)cp.getConstant(name_index);
159             }
160
161             Info info = new Info(inner, outer, innerName, af);
162             innerClasses.mInnerClasses.add(info);
163         }
164
165         return innerClasses;
166     }
167
168     public static class Info {
169         private ConstantClassInfo mInner;
170         private ConstantClassInfo mOuter;
171         private ConstantUTFInfo mName;
172         private int mAccessFlags;
173
174         Info(ConstantClassInfo inner,
175              ConstantClassInfo outer,
176              ConstantUTFInfo name,
177              int accessFlags) {
178
179             mInner = inner;
180             mOuter = outer;
181             mName = name;
182             mAccessFlags = accessFlags;
183         }
184
185         /**
186          * Returns null if no inner class specified.
187          */

188         public ConstantClassInfo getInnerClass() {
189             return mInner;
190         }
191
192         /**
193          * Returns null if no outer class specified.
194          */

195         public ConstantClassInfo getOuterClass() {
196             return mOuter;
197         }
198
199         /**
200          * Returns null if no inner class specified or is anonymous.
201          */

202         public String JavaDoc getInnerClassName() {
203             if (mName == null) {
204                 return null;
205             }
206             else {
207                 return mName.getValue();
208             }
209         }
210
211         /**
212          * Returns a copy of the access flags.
213          */

214         public AccessFlags getAccessFlags() {
215             return new AccessFlags(mAccessFlags);
216         }
217
218         public void writeTo(DataOutput dout) throws IOException {
219             if (mInner == null) {
220                 dout.writeShort(0);
221             }
222             else {
223                 dout.writeShort(mInner.getIndex());
224             }
225
226             if (mOuter == null) {
227                 dout.writeShort(0);
228             }
229             else {
230                 dout.writeShort(mOuter.getIndex());
231             }
232
233             if (mName == null) {
234                 dout.writeShort(0);
235             }
236             else {
237                 dout.writeShort(mName.getIndex());
238             }
239             
240             dout.writeShort(mAccessFlags);
241         }
242     }
243 }
244
Popular Tags