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