    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.bcel.classfile;
    018
    019import java.io.DataInput;
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022import java.util.Arrays;
    023import java.util.Iterator;
    024import java.util.stream.Stream;
    025
    026import org.apache.bcel.Const;
    027import org.apache.bcel.util.Args;
    028
    029/**
    030 * This class is derived from <em>Attribute</em> and denotes that this class is an Inner class of another. to the source
    031 * file of this class. It is instantiated from the <em>Attribute.readAttribute()</em> method.
    032 *
    033 * @see Attribute
    034 */
    035public final class InnerClasses extends Attribute implements Iterable<InnerClass> {
    036
    037    /**
    038     * Empty array.
    039     */
    040    private static final InnerClass[] EMPTY_INNER_CLASSE_ARRAY = {};
    041
    042    private InnerClass[] innerClasses;
    043
    044    /**
    045     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
    046     * physical copy.
    047     *
    048     * @param c Source to copy.
    049     */
    050    public InnerClasses(final InnerClasses c) {
    051        this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
    052    }
    053
    054    /**
    055     * Constructs object from input stream.
    056     *
    057     * @param nameIndex Index in constant pool to CONSTANT_Utf8
    058     * @param length Content length in bytes
    059     * @param input Input stream
    060     * @param constantPool Array of constants
    061     * @throws IOException if an I/O error occurs.
    062     */
    063    InnerClasses(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    064        this(nameIndex, length, (InnerClass[]) null, constantPool);
    065        final int classCount = input.readUnsignedShort();
    066        innerClasses = new InnerClass[classCount];
    067        for (int i = 0; i < classCount; i++) {
    068            innerClasses[i] = new InnerClass(input);
    069        }
    070    }
    071
    072    /**
    073     * @param nameIndex Index in constant pool to CONSTANT_Utf8
    074     * @param length Content length in bytes
    075     * @param innerClasses array of inner classes attributes
    076     * @param constantPool Array of constants
    077     */
    078    public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
    079        super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
    080        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
    081        Args.requireU2(this.innerClasses.length, "innerClasses.length");
    082    }
    083
    084    /**
    085     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    086     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    087     *
    088     * @param v Visitor object
    089     */
    090    @Override
    091    public void accept(final Visitor v) {
    092        v.visitInnerClasses(this);
    093    }
    094
    095    /**
    096     * @return deep copy of this attribute
    097     */
    098    @Override
    099    public Attribute copy(final ConstantPool constantPool) {
    100        // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
    101        final InnerClasses c = (InnerClasses) clone();
    102        c.innerClasses = new InnerClass[innerClasses.length];
    103        Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
    104        c.setConstantPool(constantPool);
    105        return c;
    106    }
    107
    108    /**
    109     * Dump source file attribute to file stream in binary format.
    110     *
    111     * @param file Output file 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(innerClasses.length);
    118        for (final InnerClass innerClass : innerClasses) {
    119            innerClass.dump(file);
    120        }
    121    }
    122
    123    /**
    124     * @return array of inner class "records"
    125     */
    126    public InnerClass[] getInnerClasses() {
    127        return innerClasses;
    128    }
    129
    130    @Override
    131    public Iterator<InnerClass> iterator() {
    132        return Stream.of(innerClasses).iterator();
    133    }
    134
    135    /**
    136     * @param innerClasses the array of inner classes
    137     */
    138    public void setInnerClasses(final InnerClass[] innerClasses) {
    139        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
    140    }
    141
    142    /**
    143     * @return String representation.
    144     */
    145    @Override
    146    public String toString() {
    147        final StringBuilder buf = new StringBuilder();
    148        buf.append("InnerClasses(");
    149        buf.append(innerClasses.length);
    150        buf.append("):\n");
    151        for (final InnerClass innerClass : innerClasses) {
    152            buf.append(innerClass.toString(super.getConstantPool())).append("\n");
    153        }
    154        return buf.substring(0, buf.length() - 1); // remove the last newline
    155    }
    156}