InnerClasses.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 java.util.Arrays;
  22. import java.util.Iterator;
  23. import java.util.stream.Stream;

  24. import org.apache.bcel.Const;
  25. import org.apache.bcel.util.Args;

  26. /**
  27.  * This class is derived from <em>Attribute</em> and denotes that this class is an Inner class of another. to the source
  28.  * file of this class. It is instantiated from the <em>Attribute.readAttribute()</em> method.
  29.  *
  30.  * @see Attribute
  31.  */
  32. public final class InnerClasses extends Attribute implements Iterable<InnerClass> {

  33.     /**
  34.      * Empty array.
  35.      */
  36.     private static final InnerClass[] EMPTY_ARRAY = {};

  37.     private InnerClass[] innerClasses;

  38.     /**
  39.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
  40.      * physical copy.
  41.      *
  42.      * @param c Source to copy.
  43.      */
  44.     public InnerClasses(final InnerClasses c) {
  45.         this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
  46.     }

  47.     /**
  48.      * Constructs object from input stream.
  49.      *
  50.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  51.      * @param length Content length in bytes
  52.      * @param input Input stream
  53.      * @param constantPool Array of constants
  54.      * @throws IOException if an I/O error occurs.
  55.      */
  56.     InnerClasses(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  57.         this(nameIndex, length, (InnerClass[]) null, constantPool);
  58.         final int classCount = input.readUnsignedShort();
  59.         innerClasses = new InnerClass[classCount];
  60.         for (int i = 0; i < classCount; i++) {
  61.             innerClasses[i] = new InnerClass(input);
  62.         }
  63.     }

  64.     /**
  65.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  66.      * @param length Content length in bytes
  67.      * @param innerClasses array of inner classes attributes
  68.      * @param constantPool Array of constants
  69.      */
  70.     public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
  71.         super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
  72.         this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
  73.         Args.requireU2(this.innerClasses.length, "innerClasses.length");
  74.     }

  75.     /**
  76.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  77.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  78.      *
  79.      * @param v Visitor object
  80.      */
  81.     @Override
  82.     public void accept(final Visitor v) {
  83.         v.visitInnerClasses(this);
  84.     }

  85.     /**
  86.      * @return deep copy of this attribute
  87.      */
  88.     @Override
  89.     public Attribute copy(final ConstantPool constantPool) {
  90.         // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
  91.         final InnerClasses c = (InnerClasses) clone();
  92.         c.innerClasses = new InnerClass[innerClasses.length];
  93.         Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
  94.         c.setConstantPool(constantPool);
  95.         return c;
  96.     }

  97.     /**
  98.      * Dump source file attribute to file stream in binary format.
  99.      *
  100.      * @param file Output file stream
  101.      * @throws IOException if an I/O error occurs.
  102.      */
  103.     @Override
  104.     public void dump(final DataOutputStream file) throws IOException {
  105.         super.dump(file);
  106.         file.writeShort(innerClasses.length);
  107.         for (final InnerClass innerClass : innerClasses) {
  108.             innerClass.dump(file);
  109.         }
  110.     }

  111.     /**
  112.      * @return array of inner class "records"
  113.      */
  114.     public InnerClass[] getInnerClasses() {
  115.         return innerClasses;
  116.     }

  117.     @Override
  118.     public Iterator<InnerClass> iterator() {
  119.         return Stream.of(innerClasses).iterator();
  120.     }

  121.     /**
  122.      * @param innerClasses the array of inner classes
  123.      */
  124.     public void setInnerClasses(final InnerClass[] innerClasses) {
  125.         this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
  126.     }

  127.     /**
  128.      * @return String representation.
  129.      */
  130.     @Override
  131.     public String toString() {
  132.         final StringBuilder buf = new StringBuilder();
  133.         buf.append("InnerClasses(");
  134.         buf.append(innerClasses.length);
  135.         buf.append("):\n");
  136.         for (final InnerClass innerClass : innerClasses) {
  137.             buf.append(innerClass.toString(super.getConstantPool())).append("\n");
  138.         }
  139.         return buf.substring(0, buf.length() - 1); // remove the last newline
  140.     }
  141. }