View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.stream.Stream;
27  
28  import org.apache.bcel.Const;
29  import org.apache.bcel.util.Args;
30  
31  /**
32   * This class is derived from <em>Attribute</em> and denotes that this class is an Inner class of another. to the source
33   * file of this class. It is instantiated from the <em>Attribute.readAttribute()</em> method.
34   *
35   * @see Attribute
36   */
37  public final class InnerClasses extends Attribute implements Iterable<InnerClass> {
38  
39      /**
40       * Empty array.
41       */
42      private static final InnerClass[] EMPTY_ARRAY = {};
43  
44      private InnerClass[] innerClasses;
45  
46      /**
47       * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
48       * physical copy.
49       *
50       * @param c Source to copy.
51       */
52      public InnerClasses(final InnerClasses c) {
53          this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool());
54      }
55  
56      /**
57       * Constructs object from input stream.
58       *
59       * @param nameIndex Index in constant pool to CONSTANT_Utf8.
60       * @param length Content length in bytes.
61       * @param input Input stream.
62       * @param constantPool Array of constants.
63       * @throws IOException if an I/O error occurs.
64       */
65      InnerClasses(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
66          this(nameIndex, length, (InnerClass[]) null, constantPool);
67          final int classCount = input.readUnsignedShort();
68          innerClasses = new InnerClass[classCount];
69          for (int i = 0; i < classCount; i++) {
70              innerClasses[i] = new InnerClass(input);
71          }
72      }
73  
74      /**
75       * Constructs an InnerClasses attribute.
76       *
77       * @param nameIndex Index in constant pool to CONSTANT_Utf8.
78       * @param length Content length in bytes.
79       * @param innerClasses array of inner classes attributes.
80       * @param constantPool Array of constants.
81       */
82      public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
83          super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
84          this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
85          Args.requireU2(this.innerClasses.length, "innerClasses.length");
86      }
87  
88      /**
89       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
90       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
91       *
92       * @param v Visitor object.
93       */
94      @Override
95      public void accept(final Visitor v) {
96          v.visitInnerClasses(this);
97      }
98  
99      /**
100      * Creates a deep copy of this attribute.
101      *
102      * @return deep copy of this attribute.
103      */
104     @Override
105     public Attribute copy(final ConstantPool constantPool) {
106         // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
107         final InnerClasses c = (InnerClasses) clone();
108         c.innerClasses = new InnerClass[innerClasses.length];
109         Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
110         c.setConstantPool(constantPool);
111         return c;
112     }
113 
114     /**
115      * Dumps source file attribute to file stream in binary format.
116      *
117      * @param file Output file stream.
118      * @throws IOException if an I/O error occurs.
119      */
120     @Override
121     public void dump(final DataOutputStream file) throws IOException {
122         super.dump(file);
123         file.writeShort(innerClasses.length);
124         for (final InnerClass innerClass : innerClasses) {
125             innerClass.dump(file);
126         }
127     }
128 
129     /**
130      * Gets the array of inner class records.
131      *
132      * @return array of inner class "records".
133      */
134     public InnerClass[] getInnerClasses() {
135         return innerClasses;
136     }
137 
138     @Override
139     public Iterator<InnerClass> iterator() {
140         return Stream.of(innerClasses).iterator();
141     }
142 
143     /**
144      * Sets the array of inner classes.
145      *
146      * @param innerClasses the array of inner classes.
147      */
148     public void setInnerClasses(final InnerClass[] innerClasses) {
149         this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
150     }
151 
152     /**
153      * @return String representation.
154      */
155     @Override
156     public String toString() {
157         final StringBuilder buf = new StringBuilder();
158         buf.append("InnerClasses(");
159         buf.append(innerClasses.length);
160         buf.append("):\n");
161         for (final InnerClass innerClass : innerClasses) {
162             buf.append(innerClass.toString(super.getConstantPool())).append("\n");
163         }
164         return buf.substring(0, buf.length() - 1); // remove the last newline
165     }
166 }