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 a new instance from an input stream.
45 *
46 * @param input Input stream.
47 * @param constantPool Array of constants.
48 * @throws IOException Thrown if an I/O error occurs.
49 */
50 public RecordComponentInfo(final DataInput input, final ConstantPool constantPool) throws IOException {
51 this.index = input.readUnsignedShort();
52 this.descriptorIndex = input.readUnsignedShort();
53 final int attributesCount = input.readUnsignedShort();
54 this.attributes = new Attribute[attributesCount];
55 for (int j = 0; j < attributesCount; j++) {
56 attributes[j] = Attribute.readAttribute(input, constantPool);
57 }
58 this.constantPool = constantPool;
59 }
60
61 @Override
62 public void accept(final Visitor v) {
63 v.visitRecordComponent(this);
64 }
65
66 /**
67 * Dumps contents into a file stream in binary format.
68 *
69 * @param file Output file stream.
70 * @throws IOException Thrown if an I/O error occurs.
71 */
72 public void dump(final DataOutputStream file) throws IOException {
73 file.writeShort(index);
74 file.writeShort(descriptorIndex);
75 file.writeShort(Args.requireU2(attributes.length, "attributes.length"));
76 for (final Attribute attribute : attributes) {
77 attribute.dump(file);
78 }
79 }
80
81 /**
82 * Gets the attribute for the given tag if present, or null if absent.
83 *
84 * @param <T> The attribute type.
85 * @param tag The attribute tag.
86 * @return Attribute for given tag, null if not found.
87 * Refer to {@link org.apache.bcel.Const#ATTR_UNKNOWN} constants named ATTR_* for possible values.
88 * @since 6.13.0
89 */
90 @SuppressWarnings("unchecked")
91 public final <T extends Attribute> T getAttribute(final byte tag) {
92 for (final Attribute attribute : getAttributes()) {
93 if (attribute.getTag() == tag) {
94 return (T) attribute;
95 }
96 }
97 return null;
98 }
99
100 /**
101 * Gets all attributes.
102 *
103 * @return all attributes.
104 */
105 public Attribute[] getAttributes() {
106 return attributes;
107 }
108
109 /**
110 * Gets the constant pool.
111 *
112 * @return Constant pool.
113 */
114 public ConstantPool getConstantPool() {
115 return constantPool;
116 }
117
118 /**
119 * Gets the description index.
120 *
121 * @return index in constant pool of this record component descriptor.
122 */
123 public int getDescriptorIndex() {
124 return descriptorIndex;
125 }
126
127 /**
128 * Gets the name index.
129 *
130 * @return index in constant pool of this record component name.
131 */
132 public int getIndex() {
133 return index;
134 }
135
136 /**
137 * Converts this instance to a String suitable for debugging.
138 *
139 * @return A String suitable for debugging.
140 */
141 @Override
142 public String toString() {
143 final StringBuilder buf = new StringBuilder();
144 buf.append("RecordComponentInfo(");
145 buf.append(constantPool.getConstantString(index, Const.CONSTANT_Utf8));
146 buf.append(",");
147 buf.append(constantPool.getConstantString(descriptorIndex, Const.CONSTANT_Utf8));
148 buf.append(",");
149 buf.append(attributes.length);
150 buf.append("):\n");
151 for (final Attribute attribute : attributes) {
152 buf.append(" ").append(attribute.toString()).append("\n");
153 }
154 return buf.substring(0, buf.length() - 1); // remove the last newline
155 }
156
157 }