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
20 package org.apache.bcel.classfile;
21
22 import java.io.DataInput;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.util.Arrays;
26
27 import org.apache.bcel.Const;
28 import org.apache.bcel.util.Args;
29 import org.apache.commons.lang3.ArrayUtils;
30
31 /**
32 * This class is derived from <em>Attribute</em> and records the classes and interfaces that are authorized to claim
33 * membership in the nest hosted by the current class or interface. There may be at most one NestMembers attribute in a
34 * ClassFile structure.
35 *
36 * @see Attribute
37 */
38 public final class NestMembers extends Attribute {
39
40 private int[] classes;
41
42 /**
43 * Constructs object from input stream.
44 *
45 * @param nameIndex Index in constant pool.
46 * @param length Content length in bytes.
47 * @param dataInput Input stream.
48 * @param constantPool Array of constants.
49 * @throws IOException Thrown if an I/O error occurs.
50 */
51 NestMembers(final int nameIndex, final int length, final DataInput dataInput, final ConstantPool constantPool) throws IOException {
52 this(nameIndex, length, (int[]) null, constantPool);
53 classes = ClassParser.readU2U2Table(dataInput);
54 }
55
56 /**
57 * Constructs a new NestMembers.
58 *
59 * @param nameIndex Index in constant pool.
60 * @param length Content length in bytes.
61 * @param classes Table of indices in constant pool.
62 * @param constantPool Array of constants.
63 */
64 public NestMembers(final int nameIndex, final int length, final int[] classes, final ConstantPool constantPool) {
65 super(Const.ATTR_NEST_MEMBERS, nameIndex, length, constantPool);
66 this.classes = ArrayUtils.nullToEmpty(classes);
67 Args.requireU2(this.classes.length, "classes.length");
68 }
69
70 /**
71 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
72 * physical copy.
73 *
74 * @param c Source to copy.
75 */
76 public NestMembers(final NestMembers c) {
77 this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool());
78 }
79
80 /**
81 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
82 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
83 *
84 * @param v Visitor object.
85 */
86 @Override
87 public void accept(final Visitor v) {
88 v.visitNestMembers(this);
89 }
90
91 /**
92 * @return deep copy of this attribute.
93 */
94 @Override
95 public Attribute copy(final ConstantPool constantPool) {
96 final NestMembers c = (NestMembers) clone();
97 if (classes.length > 0) {
98 c.classes = classes.clone();
99 }
100 c.setConstantPool(constantPool);
101 return c;
102 }
103
104 /**
105 * Dumps NestMembers attribute to file stream in binary format.
106 *
107 * @param file Output file stream.
108 * @throws IOException Thrown if an I/O error occurs.
109 */
110 @Override
111 public void dump(final DataOutputStream file) throws IOException {
112 super.dump(file);
113 file.writeShort(Args.requireU2(classes.length, "classes.length"));
114 for (final int index : classes) {
115 file.writeShort(index);
116 }
117 }
118
119 /**
120 * Gets the classes.
121 *
122 * @return array of indices into constant pool of class names.
123 */
124 public int[] getClasses() {
125 return classes;
126 }
127
128 /**
129 * Gets the class names.
130 *
131 * @return string array of class names.
132 */
133 public String[] getClassNames() {
134 final String[] names = new String[classes.length];
135 Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class)));
136 return names;
137 }
138
139 /**
140 * Gets the number of classes.
141 *
142 * @return Length of classes table.
143 */
144 public int getNumberClasses() {
145 return classes.length;
146 }
147
148 /**
149 * Sets the classes.
150 *
151 * @param classes The list of class indexes Also redefines number_of_classes according to table length.
152 */
153 public void setClasses(final int[] classes) {
154 this.classes = ArrayUtils.nullToEmpty(classes);
155 }
156
157 /**
158 * @return String representation, that is, a list of classes.
159 */
160 @Override
161 public String toString() {
162 final StringBuilder buf = new StringBuilder();
163 buf.append("NestMembers(");
164 buf.append(classes.length);
165 buf.append("):\n");
166 for (final int index : classes) {
167 final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class);
168 buf.append(" ").append(Utility.compactClassName(className, false)).append("\n");
169 }
170 return buf.substring(0, buf.length() - 1); // remove the last newline
171 }
172 }