Record.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * Extends {@link Attribute} and records the classes and
  25.  * interfaces that are authorized to claim membership in the nest hosted by the
  26.  * current class or interface. There may be at most one Record attribute in a
  27.  * ClassFile structure.
  28.  *
  29.  * @see Attribute
  30.  * @since 6.9.0
  31.  */
  32. public final class Record extends Attribute {

  33.     private static final RecordComponentInfo[] EMPTY_RCI_ARRAY = {};

  34.     private static RecordComponentInfo[] readComponents(final DataInput input, final ConstantPool constantPool)
  35.             throws IOException {
  36.         final int classCount = input.readUnsignedShort();
  37.         final RecordComponentInfo[] components = new RecordComponentInfo[classCount];
  38.         for (int i = 0; i < classCount; i++) {
  39.             components[i] = new RecordComponentInfo(input, constantPool);
  40.         }
  41.         return components;
  42.     }

  43.     private RecordComponentInfo[] components;

  44.     /**
  45.      * Constructs object from input stream.
  46.      *
  47.      * @param nameIndex    Index in constant pool
  48.      * @param length       Content length in bytes
  49.      * @param input        Input stream
  50.      * @param constantPool Array of constants
  51.      * @throws IOException if an I/O error occurs.
  52.      */
  53.     Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
  54.             throws IOException {
  55.         this(nameIndex, length, readComponents(input, constantPool), constantPool);
  56.     }

  57.     /**
  58.      * Constructs a new instance using components.
  59.      *
  60.      * @param nameIndex    Index in constant pool
  61.      * @param length       Content length in bytes
  62.      * @param classes      Array of Record Component Info elements
  63.      * @param constantPool Array of constants
  64.      */
  65.     public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes,
  66.             final ConstantPool constantPool) {
  67.         super(Const.ATTR_RECORD, nameIndex, length, constantPool);
  68.         this.components = classes != null ? classes : EMPTY_RCI_ARRAY;
  69.         Args.requireU2(this.components.length, "attributes.length");
  70.     }

  71.     /**
  72.      * Called by objects that are traversing the nodes of the tree implicitly
  73.      * defined by the contents of a Java class. For example, the hierarchy of methods,
  74.      * fields, attributes, etc. spawns a tree of objects.
  75.      *
  76.      * @param v Visitor object
  77.      */
  78.     @Override
  79.     public void accept(final Visitor v) {
  80.         v.visitRecord(this);
  81.     }

  82.     /**
  83.      * Copies this instance and its components.
  84.      *
  85.      * @return a deep copy of this instance and its components.
  86.      */
  87.     @Override
  88.     public Attribute copy(final ConstantPool constantPool) {
  89.         final Record c = (Record) clone();
  90.         if (components.length > 0) {
  91.             c.components = components.clone();
  92.         }
  93.         c.setConstantPool(constantPool);
  94.         return c;
  95.     }

  96.     /**
  97.      * Dumps this instance into a file stream in binary format.
  98.      *
  99.      * @param file output stream.
  100.      * @throws IOException if an I/O error occurs.
  101.      */
  102.     @Override
  103.     public void dump(final DataOutputStream file) throws IOException {
  104.         super.dump(file);
  105.         file.writeShort(components.length);
  106.         for (final RecordComponentInfo component : components) {
  107.             component.dump(file);
  108.         }
  109.     }

  110.     /**
  111.      * Gets all the record components.
  112.      *
  113.      * @return array of Record Component Info elements.
  114.      */
  115.     public RecordComponentInfo[] getComponents() {
  116.         return components;
  117.     }

  118.     /**
  119.      * Converts this instance to a String suitable for debugging.
  120.      *
  121.      * @return String a String suitable for debugging.
  122.      */
  123.     @Override
  124.     public String toString() {
  125.         final StringBuilder buf = new StringBuilder();
  126.         buf.append("Record(");
  127.         buf.append(components.length);
  128.         buf.append("):\n");
  129.         for (final RecordComponentInfo component : components) {
  130.             buf.append("  ").append(component.toString()).append("\n");
  131.         }
  132.         return buf.substring(0, buf.length() - 1); // remove the last newline
  133.     }

  134. }