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