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