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  
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  import org.apache.bcel.util.Args;
26  
27  /**
28   * Extends {@link Attribute} and records the classes and
29   * interfaces that are authorized to claim membership in the nest hosted by the
30   * current class or interface. There may be at most one Record attribute in a
31   * ClassFile structure.
32   *
33   * @see Attribute
34   * @since 6.9.0
35   */
36  public final class Record extends Attribute {
37  
38      private static final RecordComponentInfo[] EMPTY_RCI_ARRAY = {};
39  
40      private static RecordComponentInfo[] readComponents(final DataInput input, final ConstantPool constantPool)
41              throws IOException {
42          final int classCount = input.readUnsignedShort();
43          final RecordComponentInfo[] components = new RecordComponentInfo[classCount];
44          for (int i = 0; i < classCount; i++) {
45              components[i] = new RecordComponentInfo(input, constantPool);
46          }
47          return components;
48      }
49  
50      private RecordComponentInfo[] components;
51  
52      /**
53       * Constructs object from input stream.
54       *
55       * @param nameIndex    Index in constant pool
56       * @param length       Content length in bytes
57       * @param input        Input stream
58       * @param constantPool Array of constants
59       * @throws IOException if an I/O error occurs.
60       */
61      Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
62              throws IOException {
63          this(nameIndex, length, readComponents(input, constantPool), constantPool);
64      }
65  
66      /**
67       * Constructs a new instance using components.
68       *
69       * @param nameIndex    Index in constant pool
70       * @param length       Content length in bytes
71       * @param classes      Array of Record Component Info elements
72       * @param constantPool Array of constants
73       */
74      public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes,
75              final ConstantPool constantPool) {
76          super(Const.ATTR_RECORD, nameIndex, length, constantPool);
77          this.components = classes != null ? classes : EMPTY_RCI_ARRAY;
78          Args.requireU2(this.components.length, "attributes.length");
79      }
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitly
83       * defined by the contents of a Java class. For example, the hierarchy of methods,
84       * fields, attributes, etc. spawns a tree of objects.
85       *
86       * @param v Visitor object
87       */
88      @Override
89      public void accept(final Visitor v) {
90          v.visitRecord(this);
91      }
92  
93      /**
94       * Copies this instance and its components.
95       *
96       * @return a deep copy of this instance and its components.
97       */
98      @Override
99      public Attribute copy(final ConstantPool constantPool) {
100         final Record c = (Record) clone();
101         if (components.length > 0) {
102             c.components = components.clone();
103         }
104         c.setConstantPool(constantPool);
105         return c;
106     }
107 
108     /**
109      * Dumps this instance into a file stream in binary format.
110      *
111      * @param file output stream.
112      * @throws IOException if an I/O error occurs.
113      */
114     @Override
115     public void dump(final DataOutputStream file) throws IOException {
116         super.dump(file);
117         file.writeShort(components.length);
118         for (final RecordComponentInfo component : components) {
119             component.dump(file);
120         }
121     }
122 
123     /**
124      * Gets all the record components.
125      *
126      * @return array of Record Component Info elements.
127      */
128     public RecordComponentInfo[] getComponents() {
129         return components;
130     }
131 
132     /**
133      * Converts this instance to a String suitable for debugging.
134      *
135      * @return String a String suitable for debugging.
136      */
137     @Override
138     public String toString() {
139         final StringBuilder buf = new StringBuilder();
140         buf.append("Record(");
141         buf.append(components.length);
142         buf.append("):\n");
143         for (final RecordComponentInfo component : components) {
144             buf.append("  ").append(component.toString()).append("\n");
145         }
146         return buf.substring(0, buf.length() - 1); // remove the last newline
147     }
148 
149 }