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