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 package org.apache.bcel.classfile;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.util.Args;
27
28 /**
29 * Record component info from a record. Instances from this class maps
30 * every component from a given record.
31 *
32 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se14/preview/specs/records-jvms.html#jvms-4.7.30">
33 * The Java Virtual Machine Specification, Java SE 14 Edition, Records (preview)</a>
34 * @since 6.9.0
35 */
36 public class RecordComponentInfo implements Node {
37
38 private final int index;
39 private final int descriptorIndex;
40 private final Attribute[] attributes;
41 private final ConstantPool constantPool;
42
43 /**
44 * Constructs record component information from a data input using standard Code headers for nested attributes.
45 *
46 * @param input Input stream.
47 * @param constantPool The constant pool.
48 * @throws IOException Thrown if an I/O error occurs.
49 * @throws ClassFormatException Thrown if a component attribute is malformed.
50 */
51 public RecordComponentInfo(final DataInput input, final ConstantPool constantPool) throws IOException {
52 this(input, constantPool, false);
53 }
54
55 /**
56 * Constructs record component information from a data input using the selected Code header layout for nested attributes.
57 *
58 * <p>
59 * The header selection is forwarded when reading component attributes; it does not change the record component layout.
60 * </p>
61 *
62 * @param input Input stream.
63 * @param constantPool The constant pool.
64 * @param compactCodeHeader Whether to read any Code attributes encountered with compact four-byte headers ({@code true})
65 * or standard eight-byte headers ({@code false}).
66 * @throws IOException Thrown if an I/O error occurs.
67 * @throws ClassFormatException Thrown if a component attribute is malformed.
68 * @see Attribute#readAttribute(DataInput, ConstantPool, boolean)
69 */
70 RecordComponentInfo(final DataInput input, final ConstantPool constantPool, final boolean compactCodeHeader) throws IOException {
71 this.index = input.readUnsignedShort();
72 this.descriptorIndex = input.readUnsignedShort();
73 final int attributesCount = input.readUnsignedShort();
74 this.attributes = new Attribute[attributesCount];
75 for (int j = 0; j < attributesCount; j++) {
76 attributes[j] = Attribute.readAttribute(input, constantPool, compactCodeHeader);
77 }
78 this.constantPool = constantPool;
79 }
80
81 @Override
82 public void accept(final Visitor v) {
83 v.visitRecordComponent(this);
84 }
85
86 /**
87 * Dumps contents into a file stream in binary format.
88 *
89 * @param file Output file stream.
90 * @throws IOException Thrown if an I/O error occurs.
91 */
92 public void dump(final DataOutputStream file) throws IOException {
93 file.writeShort(index);
94 file.writeShort(descriptorIndex);
95 file.writeShort(Args.requireU2(attributes.length, "attributes.length"));
96 for (final Attribute attribute : attributes) {
97 attribute.dump(file);
98 }
99 }
100
101 /**
102 * Gets the attribute for the given tag if present, or null if absent.
103 *
104 * @param <T> The attribute type.
105 * @param tag The attribute tag.
106 * @return Attribute for given tag, null if not found.
107 * Refer to {@link org.apache.bcel.Const#ATTR_UNKNOWN} constants named ATTR_* for possible values.
108 * @since 6.13.0
109 */
110 @SuppressWarnings("unchecked")
111 public final <T extends Attribute> T getAttribute(final byte tag) {
112 for (final Attribute attribute : getAttributes()) {
113 if (attribute.getTag() == tag) {
114 return (T) attribute;
115 }
116 }
117 return null;
118 }
119
120 /**
121 * Gets all attributes.
122 *
123 * @return all attributes.
124 */
125 public Attribute[] getAttributes() {
126 return attributes;
127 }
128
129 /**
130 * Gets the constant pool.
131 *
132 * @return Constant pool.
133 */
134 public ConstantPool getConstantPool() {
135 return constantPool;
136 }
137
138 /**
139 * Gets the description index.
140 *
141 * @return index in constant pool of this record component descriptor.
142 */
143 public int getDescriptorIndex() {
144 return descriptorIndex;
145 }
146
147 /**
148 * Gets the name index.
149 *
150 * @return index in constant pool of this record component name.
151 */
152 public int getIndex() {
153 return index;
154 }
155
156 /**
157 * Converts this instance to a String suitable for debugging.
158 *
159 * @return A String suitable for debugging.
160 */
161 @Override
162 public String toString() {
163 final StringBuilder buf = new StringBuilder();
164 buf.append("RecordComponentInfo(");
165 buf.append(constantPool.getConstantString(index, Const.CONSTANT_Utf8));
166 buf.append(",");
167 buf.append(constantPool.getConstantString(descriptorIndex, Const.CONSTANT_Utf8));
168 buf.append(",");
169 buf.append(attributes.length);
170 buf.append("):\n");
171 for (final Attribute attribute : attributes) {
172 buf.append(" ").append(attribute.toString()).append("\n");
173 }
174 return buf.substring(0, buf.length() - 1); // remove the last newline
175 }
176
177 }