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
24 import org.apache.bcel.Const;
25
26
27
28
29
30
31
32
33 public final class ModuleProvides implements Cloneable, Node {
34
35 private static String getImplementationClassNameAtIndex(final ConstantPool constantPool, final int index, final boolean compactClassName) {
36 final String className = constantPool.getConstantString(index, Const.CONSTANT_Class);
37 if (compactClassName) {
38 return Utility.compactClassName(className, false);
39 }
40 return className;
41 }
42 private final int providesIndex;
43 private final int providesWithCount;
44
45 private final int[] providesWithIndex;
46
47
48
49
50
51
52
53 ModuleProvides(final DataInput file) throws IOException {
54 providesIndex = file.readUnsignedShort();
55 providesWithCount = file.readUnsignedShort();
56 providesWithIndex = new int[providesWithCount];
57 for (int i = 0; i < providesWithCount; i++) {
58 providesWithIndex[i] = file.readUnsignedShort();
59 }
60 }
61
62
63
64
65
66
67
68 @Override
69 public void accept(final Visitor v) {
70 v.visitModuleProvides(this);
71 }
72
73
74
75
76 public ModuleProvides copy() {
77 try {
78 return (ModuleProvides) clone();
79 } catch (final CloneNotSupportedException e) {
80
81 }
82 return null;
83 }
84
85
86
87
88
89
90
91 public void dump(final DataOutputStream file) throws IOException {
92 file.writeShort(providesIndex);
93 file.writeShort(providesWithCount);
94 for (final int entry : providesWithIndex) {
95 file.writeShort(entry);
96 }
97 }
98
99
100
101
102
103
104
105
106 public String[] getImplementationClassNames(final ConstantPool constantPool, final boolean compactClassName) {
107 final String[] implementationClassNames = new String[providesWithCount];
108 for (int i = 0; i < providesWithCount; i++) {
109 implementationClassNames[i] = getImplementationClassNameAtIndex(constantPool, providesWithIndex[i], compactClassName);
110 }
111 return implementationClassNames;
112 }
113
114
115
116
117
118
119
120 public String getInterfaceName(final ConstantPool constantPool) {
121 return constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
122 }
123
124
125
126
127 @Override
128 public String toString() {
129 return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
130 }
131
132
133
134
135 public String toString(final ConstantPool constantPool) {
136 final StringBuilder buf = new StringBuilder();
137 final String interfaceName = getInterfaceName(constantPool);
138 buf.append(interfaceName);
139 buf.append(", with(").append(providesWithCount).append("):\n");
140 for (final int index : providesWithIndex) {
141 final String className = getImplementationClassNameAtIndex(constantPool, index, true);
142 buf.append(" ").append(className).append("\n");
143 }
144 return buf.substring(0, buf.length() - 1);
145 }
146 }