KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > attribute > InnerClassesAttr


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile.attribute;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.DataInput JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.cojen.classfile.Attribute;
25 import org.cojen.classfile.ConstantPool;
26 import org.cojen.classfile.Modifiers;
27 import org.cojen.classfile.constant.ConstantClassInfo;
28 import org.cojen.classfile.constant.ConstantUTFInfo;
29
30 /**
31  * This class corresponds to the InnerClasses_attribute structure introduced in
32  * JDK1.1. It is not defined in the first edition of
33  * <i>The Java Virual Machine Specification</i>.
34  *
35  * @author Brian S O'Neill
36  */

37 public class InnerClassesAttr extends Attribute {
38
39     private List JavaDoc mInnerClasses = new ArrayList JavaDoc();
40
41     public InnerClassesAttr(ConstantPool cp) {
42         super(cp, INNER_CLASSES);
43     }
44
45     public InnerClassesAttr(ConstantPool cp, String JavaDoc name) {
46         super(cp, name);
47     }
48
49     public InnerClassesAttr(ConstantPool cp, String JavaDoc name, int length, DataInput JavaDoc din)
50         throws IOException JavaDoc
51     {
52         super(cp, name);
53
54         int size = din.readUnsignedShort();
55         for (int i=0; i<size; i++) {
56             int inner_index = din.readUnsignedShort();
57             int outer_index = din.readUnsignedShort();
58             int name_index = din.readUnsignedShort();
59             int af = din.readUnsignedShort();
60             
61             ConstantClassInfo inner;
62             if (inner_index == 0) {
63                 inner = null;
64             } else {
65                 inner = (ConstantClassInfo)cp.getConstant(inner_index);
66             }
67
68             ConstantClassInfo outer;
69             if (outer_index == 0) {
70                 outer = null;
71             } else {
72                 outer = (ConstantClassInfo)cp.getConstant(outer_index);
73             }
74             
75             ConstantUTFInfo innerName;
76             if (name_index == 0) {
77                 innerName = null;
78             } else {
79                 innerName = (ConstantUTFInfo)cp.getConstant(name_index);
80             }
81
82             mInnerClasses.add(new Info(inner, outer, innerName, Modifiers.getInstance(af)));
83         }
84     }
85
86     /**
87      * @param inner The full inner class name
88      * @param outer The full outer class name
89      * @param name The simple name of the inner class, or null if anonymous
90      * @param modifiers Modifiers for the inner class
91      */

92     public void addInnerClass(String JavaDoc inner,
93                               String JavaDoc outer,
94                               String JavaDoc name,
95                               Modifiers modifiers) {
96         
97         ConstantClassInfo innerInfo = getConstantPool().addConstantClass(inner);
98         ConstantClassInfo outerInfo;
99         if (outer == null) {
100             outerInfo = null;
101         } else {
102             outerInfo = getConstantPool().addConstantClass(outer);
103         }
104
105         ConstantUTFInfo nameInfo;
106         if (name == null) {
107             nameInfo = null;
108         } else {
109             nameInfo = getConstantPool().addConstantUTF(name);
110         }
111
112         mInnerClasses.add(new Info(innerInfo, outerInfo, nameInfo, modifiers));
113     }
114
115     public Info[] getInnerClassesInfo() {
116         Info[] infos = new Info[mInnerClasses.size()];
117         return (Info[])mInnerClasses.toArray(infos);
118     }
119
120     public int getLength() {
121         return 2 + 8 * mInnerClasses.size();
122     }
123
124     public void writeDataTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
125         int size = mInnerClasses.size();
126         dout.writeShort(size);
127         for (int i=0; i<size; i++) {
128             ((Info)mInnerClasses.get(i)).writeTo(dout);
129         }
130     }
131
132     public static class Info {
133         private ConstantClassInfo mInner;
134         private ConstantClassInfo mOuter;
135         private ConstantUTFInfo mName;
136         private Modifiers mModifiers;
137
138         Info(ConstantClassInfo inner,
139              ConstantClassInfo outer,
140              ConstantUTFInfo name,
141              Modifiers modifiers) {
142
143             mInner = inner;
144             mOuter = outer;
145             mName = name;
146             mModifiers = modifiers;
147         }
148
149         /**
150          * Returns null if no inner class specified.
151          */

152         public ConstantClassInfo getInnerClass() {
153             return mInner;
154         }
155
156         /**
157          * Returns null if no outer class specified.
158          */

159         public ConstantClassInfo getOuterClass() {
160             return mOuter;
161         }
162
163         /**
164          * Returns null if no inner class specified or is anonymous.
165          */

166         public ConstantUTFInfo getInnerClassName() {
167             return mName;
168         }
169
170         /**
171          * Returns the modifiers.
172          */

173         public Modifiers getModifiers() {
174             return mModifiers;
175         }
176
177         public void writeTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
178             if (mInner == null) {
179                 dout.writeShort(0);
180             } else {
181                 dout.writeShort(mInner.getIndex());
182             }
183
184             if (mOuter == null) {
185                 dout.writeShort(0);
186             } else {
187                 dout.writeShort(mOuter.getIndex());
188             }
189
190             if (mName == null) {
191                 dout.writeShort(0);
192             } else {
193                 dout.writeShort(mName.getIndex());
194             }
195             
196             dout.writeShort(mModifiers.getBitmask());
197         }
198     }
199 }
200
Popular Tags