View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  
25  /**
26   * Record component info from a record. Instances from this class maps
27   * every component from a given record.
28   *
29   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se14/preview/specs/records-jvms.html#jvms-4.7.30">
30   *      The Java Virtual Machine Specification, Java SE 14 Edition, Records (preview)</a>
31   * @since 6.9.0
32   */
33  public class RecordComponentInfo implements Node {
34  
35      private final int index;
36      private final int descriptorIndex;
37      private final Attribute[] attributes;
38      private final ConstantPool constantPool;
39  
40      /**
41       * Constructs a new instance from an input stream.
42       *
43       * @param input        Input stream
44       * @param constantPool Array of constants
45       * @throws IOException if an I/O error occurs.
46       */
47      public RecordComponentInfo(final DataInput input, final ConstantPool constantPool) throws IOException {
48          this.index = input.readUnsignedShort();
49          this.descriptorIndex = input.readUnsignedShort();
50          final int attributesCount = input.readUnsignedShort();
51          this.attributes = new Attribute[attributesCount];
52          for (int j = 0; j < attributesCount; j++) {
53              attributes[j] = Attribute.readAttribute(input, constantPool);
54          }
55          this.constantPool = constantPool;
56      }
57  
58      @Override
59      public void accept(final Visitor v) {
60          v.visitRecordComponent(this);
61      }
62  
63      /**
64       * Dumps contents into a file stream in binary format.
65       *
66       * @param file Output file stream
67       * @throws IOException if an I/O error occurs.
68       */
69      public void dump(final DataOutputStream file) throws IOException {
70          file.writeShort(index);
71          file.writeShort(descriptorIndex);
72          file.writeShort(attributes.length);
73          for (final Attribute attribute : attributes) {
74              attribute.dump(file);
75          }
76      }
77  
78      /**
79       * Gets all attributes.
80       *
81       * @return all attributes.
82       */
83      public Attribute[] getAttributes() {
84          return attributes;
85      }
86  
87      /**
88       * Gets the constant pool.
89       *
90       * @return Constant pool.
91       */
92      public ConstantPool getConstantPool() {
93          return constantPool;
94      }
95  
96      /**
97       * Gets the description index.
98       *
99       * @return index in constant pool of this record component descriptor.
100      */
101     public int getDescriptorIndex() {
102         return descriptorIndex;
103     }
104 
105     /**
106      * Gets the name index.
107      *
108      * @return index in constant pool of this record component name.
109      */
110     public int getIndex() {
111         return index;
112     }
113 
114     /**
115      * Converts this instance to a String suitable for debugging.
116      *
117      * @return a String suitable for debugging.
118      */
119     @Override
120     public String toString() {
121         final StringBuilder buf = new StringBuilder();
122         buf.append("RecordComponentInfo(");
123         buf.append(constantPool.getConstantString(index, Const.CONSTANT_Utf8));
124         buf.append(",");
125         buf.append(constantPool.getConstantString(descriptorIndex, Const.CONSTANT_Utf8));
126         buf.append(",");
127         buf.append(attributes.length);
128         buf.append("):\n");
129         for (final Attribute attribute : attributes) {
130             buf.append("  ").append(attribute.toString()).append("\n");
131         }
132         return buf.substring(0, buf.length() - 1); // remove the last newline
133     }
134 
135 }