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 public final class ModulePackages extends Attribute {
36
37 private int[] packageIndexTable;
38
39
40
41
42
43
44
45
46
47
48 ModulePackages(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
49 this(nameIndex, length, (int[]) null, constantPool);
50 final int packageCount = input.readUnsignedShort();
51 packageIndexTable = new int[packageCount];
52 for (int i = 0; i < packageCount; i++) {
53 packageIndexTable[i] = input.readUnsignedShort();
54 }
55 }
56
57
58
59
60
61
62
63 public ModulePackages(final int nameIndex, final int length, final int[] packageIndexTable, final ConstantPool constantPool) {
64 super(Const.ATTR_MODULE_PACKAGES, nameIndex, length, constantPool);
65 this.packageIndexTable = ArrayUtils.nullToEmpty(packageIndexTable);
66 Args.requireU2(this.packageIndexTable.length, "packageIndexTable.length");
67 }
68
69
70
71
72
73
74
75 public ModulePackages(final ModulePackages c) {
76 this(c.getNameIndex(), c.getLength(), c.getPackageIndexTable(), c.getConstantPool());
77 }
78
79
80
81
82
83
84
85 @Override
86 public void accept(final Visitor v) {
87 v.visitModulePackages(this);
88 }
89
90
91
92
93 @Override
94 public Attribute copy(final ConstantPool constantPool) {
95 final ModulePackages c = (ModulePackages) clone();
96 if (packageIndexTable != null) {
97 c.packageIndexTable = packageIndexTable.clone();
98 }
99 c.setConstantPool(constantPool);
100 return c;
101 }
102
103
104
105
106
107
108
109 @Override
110 public void dump(final DataOutputStream file) throws IOException {
111 super.dump(file);
112 file.writeShort(packageIndexTable.length);
113 for (final int index : packageIndexTable) {
114 file.writeShort(index);
115 }
116 }
117
118
119
120
121 public int getNumberOfPackages() {
122 return packageIndexTable == null ? 0 : packageIndexTable.length;
123 }
124
125
126
127
128 public int[] getPackageIndexTable() {
129 return packageIndexTable;
130 }
131
132
133
134
135 public String[] getPackageNames() {
136 final String[] names = new String[packageIndexTable.length];
137 Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(packageIndexTable[i], Const.CONSTANT_Package)));
138 return names;
139 }
140
141
142
143
144 public void setPackageIndexTable(final int[] packageIndexTable) {
145 this.packageIndexTable = ArrayUtils.nullToEmpty(packageIndexTable);
146 }
147
148
149
150
151 @Override
152 public String toString() {
153 final StringBuilder buf = new StringBuilder();
154 buf.append("ModulePackages(");
155 buf.append(packageIndexTable.length);
156 buf.append("):\n");
157 for (final int index : packageIndexTable) {
158 final String packageName = super.getConstantPool().getConstantString(index, Const.CONSTANT_Package);
159 buf.append(" ").append(Utility.compactClassName(packageName, false)).append("\n");
160 }
161 return buf.substring(0, buf.length() - 1);
162 }
163 }