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