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
24 import org.apache.bcel.Const;
25 import org.apache.bcel.util.Args;
26
27
28
29
30
31
32
33
34
35
36 public final class Record extends Attribute {
37
38 private static final RecordComponentInfo[] EMPTY_RCI_ARRAY = {};
39
40 private static RecordComponentInfo[] readComponents(final DataInput input, final ConstantPool constantPool)
41 throws IOException {
42 final int classCount = input.readUnsignedShort();
43 final RecordComponentInfo[] components = new RecordComponentInfo[classCount];
44 for (int i = 0; i < classCount; i++) {
45 components[i] = new RecordComponentInfo(input, constantPool);
46 }
47 return components;
48 }
49
50 private RecordComponentInfo[] components;
51
52
53
54
55
56
57
58
59
60
61 Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
62 throws IOException {
63 this(nameIndex, length, readComponents(input, constantPool), constantPool);
64 }
65
66
67
68
69
70
71
72
73
74 public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes,
75 final ConstantPool constantPool) {
76 super(Const.ATTR_RECORD, nameIndex, length, constantPool);
77 this.components = classes != null ? classes : EMPTY_RCI_ARRAY;
78 Args.requireU2(this.components.length, "attributes.length");
79 }
80
81
82
83
84
85
86
87
88 @Override
89 public void accept(final Visitor v) {
90 v.visitRecord(this);
91 }
92
93
94
95
96
97
98 @Override
99 public Attribute copy(final ConstantPool constantPool) {
100 final Record c = (Record) clone();
101 if (components.length > 0) {
102 c.components = components.clone();
103 }
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(components.length);
118 for (final RecordComponentInfo component : components) {
119 component.dump(file);
120 }
121 }
122
123
124
125
126
127
128 public RecordComponentInfo[] getComponents() {
129 return components;
130 }
131
132
133
134
135
136
137 @Override
138 public String toString() {
139 final StringBuilder buf = new StringBuilder();
140 buf.append("Record(");
141 buf.append(components.length);
142 buf.append("):\n");
143 for (final RecordComponentInfo component : components) {
144 buf.append(" ").append(component.toString()).append("\n");
145 }
146 return buf.substring(0, buf.length() - 1);
147 }
148
149 }