View Javadoc
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)
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);
48          }
49          return components;
50      }
51  
52      private RecordComponentInfo[] components;
53  
54      /**
55       * Constructs object from input stream.
56       *
57       * @param nameIndex    Index in constant pool
58       * @param length       Content length in bytes
59       * @param input        Input stream
60       * @param constantPool Array of constants
61       * @throws IOException if an I/O error occurs.
62       */
63      Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
64              throws IOException {
65          this(nameIndex, length, readComponents(input, constantPool), constantPool);
66      }
67  
68      /**
69       * Constructs a new instance using components.
70       *
71       * @param nameIndex    Index in constant pool
72       * @param length       Content length in bytes
73       * @param classes      Array of Record Component Info elements
74       * @param constantPool Array of constants
75       */
76      public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes,
77              final ConstantPool constantPool) {
78          super(Const.ATTR_RECORD, nameIndex, length, constantPool);
79          this.components = classes != null ? classes : EMPTY_RCI_ARRAY;
80          Args.requireU2(this.components.length, "attributes.length");
81      }
82  
83      /**
84       * Called by objects that are traversing the nodes of the tree implicitly
85       * defined by the contents of a Java class. For example, the hierarchy of methods,
86       * fields, attributes, etc. spawns a tree of objects.
87       *
88       * @param v Visitor object
89       */
90      @Override
91      public void accept(final Visitor v) {
92          v.visitRecord(this);
93      }
94  
95      /**
96       * Copies this instance and its components.
97       *
98       * @return a deep copy of this instance and its components.
99       */
100     @Override
101     public Attribute copy(final ConstantPool constantPool) {
102         final Record c = (Record) clone();
103         if (components.length > 0) {
104             c.components = components.clone();
105         }
106         c.setConstantPool(constantPool);
107         return c;
108     }
109 
110     /**
111      * Dumps this instance into a file stream in binary format.
112      *
113      * @param file output stream.
114      * @throws IOException if an I/O error occurs.
115      */
116     @Override
117     public void dump(final DataOutputStream file) throws IOException {
118         super.dump(file);
119         file.writeShort(components.length);
120         for (final RecordComponentInfo component : components) {
121             component.dump(file);
122         }
123     }
124 
125     /**
126      * Gets all the record components.
127      *
128      * @return array of Record Component Info elements.
129      */
130     public RecordComponentInfo[] getComponents() {
131         return components;
132     }
133 
134     /**
135      * Converts this instance to a String suitable for debugging.
136      *
137      * @return String a String suitable for debugging.
138      */
139     @Override
140     public String toString() {
141         final StringBuilder buf = new StringBuilder();
142         buf.append("Record(");
143         buf.append(components.length);
144         buf.append("):\n");
145         for (final RecordComponentInfo component : components) {
146             buf.append("  ").append(component.toString()).append("\n");
147         }
148         return buf.substring(0, buf.length() - 1); // remove the last newline
149     }
150 
151 }