KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > file > InnerClassesAttribute


1 package alt.jiapi.file;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.DataInputStream JavaDoc;
5 import java.io.DataOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * Class CodeAttribute.
13  *
14  * @author Mika Riekkinen
15  * @see Java Virtual Machine Specification 2nd edition, ch. 4.7.5
16  */

17 public class InnerClassesAttribute extends Attribute {
18     List JavaDoc classes = new LinkedList JavaDoc();
19
20     InnerClassesAttribute(short nameIndex, DataInputStream JavaDoc dis) throws IOException JavaDoc {
21         super(nameIndex);
22
23         short count = dis.readShort();
24         for (int i = 0; i < count; i++) {
25             short inner_class_info_index = dis.readShort();
26             short outer_class_info_index = dis.readShort();
27             short inner_name_index = dis.readShort();
28             short inner_class_access_flags = dis.readShort();
29
30             classes.add(new InnerClass(inner_class_info_index,
31                                        outer_class_info_index,
32                                        inner_name_index,
33                                        inner_class_access_flags));
34         }
35     }
36     
37     public byte[] getBytes() {
38         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
39         DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(baos);
40
41         try {
42             dos.writeShort(classes.size());
43
44             Iterator JavaDoc i = classes.iterator();
45             while(i.hasNext()) {
46                 InnerClass ic = (InnerClass)i.next();
47                 dos.writeShort(ic.getInnerClassInfoIndex());
48                 dos.writeShort(ic.getOuterClassInfoIndex());
49                 dos.writeShort(ic.getInnerNameIndex());
50                 dos.writeShort(ic.getInnerClassAccessFlags());
51             }
52         }
53         catch(IOException JavaDoc ioe) {
54             // Should not ever happen
55
throw new RuntimeException JavaDoc(ioe);
56         }
57
58         return baos.toByteArray();
59     }
60
61
62     public class InnerClass {
63         private short inner_class_info_index;
64         private short outer_class_info_index;
65         private short inner_name_index;
66         private short inner_class_access_flags;
67
68         public InnerClass(short inner_class_info_index,
69                           short outer_class_info_index,
70                           short inner_name_index,
71                           short inner_class_access_flags) {
72             this.inner_class_info_index = inner_class_info_index;
73             this.outer_class_info_index = outer_class_info_index;
74             this.inner_name_index = inner_name_index;
75             this.inner_class_access_flags = inner_class_access_flags;
76         }
77
78
79         public short getInnerClassInfoIndex() {
80             return inner_class_info_index;
81         }
82         public short getOuterClassInfoIndex() {
83             return outer_class_info_index;
84         }
85         public short getInnerNameIndex() {
86             return inner_name_index;
87         }
88         public short getInnerClassAccessFlags() {
89             return inner_class_access_flags;
90         }
91     }
92 }
93
94
Popular Tags