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
26 import org.apache.bcel.Const;
27
28 /**
29 * This class represents an entry in the provides table of the Module attribute. Each entry describes a service
30 * implementation that the parent module provides.
31 *
32 * @see Module
33 * @since 6.4.0
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; // points to CONSTANT_Class_info
45 private final int providesWithCount;
46
47 private final int[] providesWithIndex; // points to CONSTANT_Class_info
48
49 /**
50 * Constructs object from file stream.
51 *
52 * @param dataInput Input stream.
53 * @throws IOException if an I/O Exception occurs in readUnsignedShort.
54 */
55 ModuleProvides(final DataInput dataInput) throws IOException {
56 providesIndex = dataInput.readUnsignedShort();
57 providesWithIndex = ClassParser.readU2U2Table(dataInput);
58 providesWithCount = providesWithIndex.length;
59 }
60
61 /**
62 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
63 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
64 *
65 * @param v Visitor object.
66 */
67 @Override
68 public void accept(final Visitor v) {
69 v.visitModuleProvides(this);
70 }
71
72 /**
73 * @return deep copy of this object.
74 */
75 public ModuleProvides copy() {
76 try {
77 return (ModuleProvides) clone();
78 } catch (final CloneNotSupportedException e) {
79 // TODO should this throw?
80 }
81 return null;
82 }
83
84 /**
85 * Dumps table entry to file stream in binary format.
86 *
87 * @param file Output file stream.
88 * @throws IOException if an I/O Exception occurs in writeShort.
89 */
90 public void dump(final DataOutputStream file) throws IOException {
91 file.writeShort(providesIndex);
92 file.writeShort(providesWithCount);
93 for (final int entry : providesWithIndex) {
94 file.writeShort(entry);
95 }
96 }
97
98 /**
99 * Gets the array of implementation class names for this ModuleProvides.
100 *
101 * @param constantPool Array of constants usually obtained from the ClassFile object.
102 * @param compactClassName false for original constant pool value, true to replace '/' with '.'.
103 * @return array of implementation class names.
104 * @since 6.10.0
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 * Gets the interface name for this ModuleProvides.
116 *
117 * @param constantPool Array of constants usually obtained from the ClassFile object.
118 * @return interface name.
119 * @since 6.10.0
120 */
121 public String getInterfaceName(final ConstantPool constantPool) {
122 return constantPool.constantToString(providesIndex, Const.CONSTANT_Class);
123 }
124
125 /**
126 * @return String representation.
127 */
128 @Override
129 public String toString() {
130 return "provides(" + providesIndex + ", " + providesWithCount + ", ...)";
131 }
132
133 /**
134 * @return Resolved string representation.
135 */
136 public String toString(final ConstantPool constantPool) {
137 final StringBuilder buf = new StringBuilder();
138 final String interfaceName = getInterfaceName(constantPool);
139 buf.append(interfaceName);
140 buf.append(", with(").append(providesWithCount).append("):\n");
141 for (final int index : providesWithIndex) {
142 final String className = getImplementationClassNameAtIndex(constantPool, index, true);
143 buf.append(" ").append(className).append("\n");
144 }
145 return buf.substring(0, buf.length() - 1); // remove the last newline
146 }
147 }