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