1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInput;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.stream.Stream;
25
26 import org.apache.bcel.Const;
27 import org.apache.bcel.util.Args;
28
29
30
31
32
33
34
35 public final class InnerClasses extends Attribute implements Iterable<InnerClass> {
36
37
38
39
40 private static final InnerClass[] EMPTY_ARRAY = {};
41
42 private InnerClass[] innerClasses;
43
44
45
46
47
48
49
50 public InnerClasses(final InnerClasses c) {
51 this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
52 }
53
54
55
56
57
58
59
60
61
62
63 InnerClasses(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
64 this(nameIndex, length, (InnerClass[]) null, constantPool);
65 final int classCount = input.readUnsignedShort();
66 innerClasses = new InnerClass[classCount];
67 for (int i = 0; i < classCount; i++) {
68 innerClasses[i] = new InnerClass(input);
69 }
70 }
71
72
73
74
75
76
77
78 public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
79 super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
80 this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
81 Args.requireU2(this.innerClasses.length, "innerClasses.length");
82 }
83
84
85
86
87
88
89
90 @Override
91 public void accept(final Visitor v) {
92 v.visitInnerClasses(this);
93 }
94
95
96
97
98 @Override
99 public Attribute copy(final ConstantPool constantPool) {
100
101 final InnerClasses c = (InnerClasses) clone();
102 c.innerClasses = new InnerClass[innerClasses.length];
103 Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
104 c.setConstantPool(constantPool);
105 return c;
106 }
107
108
109
110
111
112
113
114 @Override
115 public void dump(final DataOutputStream file) throws IOException {
116 super.dump(file);
117 file.writeShort(innerClasses.length);
118 for (final InnerClass innerClass : innerClasses) {
119 innerClass.dump(file);
120 }
121 }
122
123
124
125
126 public InnerClass[] getInnerClasses() {
127 return innerClasses;
128 }
129
130 @Override
131 public Iterator<InnerClass> iterator() {
132 return Stream.of(innerClasses).iterator();
133 }
134
135
136
137
138 public void setInnerClasses(final InnerClass[] innerClasses) {
139 this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
140 }
141
142
143
144
145 @Override
146 public String toString() {
147 final StringBuilder buf = new StringBuilder();
148 buf.append("InnerClasses(");
149 buf.append(innerClasses.length);
150 buf.append("):\n");
151 for (final InnerClass innerClass : innerClasses) {
152 buf.append(innerClass.toString(super.getConstantPool())).append("\n");
153 }
154 return buf.substring(0, buf.length() - 1);
155 }
156 }