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