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;
027
028/**
029 * This class is derived from <em>Attribute</em> and denotes that this class is an Inner class of another. to the source
030 * file of this class. It is instantiated from the <em>Attribute.readAttribute()</em> method.
031 *
032 * @see Attribute
033 */
034public final class InnerClasses extends Attribute implements Iterable<InnerClass> {
035
036    /**
037     * Empty array.
038     */
039    private static final InnerClass[] EMPTY_INNER_CLASSE_ARRAY = {};
040
041    private InnerClass[] innerClasses;
042
043    /**
044     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
045     * physical copy.
046     */
047    public InnerClasses(final InnerClasses c) {
048        this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
049    }
050
051    /**
052     * Construct object from input stream.
053     *
054     * @param nameIndex Index in constant pool to CONSTANT_Utf8
055     * @param length Content length in bytes
056     * @param input Input stream
057     * @param constantPool Array of constants
058     * @throws IOException if an I/O error occurs.
059     */
060    InnerClasses(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
061        this(nameIndex, length, (InnerClass[]) null, constantPool);
062        final int classCount = input.readUnsignedShort();
063        innerClasses = new InnerClass[classCount];
064        for (int i = 0; i < classCount; i++) {
065            innerClasses[i] = new InnerClass(input);
066        }
067    }
068
069    /**
070     * @param nameIndex Index in constant pool to CONSTANT_Utf8
071     * @param length Content length in bytes
072     * @param innerClasses array of inner classes attributes
073     * @param constantPool Array of constants
074     */
075    public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
076        super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
077        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
078    }
079
080    /**
081     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
082     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
083     *
084     * @param v Visitor object
085     */
086    @Override
087    public void accept(final Visitor v) {
088        v.visitInnerClasses(this);
089    }
090
091    /**
092     * @return deep copy of this attribute
093     */
094    @Override
095    public Attribute copy(final ConstantPool constantPool) {
096        // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
097        final InnerClasses c = (InnerClasses) clone();
098        c.innerClasses = new InnerClass[innerClasses.length];
099        Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
100        c.setConstantPool(constantPool);
101        return c;
102    }
103
104    /**
105     * Dump source file attribute to file stream in binary format.
106     *
107     * @param file Output file stream
108     * @throws IOException if an I/O error occurs.
109     */
110    @Override
111    public void dump(final DataOutputStream file) throws IOException {
112        super.dump(file);
113        file.writeShort(innerClasses.length);
114        for (final InnerClass innerClass : innerClasses) {
115            innerClass.dump(file);
116        }
117    }
118
119    /**
120     * @return array of inner class "records"
121     */
122    public InnerClass[] getInnerClasses() {
123        return innerClasses;
124    }
125
126    @Override
127    public Iterator<InnerClass> iterator() {
128        return Stream.of(innerClasses).iterator();
129    }
130
131    /**
132     * @param innerClasses the array of inner classes
133     */
134    public void setInnerClasses(final InnerClass[] innerClasses) {
135        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
136    }
137
138    /**
139     * @return String representation.
140     */
141    @Override
142    public String toString() {
143        final StringBuilder buf = new StringBuilder();
144        buf.append("InnerClasses(");
145        buf.append(innerClasses.length);
146        buf.append("):\n");
147        for (final InnerClass innerClass : innerClasses) {
148            buf.append(innerClass.toString(super.getConstantPool())).append("\n");
149        }
150        return buf.substring(0, buf.length() - 1); // remove the last newline
151    }
152}