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 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.util.Arrays;
024import java.util.Iterator;
025import java.util.stream.Stream;
026
027import org.apache.bcel.Const;
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    public InnerClasses(final InnerClasses c) {
049        this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
050    }
051
052    /**
053     * Construct object from input stream.
054     *
055     * @param name_index Index in constant pool to CONSTANT_Utf8
056     * @param length Content length in bytes
057     * @param input Input stream
058     * @param constant_pool Array of constants
059     * @throws IOException if an I/O error occurs.
060     */
061    InnerClasses(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
062        this(name_index, length, (InnerClass[]) null, constant_pool);
063        final int number_of_classes = input.readUnsignedShort();
064        innerClasses = new InnerClass[number_of_classes];
065        for (int i = 0; i < number_of_classes; i++) {
066            innerClasses[i] = new InnerClass(input);
067        }
068    }
069
070    /**
071     * @param name_index Index in constant pool to CONSTANT_Utf8
072     * @param length Content length in bytes
073     * @param innerClasses array of inner classes attributes
074     * @param constant_pool Array of constants
075     */
076    public InnerClasses(final int name_index, final int length, final InnerClass[] innerClasses, final ConstantPool constant_pool) {
077        super(Const.ATTR_INNER_CLASSES, name_index, length, constant_pool);
078        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
079    }
080
081    /**
082     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
083     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
084     *
085     * @param v Visitor object
086     */
087    @Override
088    public void accept(final Visitor v) {
089        v.visitInnerClasses(this);
090    }
091
092    /**
093     * @return deep copy of this attribute
094     */
095    @Override
096    public Attribute copy(final ConstantPool constantPool) {
097        // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
098        final InnerClasses c = (InnerClasses) clone();
099        c.innerClasses = new InnerClass[innerClasses.length];
100        Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
101        c.setConstantPool(constantPool);
102        return c;
103    }
104
105    /**
106     * Dump source file attribute to file stream in binary format.
107     *
108     * @param file Output file stream
109     * @throws IOException if an I/O error occurs.
110     */
111    @Override
112    public void dump(final DataOutputStream file) throws IOException {
113        super.dump(file);
114        file.writeShort(innerClasses.length);
115        for (final InnerClass inner_class : innerClasses) {
116            inner_class.dump(file);
117        }
118    }
119
120    /**
121     * @return array of inner class "records"
122     */
123    public InnerClass[] getInnerClasses() {
124        return innerClasses;
125    }
126
127    @Override
128    public Iterator<InnerClass> iterator() {
129        return Stream.of(innerClasses).iterator();
130    }
131
132    /**
133     * @param innerClasses the array of inner classes
134     */
135    public void setInnerClasses(final InnerClass[] innerClasses) {
136        this.innerClasses = innerClasses != null ? innerClasses : EMPTY_INNER_CLASSE_ARRAY;
137    }
138
139    /**
140     * @return String representation.
141     */
142    @Override
143    public String toString() {
144        final StringBuilder buf = new StringBuilder();
145        buf.append("InnerClasses(");
146        buf.append(innerClasses.length);
147        buf.append("):\n");
148        for (final InnerClass inner_class : innerClasses) {
149            buf.append(inner_class.toString(super.getConstantPool())).append("\n");
150        }
151        return buf.substring(0, buf.length() - 1); // remove the last newline
152    }
153}