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 * @param nameIndex Index in constant pool to CONSTANT_Utf8
76 * @param length Content length in bytes
77 * @param innerClasses array of inner classes attributes
78 * @param constantPool Array of constants
79 */
80 public InnerClasses(final int nameIndex, final int length, final InnerClass[] innerClasses, final ConstantPool constantPool) {
81 super(Const.ATTR_INNER_CLASSES, nameIndex, length, constantPool);
82 this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
83 Args.requireU2(this.innerClasses.length, "innerClasses.length");
84 }
85
86 /**
87 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
88 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
89 *
90 * @param v Visitor object
91 */
92 @Override
93 public void accept(final Visitor v) {
94 v.visitInnerClasses(this);
95 }
96
97 /**
98 * @return deep copy of this attribute
99 */
100 @Override
101 public Attribute copy(final ConstantPool constantPool) {
102 // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes
103 final InnerClasses c = (InnerClasses) clone();
104 c.innerClasses = new InnerClass[innerClasses.length];
105 Arrays.setAll(c.innerClasses, i -> innerClasses[i].copy());
106 c.setConstantPool(constantPool);
107 return c;
108 }
109
110 /**
111 * Dump source file attribute to file stream in binary format.
112 *
113 * @param file Output file stream
114 * @throws IOException if an I/O error occurs.
115 */
116 @Override
117 public void dump(final DataOutputStream file) throws IOException {
118 super.dump(file);
119 file.writeShort(innerClasses.length);
120 for (final InnerClass innerClass : innerClasses) {
121 innerClass.dump(file);
122 }
123 }
124
125 /**
126 * @return array of inner class "records"
127 */
128 public InnerClass[] getInnerClasses() {
129 return innerClasses;
130 }
131
132 @Override
133 public Iterator<InnerClass> iterator() {
134 return Stream.of(innerClasses).iterator();
135 }
136
137 /**
138 * @param innerClasses the array of inner classes
139 */
140 public void setInnerClasses(final InnerClass[] innerClasses) {
141 this.innerClasses = innerClasses != null ? innerClasses : EMPTY_ARRAY;
142 }
143
144 /**
145 * @return String representation.
146 */
147 @Override
148 public String toString() {
149 final StringBuilder buf = new StringBuilder();
150 buf.append("InnerClasses(");
151 buf.append(innerClasses.length);
152 buf.append("):\n");
153 for (final InnerClass innerClass : innerClasses) {
154 buf.append(innerClass.toString(super.getConstantPool())).append("\n");
155 }
156 return buf.substring(0, buf.length() - 1); // remove the last newline
157 }
158 }