001: /*
002: * Copyright 2000-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package org.apache.bcel.classfile;
018:
019: import java.io.DataInputStream;
020: import java.io.DataOutputStream;
021: import java.io.IOException;
022: import org.apache.bcel.Constants;
023:
024: /**
025: * This class is derived from <em>Attribute</em> and denotes that this class
026: * is an Inner class of another.
027: * to the source file of this class.
028: * It is instantiated from the <em>Attribute.readAttribute()</em> method.
029: *
030: * @version $Id: InnerClasses.java 386056 2006-03-15 11:31:56Z tcurdt $
031: * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
032: * @see Attribute
033: */
034: public final class InnerClasses extends Attribute {
035:
036: private InnerClass[] inner_classes;
037: private int number_of_classes;
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(InnerClasses c) {
044: this (c.getNameIndex(), c.getLength(), c.getInnerClasses(), c
045: .getConstantPool());
046: }
047:
048: /**
049: * @param name_index Index in constant pool to CONSTANT_Utf8
050: * @param length Content length in bytes
051: * @param inner_classes array of inner classes attributes
052: * @param constant_pool Array of constants
053: */
054: public InnerClasses(int name_index, int length,
055: InnerClass[] inner_classes, ConstantPool constant_pool) {
056: super (Constants.ATTR_INNER_CLASSES, name_index, length,
057: constant_pool);
058: setInnerClasses(inner_classes);
059: }
060:
061: /**
062: * Construct object from file stream.
063: *
064: * @param name_index Index in constant pool to CONSTANT_Utf8
065: * @param length Content length in bytes
066: * @param file Input stream
067: * @param constant_pool Array of constants
068: * @throws IOException
069: */
070: InnerClasses(int name_index, int length, DataInputStream file,
071: ConstantPool constant_pool) throws IOException {
072: this (name_index, length, (InnerClass[]) null, constant_pool);
073: number_of_classes = file.readUnsignedShort();
074: inner_classes = new InnerClass[number_of_classes];
075: for (int i = 0; i < number_of_classes; i++) {
076: inner_classes[i] = new InnerClass(file);
077: }
078: }
079:
080: /**
081: * Called by objects that are traversing the nodes of the tree implicitely
082: * defined by the contents of a Java class. I.e., the hierarchy of methods,
083: * fields, attributes, etc. spawns a tree of objects.
084: *
085: * @param v Visitor object
086: */
087: public void accept(Visitor v) {
088: v.visitInnerClasses(this );
089: }
090:
091: /**
092: * Dump source file attribute to file stream in binary format.
093: *
094: * @param file Output file stream
095: * @throws IOException
096: */
097: public final void dump(DataOutputStream file) throws IOException {
098: super .dump(file);
099: file.writeShort(number_of_classes);
100: for (int i = 0; i < number_of_classes; i++) {
101: inner_classes[i].dump(file);
102: }
103: }
104:
105: /**
106: * @return array of inner class "records"
107: */
108: public final InnerClass[] getInnerClasses() {
109: return inner_classes;
110: }
111:
112: /**
113: * @param inner_classes the array of inner classes
114: */
115: public final void setInnerClasses(InnerClass[] inner_classes) {
116: this .inner_classes = inner_classes;
117: number_of_classes = (inner_classes == null) ? 0
118: : inner_classes.length;
119: }
120:
121: /**
122: * @return String representation.
123: */
124: public final String toString() {
125: StringBuffer buf = new StringBuffer();
126: for (int i = 0; i < number_of_classes; i++) {
127: buf.append(inner_classes[i].toString(constant_pool))
128: .append("\n");
129: }
130: return buf.toString();
131: }
132:
133: /**
134: * @return deep copy of this attribute
135: */
136: public Attribute copy(ConstantPool _constant_pool) {
137: InnerClasses c = (InnerClasses) clone();
138: c.inner_classes = new InnerClass[number_of_classes];
139: for (int i = 0; i < number_of_classes; i++) {
140: c.inner_classes[i] = inner_classes[i].copy();
141: }
142: c.constant_pool = _constant_pool;
143: return c;
144: }
145: }
|