1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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
26 import org.apache.bcel.Const;
27 import org.apache.bcel.util.Args;
28
29 /**
30 * Extends {@link Attribute} and records the classes and
31 * interfaces that are authorized to claim membership in the nest hosted by the
32 * current class or interface. There may be at most one Record attribute in a
33 * ClassFile structure.
34 *
35 * @see Attribute
36 * @since 6.9.0
37 */
38 public final class Record extends Attribute {
39
40 private static final RecordComponentInfo[] EMPTY_RCI_ARRAY = {};
41
42 private static RecordComponentInfo[] readComponents(final DataInput input, final ConstantPool constantPool, final boolean compactCodeHeader)
43 throws IOException {
44 final int classCount = input.readUnsignedShort();
45 final RecordComponentInfo[] components = new RecordComponentInfo[classCount];
46 for (int i = 0; i < classCount; i++) {
47 components[i] = new RecordComponentInfo(input, constantPool, compactCodeHeader);
48 }
49 return components;
50 }
51
52 private RecordComponentInfo[] components;
53
54 /**
55 * Constructs a Record attribute from a data input using standard Code headers for nested attributes.
56 *
57 * @param nameIndex Constant pool index of the attribute name.
58 * @param length Content length in bytes.
59 * @param input Input stream.
60 * @param constantPool The constant pool.
61 * @throws IOException Thrown if an I/O error occurs.
62 * @throws ClassFormatException Thrown if the attribute or one of its components is malformed.
63 */
64 Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
65 throws IOException {
66 this(nameIndex, length, input, constantPool, false);
67 }
68
69 /**
70 * Constructs a Record attribute from a data input using the selected Code header layout for nested attributes.
71 *
72 * <p>
73 * The header selection is forwarded when reading component attributes; it does not change the Record attribute or record component layout.
74 * </p>
75 *
76 * @param nameIndex Constant pool index of the attribute name.
77 * @param length Content length in bytes.
78 * @param input Input stream.
79 * @param constantPool The constant pool.
80 * @param compactCodeHeader Whether to read any Code attributes encountered with compact four-byte headers ({@code true})
81 * or standard eight-byte headers ({@code false}).
82 * @throws IOException Thrown if an I/O error occurs.
83 * @throws ClassFormatException Thrown if the attribute or one of its components is malformed.
84 * @see Attribute#readAttribute(DataInput, ConstantPool, boolean)
85 */
86 Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool, final boolean compactCodeHeader)
87 throws IOException {
88 this(nameIndex, length, readComponents(input, constantPool, compactCodeHeader), constantPool);
89 }
90
91 /**
92 * Constructs a new instance using components.
93 *
94 * @param nameIndex Index in constant pool.
95 * @param length Content length in bytes.
96 * @param classes Array of Record Component Info elements.
97 * @param constantPool Array of constants.
98 */
99 public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes,
100 final ConstantPool constantPool) {
101 super(Const.ATTR_RECORD, nameIndex, length, constantPool);
102 this.components = classes != null ? classes : EMPTY_RCI_ARRAY;
103 Args.requireU2(this.components.length, "attributes.length");
104 }
105
106 /**
107 * Called by objects that are traversing the nodes of the tree implicitly
108 * defined by the contents of a Java class. For example, the hierarchy of methods,
109 * fields, attributes, and other elements forms a tree of objects.
110 *
111 * @param v Visitor object.
112 */
113 @Override
114 public void accept(final Visitor v) {
115 v.visitRecord(this);
116 }
117
118 /**
119 * Copies this instance and its components.
120 *
121 * @return A deep copy of this instance and its components.
122 */
123 @Override
124 public Attribute copy(final ConstantPool constantPool) {
125 final Record c = (Record) clone();
126 if (components.length > 0) {
127 c.components = components.clone();
128 }
129 c.setConstantPool(constantPool);
130 return c;
131 }
132
133 /**
134 * Dumps this instance into a file stream in binary format.
135 *
136 * @param file output stream.
137 * @throws IOException Thrown if an I/O error occurs.
138 */
139 @Override
140 public void dump(final DataOutputStream file) throws IOException {
141 super.dump(file);
142 file.writeShort(Args.requireU2(components.length, "components.length"));
143 for (final RecordComponentInfo component : components) {
144 component.dump(file);
145 }
146 }
147
148 /**
149 * Gets all the record components.
150 *
151 * @return array of Record Component Info elements.
152 */
153 public RecordComponentInfo[] getComponents() {
154 return components;
155 }
156
157 /**
158 * Converts this instance to a String suitable for debugging.
159 *
160 * @return String a String suitable for debugging.
161 */
162 @Override
163 public String toString() {
164 final StringBuilder buf = new StringBuilder();
165 buf.append("Record(");
166 buf.append(components.length);
167 buf.append("):\n");
168 for (final RecordComponentInfo component : components) {
169 buf.append(" ").append(component.toString()).append("\n");
170 }
171 return buf.substring(0, buf.length() - 1); // remove the last newline
172 }
173
174 }