1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.classfile;
19
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.util.Arrays;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.util.Args;
27 import org.apache.commons.lang3.ArrayUtils;
28
29
30
31
32
33
34
35
36 public final class NestMembers extends Attribute {
37
38 private int[] classes;
39
40
41
42
43
44
45
46
47
48
49 NestMembers(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
50 this(nameIndex, length, (int[]) null, constantPool);
51 final int classCount = input.readUnsignedShort();
52 classes = new int[classCount];
53 for (int i = 0; i < classCount; i++) {
54 classes[i] = input.readUnsignedShort();
55 }
56 }
57
58
59
60
61
62
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
72
73
74
75
76 public NestMembers(final NestMembers c) {
77 this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool());
78 }
79
80
81
82
83
84
85
86 @Override
87 public void accept(final Visitor v) {
88 v.visitNestMembers(this);
89 }
90
91
92
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
106
107
108
109
110 @Override
111 public void dump(final DataOutputStream file) throws IOException {
112 super.dump(file);
113 file.writeShort(classes.length);
114 for (final int index : classes) {
115 file.writeShort(index);
116 }
117 }
118
119
120
121
122 public int[] getClasses() {
123 return classes;
124 }
125
126
127
128
129 public String[] getClassNames() {
130 final String[] names = new String[classes.length];
131 Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class)));
132 return names;
133 }
134
135
136
137
138 public int getNumberClasses() {
139 return classes.length;
140 }
141
142
143
144
145 public void setClasses(final int[] classes) {
146 this.classes = ArrayUtils.nullToEmpty(classes);
147 }
148
149
150
151
152 @Override
153 public String toString() {
154 final StringBuilder buf = new StringBuilder();
155 buf.append("NestMembers(");
156 buf.append(classes.length);
157 buf.append("):\n");
158 for (final int index : classes) {
159 final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class);
160 buf.append(" ").append(Utility.compactClassName(className, false)).append("\n");
161 }
162 return buf.substring(0, buf.length() - 1);
163 }
164 }